我有两个字段,用户名和密码,目前有两个用户,我的代码只接受第一个用户的用户名和密码,但不接受第二个用户。似乎无法弄清楚我应该在哪里做出改变。我在与重复数据输入相关的ms访问中收到错误。
请告诉我如何更改我的代码,目前我的可视化基本代码逻辑是,如果输入的用户名和密码正确,则打开表单
Private Sub LoginBtn_Click()
Set L = CurrentDb.OpenRecordset("Login")
'validation to check if the user entered the username in the username field
If IsNull(Me.txtUsername) Or Me.txtUsername = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUsername.SetFocus
Exit Sub
End If
'validation to check if the user entered the password in the password field
If IsNull(Me.strPassword) Or Me.strPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.strPassword.SetFocus
Exit Sub
End If
'check to see if the password and username match
If (Me.txtUsername = L.[Username]) And (Me.strPassword = L.[Password]) Then
'Close Member login form and open the member form
DoCmd.OpenForm "Memberform"
Else
MsgBox "Password Invalid. Please try again.", vbOKOnly, "Invalid Entry!"
Me.strPassword.SetFocus
End If
End Sub
答案 0 :(得分:1)
看起来问题是最后一个If块:你打开登录表(或查询?)但你只是查看第一条记录,而不是循环遍历所有记录来检查如果有匹配。
因此,您可以通过更改它来循环记录直到找到匹配(不推荐),或者您可以改为查询其条件是提供的用户名和密码并查看是否返回任何记录来解决此问题。
Private Sub LoginBtn_Click()
'validation to check if the user entered the username in the username field
If IsNull(Me.txtUserName) Or Me.txtUserName = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUserName.SetFocus
Exit Sub
End If
'validation to check if the user entered the password in the password field
If IsNull(Me.strPassword) Or Me.strPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.strPassword.SetFocus
Exit Sub
End If
'check to see if the password and username match
Set L = CurrentDb.OpenRecordset("Login")
Dim found As Boolean
found = False
If L.BOF And L.EOF Then
MsgBox "There are no usernames or passwords in the database.", vbOKOnly, "No login info"
Exit Sub
Else
Do While Not L.EOF
If (Me.txtUserName = L.[UserName]) And (Me.strPassword = L.[Password]) Then
found = True
Exit Do
End If
L.MoveNext
Loop
End If
If L.EOF And Not found Then
' at the last record and did not find a match
MsgBox "Invalid username or password. Please try again.", vbOKOnly, "Invalid Entry!"
Me.strPassword.SetFocus
' clean up
L.Close
Set L = Nothing
Else
' we found a match; close Member login form and open the member form
' clean up
L.Close
Set L = Nothing
DoCmd.OpenForm "Memberform"
DoCmd.Close acForm, Me.Name
End If
End Sub
Private Sub LoginBtn_Click()
'validation to check if the user entered the username in the username field
If IsNull(Me.txtUsername) Or Me.txtUsername = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUsername.SetFocus
Exit Sub
End If
'validation to check if the user entered the password in the password field
If IsNull(Me.strPassword) Or Me.strPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.strPassword.SetFocus
Exit Sub
End If
'check to see if the password and username match
' Using a QueryDef with parameters to avoid SQL injection
Dim qdf as DAO.QueryDef
Set qdf = CurrentDb.CreateQueryDef("","SELECT Username, Password FROM Login WHERE Username=[_uname] AND Password=[_pw]")
qdf.Parameters("_uname") = Me.txtUsername
qdf.Parameters("_pw") = Me.strPassword
Set L = qdf.OpenRecordset
If L.BOF And L.EOF Then
' no match
MsgBox "Invalid username or password. Please try again.", vbOKOnly, "Invalid Entry!"
Me.strPassword.SetFocus
' clean up
L.Close
Set L = Nothing
Else
' we found a match; close Member login form and open the member form
'clean up
L.Close
Set L = Nothing
DoCmd.OpenForm "Memberform"
DoCmd.Close acForm, Me.Name
End If
End Sub