我正在尝试为我的Access数据库创建一个登录名,但我无法让它工作。这是我的代码(请记住" Preparer"是包含用户名和密码信息的表的名称:
Private Sub Command1_Click()
If IsNull(Me.txtLogin) Then
MsgBox "Please Enter Login", vbInformation, "Need ID"
Me.txtLogin.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password",vbInformation, "Need Password"
Me.txtPassword.SetFocus
Else
If ((IsNull(DLookup("Login","Preparer","Login='& Me.txtLogin.Value &'"))) or _
(IsNull(DLookup("Password","Preparer","Password='& Me.txtPassword.Value &'")))) Then
MsgBox "Incorrect Login or Password"
Else
DoCmd.Close
MsgBox "Success"
DoCmd.OpenForm "Master"
End If
End If
End Sub
前两个部分有效,所以如果登录或密码为空,则会给出正确的错误信息,但每当我输入登录名和密码时,即使它们是正确的(即它们存在于编制者表中),它也会给我& #34;登录或密码不正确"
我假设问题出现在这里的代码中:
If ((IsNull(DLookup("Login","Preparer","Login='& Me.txtLogin.Value &'"))) or _
(IsNull(DLookup("Password","Preparer","Password='& Me.txtPassword.Value &'")))) Then
MsgBox "Incorrect Login or Password"
任何人都明白为什么我无法理解其他声明?
答案 0 :(得分:3)
您没有比较正确的信息。您应该检查表中是否存在为Password
输入的Username
。所以你的代码应该只改为一个单一的DLookup,如。
Private Sub Command1_Click()
If IsNull(Me.txtLogin) Then
MsgBox "Please Enter Login", vbInformation, "Need ID"
Me.txtLogin.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password",vbInformation, "Need Password"
Me.txtPassword.SetFocus
Else
If Nz(DLookup("Password", "Preparer", "Login = '" & Me.txtLogin.Value & "'")), "GarbageEntry") = Me.txtPassword Then
DoCmd.Close
MsgBox "Success"
DoCmd.OpenForm "Master"
Else
MsgBox "Incorrect Login or Password"
End If
End If
End Sub
在上面的代码中,您首先使用DLookup检索用户名的密码。如果用户名匹配,将返回密码,ELSE默认值为" GarbageEntry "退回。因此,将正确的密码(或)GarbageEntry与输入的实际密码进行比较。如果它们是相同的,那么你给他们访问权限。还有一条消息。
希望这有帮助!