在Access中根据其用户名验证密码

时间:2015-11-30 17:53:43

标签: ms-access access-vba

我在Access上有一个登录表单,如果用户输入Login表中的任何用户名和密码,将允许用户登录。它不会检查用户名和密码是否相互匹配。我该如何解决这个问题?

这是我的代码:

Private Sub Command4_Click()

If IsNull(Me.txtLog) Then
MsgBox "Please enter username", vbInformation, "Username required"
Me.txtLog.SetFocus

ElseIf IsNull(Me.txtPass) Then
MsgBox "Please enter password", vbInformation, "Password required"
Me.txtPass.SetFocus

Else
'checking Password And UserName
If (IsNull(DLookup("Username", "Login", "Username = '" & Me.txtLog.Value & "'"))) Or _
(IsNull(DLookup("Password", "Login", "Password = '" & Me.txtPass.Value & "'"))) Then
MsgBox "Incorrect Username or Password"

Else
    DoCmd.OpenForm "Main Menu"

End If
End If
End Sub

1 个答案:

答案 0 :(得分:0)

检查是否有任何行与用户名和密码匹配。

Dim strCriteria As String
strCriteria = "Username = '" & Me.txtLog.Value & _
    "' AND [Password] = '" & Me.txtPass.Value & "'"
Debug.Print strCriteria '<- view this in Immediate window; Ctrl+g will take you there
If DCount("*", "[Login]", strCriteria) = 0 Then
    MsgBox "Incorrect Username or Password"
Else
    DoCmd.OpenForm "Main Menu"
End If