Microsoft Access编译错误:找不到方法或数据成员

时间:2017-11-05 15:13:15

标签: vba ms-access access-vba

我正在为我的数据库创建一个简单的登录表单。当我单击登录时,消息"编译错误:方法或数据成员未找到"出现。我该如何解决这个问题?谢谢!代码如下

Option Compare Database
Option Explicit

Private Sub btnLogin_Click()
    Dim rs As Recordset

    Set rs = CurrentDb.OpenRecordset("TBL:Staff", dbOpenSnapshot, dbReadOnly)

    rs.FindFirst "UserName='" & Me.txtUserName & "'"

    If rs.NoMatch = True Then
        Me.lblWrongUser.Visible = True
        Me.txtUserName.SetFocus
        Exit Sub
        Me.lblWrongUser.Visible = False

    If rs!Password <> Nz(Me.txtPassword, "") Then
        Me.lblWrongPass.Visible = True
        Me.txtPassword.SetFocus
        Exit Sub
    End If
    Me.lblWrongPass.Visible = False
    DoCmd.OpenForm "FRM:Customer"
    DoCmd.Close acForm, Me.Name
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个。

检查已提供的用户名和密码值,然后通过简单的DCount查看它们是否存在于数据库中。

如果存在用户名/密码,则会返回&gt;如果不是0,它将返回0.

Private Sub btnLogin_Click()
    With Me
        'Username/Password - value provided?
        If IsNull(.txtUserName.Value) Or IsNull(.txtPassword.Value) Then
            MsgBox "Both fields required.", vbExclamation
            Exit Sub
        End If

        'Username exists in Table?
        If DCount("*", "Staff", "UserName='" & .txtUserName.Value & "'") = 0 Then
            .lblWrongUser.Visible = True
            .txtUserName.SetFocus
            Exit Sub
        End If

        'Password exists in Table?
        If DCount("*", "Staff", "UserName='" & .txtUserName.Value & _
                                "' And Password='" & .txtPassword.Value & "'") = 0 Then
            .lblWrongPass.Visible = True
            .txtPassword.SetFocus
            Exit Sub
        End If
    End With

    'Code will reach here only if supplied username and passowrd are correct
    With DoCmd
        .OpenForm "Customer", acNormal, , , acFormPropertySettings, acWindowNormal
        .Close acForm, Me.Name, acSavePrompt
    End With
End Sub