Option Compare Database
Option Explicit
Public lngLogAttempts As Long
Const Mssg1 = " Please Enter User Name and Password. "
Const Mssg2 = "Logon Details Incorrect... User Not Found. "
Const Title1 = " Logon Error"
Const Mssg3 = " Password must be typed identically."
Private Sub cboLogin_AfterUpdate()
Me.txtPsswd.SetFocus
End Sub
Private Sub cmdSubmit_Click()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb()
strSQL = "SELECT * FROM tblEmployee WHERE [EmployeeID] = " & Me.cboLogin.Value
'CHECKING FOR NULLS
If IsNull(Me.cboLogin) Then
MsgBox Mssg1, vbCritical, Title1
Me.cboLogin.SetFocus
Call CheckLogAttempts
ElseIf IsNull(Me.txtPassword) Then
MsgBox Mssg1, vbCritical, Title1
Me.txtPassword.SetFocus
Call CheckLogAttempts
'VALIDATING PASSWORD
Else
If Me.txtPassword.Value = DLookup("Password", "tblEmployee", "[EmployeeID]=" & Me.cboLogin.Value) Then
'PASSING VARIABLES TO DB
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
With User
.CurrentUser = rst.Fields("EmployeeID")
End With
rst.Close
'Open Appropriate Home Form
If Me.txtPassword.Value = "changeme" Then
DoCmd.OpenForm "frmchangePassword", acNormal, , "EmployeeID=" & Me!cboLogin, acFormEdit, acDialog
Else
Dim strAccessLevel As String
Dim strForm As String
strAccessLevel = Nz(DLookup("UserTypeID", "tblEmployee", "[EmployeeID]=" & Me.cboLogin.Value), "4")
Select Case strAccessLevel
Case 1
strForm = "frmHome"
Case 2
strForm = "frmHomeOperator"
Case 3
strForm = "frmHomeManager"
Case 4
strForm = "frmHomeUser"
End Select
Me.Visible = False
DoCmd.OpenForm strForm
Me.Visible = False
End If
'Close Login Form
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
Call CheckLogAttempts
End If
End If
End Sub
Private Sub CheckLogAttempts()
If lngLogAttempts >= 2 Then
MsgBox " It appears you are having trouble logging on. " & Chr(13) & "Please contact your System Administrator " & "for assistance. ", vbCritical, Title1
DoCmd.CloseDatabase
Else
lngLogAttempts = lngLogAttempts + 1
End If
End Sub
Private Sub Form_Load()
On Error GoTo Err_Form_Load
Me.cboLogin.SetFocus
lngLogAttempts = 0
Exit_Form_Load:
Exit Sub
Err_Form_Load:
MsgBox err.Description, , " LogOn Error"
Resume Exit_Form_Load
End Sub