I have a MS Access 2016 database where the user logs into the database with a username a password. The code would then open a set of forms BUT there is an error in which I cannot find.
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
The username and password for a member of staff is 'RJ1'. When I try to login with these credentials, the MsgBox "Incorrect Username Or Password"
shows up.
Why is this happening?
*In return to HansUp's question 'what is the error message?'
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