2016-06-17 53 views
1

我有一個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的問題'錯誤信息是什麼?'

Error

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 
+0

邏輯似乎是好的。你是否肯定「Me.txtBoxUsername.Value」和「Me.txtBoxPassword.Value」是你認爲的?即當你調試它們的值是'RJ1'? – Brad

+0

或者,您可以使用參數化記錄集來確認用戶/密碼,它不會有用戶輸入'''和dlookup失敗的風險。另外,當您獲得確認記錄集時,您還會看到其他可能像權限一樣有用的列。 – Brad

+1

它看起來像我可以輸入我的用戶名和我的同事密碼,它會讓我進來。不應該'DLookups'搜索「Staff Table」中同一行上的匹配條目嗎? – MoondogsMaDawg

回答

1

我想你DLookups是語法錯誤失敗的原因。嘗試:

If ((IsNull(DLookup("[Username]", "Staff Table", "[Username] = '" & Me.txtBoxUsername.Value & "'"))) Or _ 
(IsNull(DLookup("[Password]", "Staff Table", "[Password] = '" & Me.txtBoxPassword.Value & "'")))) Then 
+0

剛纔看到你的雙引號沒有正確地連接字符串。我編輯了我的答案。 – MoondogsMaDawg

+0

輝煌,工作。一旦將「RJ1」輸入到用戶名和密碼字段中,就會顯示一條消息,指出用戶名和密碼正確,然後打開所有5個表單。 :D – FutureProgrammer

5

由於Christopher pointed out,這If表達式計算在Staff Table的用戶名和密碼是否被發現,但在同一行中不一定找到。所以,如果你提交與其他用戶的密碼一個用戶名,這個邏輯會考慮這些值是一個有效的組合:

'proccess the job 
If ((IsNull(DLookup("Username", "Staff Table", "Username='" & Me.txtBoxUsername.Value & "'"))) Or _ 
    (IsNull(DLookup("[Password]", "Staff Table", "[Password]='" & Me.txtBoxPassword.Value & "'")))) Then 
    ' Note: quotes added .....................................^.............................^ 

請務必檢查用戶名和密碼是否在同一行中存在:

Dim strCriteria As String 
strCriteria = "Username='" & Me.txtBoxUsername.Value & "' AND [Password]='" & Me.txtBoxPassword.Value & "'" 
Debug.Print strCriteria '<- inspect in Immediate window; Ctrl+g will take you there 
If DCount("*", "Staff Table", strCriteria) = 0 Then 
    MsgBox "Incorrect Username Or Password" 
End If 

儘管該更改在邏輯上可能是正確的,但當用戶名或密碼包含撇號時它可能會中斷。一個不太脆弱的方法是基於參數查詢的Recordset

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 
    MsgBox "Incorrect Username Or Password" 
End If 
+0

無論用戶名和密碼如何,單擊「登錄」按鈕後都會返回錯誤。另外,在'「WHERE用戶名= [pUser]和[密碼] = [pPWD];」'有助於這個錯誤嗎? – FutureProgrammer

+0

哪一行觸發錯誤,錯誤信息是什麼? – HansUp

+0

儘管第5行在'Username'的開頭和末尾缺少括號,但我不確定哪一行,因爲它們在單詞'Password'上可見。 – FutureProgrammer

相關問題