Public Sub Login()
On Error GoTo ErrorHandler:
If IsNull([cboUser]) = True Then 'Check UserName is completed
MsgBox "Username is required"
ElseIf IsNull([txtPassword]) = True Then 'Check Password is completed
MsgBox "Password is required"
ElseIf Me.txtPassword.Value <> DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") Then
MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Password"
intLogAttempt = intLogAttempt + 1
txtPassword.SetFocus
End If
'Compare value of txtPassword with the saved Password in tblUser
'Admin Role
If Me.txtPassword.Value = DLookup("Password", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") Then
strUser = Me.cboUser.Value 'Set the value of strUser declared as Global Variable
strRole = DLookup("Role", "tblUsers", "[UserName]='" & Me.cboUser.Value & "'") 'set the value of strRole declared as Global Variable
If strRole = "admin" Then
DoCmd.OpenForm "adminMenu", acNormal, "", "", , acNormal
DoCmd.Close acForm, "frmLogin", acSaveNo
MsgBox "Welcome Back, " & strUser, vbOKOnly, "Welcome"
'Staff Role
ElseIf strRole = "staff" Then
DoCmd.OpenForm "staffmenu", acNormal
DoCmd.Close acForm, "frmLogin", acSaveNo
MsgBox "Welcome Back, " & strUser, vbOKOnly, "Welcome"
End If
End If
'Check if the user has 3 wrong log-in attempts and close the application
If intLogAttempt = 3 Then
MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _
"Application will exit.", vbCritical, "Restricted Access!"
Application.Quit
End If
ErrorHandler:
End Sub