我有一個MS Access 2016數據庫,用戶使用用戶名密碼登錄數據庫。代碼會打開一組表單,但是有一個我找不到的錯誤。「用戶名和密碼錯誤」雖然正確嗎?
Private Sub btnLogin_Click()
If IsNull(Me.txtBoxUsername) Then
MsgBox "Please Enter Username", vbInformation, "Username Required"
Me.txtBoxUsername.SetFocus
ElseIf IsNull(Me.txtBoxPassword) Then
MsgBox "Please Enter Password", vbInformation, "Password Required"
Me.txtBoxPassword.SetFocus
Else
'proccess the job
If ((IsNull(DLookup("Username", "Staff Table", "Username='& Me.txtBoxUsername.Value &'"))) Or _
(IsNull(DLookup("Password", "Staff Table", "Password='& Me.txtBoxPassword.Value &'")))) Then
MsgBox "Incorrect Username Or Password"
Else
MsgBox "Username & Password Correct"
DoCmd.OpenForm "Branch Form"
DoCmd.OpenForm "Customer Form"
DoCmd.OpenForm "Item Form"
DoCmd.OpenForm "Order Form"
DoCmd.OpenForm "Staff Form"
End If
End If
End Sub
員工的用戶名和密碼是'RJ1'。當我嘗試使用這些憑據登錄時,出現MsgBox "Incorrect Username Or Password"
。
這是怎麼發生的?
*爲了回答HansUp的問題'錯誤信息是什麼?'
SOLUTION:
Private Sub btnLogin_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSelect As String
strSelect = "SELECT Count(*) FROM [Staff Table]" & vbCrLf & _
"WHERE Username=[pUser] AND [Password]=[pPWD];"
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSelect)
qdf.Parameters("pUser").Value = Me!txtBoxUsername.Value
qdf.Parameters("pPWD").Value = Me!txtBoxPassword.Value
If qdf.OpenRecordset(dbOpenSnapshot)(0) = 0 Then
End If
If IsNull(Me.txtBoxUsername) Then
MsgBox "Please Enter Username", vbInformation, "Username Required"
Me.txtBoxUsername.SetFocus
ElseIf IsNull(Me.txtBoxPassword) Then
MsgBox "Please Enter Password", vbInformation, "Password Required"
Me.txtBoxPassword.SetFocus
Else
'proccess the job
If ((IsNull(DLookup("[Username]", "Staff Table", "[Username] = '" & Me.txtBoxUsername.Value & "'"))) Or _
(IsNull(DLookup("[Password]", "Staff Table", "[Password] = '" & Me.txtBoxPassword.Value & "'")))) Then
MsgBox "Incorrect Username Or Password"
Else
DoCmd.OpenForm "Branch Form"
DoCmd.OpenForm "Customer Form"
DoCmd.OpenForm "Item Form"
DoCmd.OpenForm "Order Form"
DoCmd.OpenForm "Staff Form"
End If
End If
End Sub
邏輯似乎是好的。你是否肯定「Me.txtBoxUsername.Value」和「Me.txtBoxPassword.Value」是你認爲的?即當你調試它們的值是'RJ1'? – Brad
或者,您可以使用參數化記錄集來確認用戶/密碼,它不會有用戶輸入'''和dlookup失敗的風險。另外,當您獲得確認記錄集時,您還會看到其他可能像權限一樣有用的列。 – Brad
它看起來像我可以輸入我的用戶名和我的同事密碼,它會讓我進來。不應該'DLookups'搜索「Staff Table」中同一行上的匹配條目嗎? – MoondogsMaDawg