Hi, anyone here could help me on my codes. I want to validate my username and password from my user table. If username is not found in user table a message should appear "Username Invalid", and If password is invalid a message should appear "Password is invalid." and if Username is correct and password is Invalid a messages should be "Password is invalid", if password is correct and username is wrong a message should appear "Password is invalid". Here's my code..I am using MS Access (VBA)Programmin gThanks in advance
Code:
Private Sub cmdLogin_Click() Call Login End Sub Public Sub Login() On Error GoTo ErrorHandler: If IsNull(txtUsername) = True Then 'Check Username MsgBox "Username is required" txtUsername.SetFocus End If If IsNull(txtPassword) = True Then 'Check Password MsgBox "Password is required" txtPassword.SetFocus End If 'If (txtUsername) = True And (txtPassword) = True Then 'Compare value of txtUsername with the saved EmployeeID in tblUser If Me.txtUsername.Value = DLookup("EmployeeID", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") Then strUser = Me.txtUsername.Value 'Set the value of the strUser declared as Global variable strRole = DLookup("Role", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") 'set the value of strRole declared as Global Variable DoCmd.Close acForm, "frmUserLogin", acSaveNo MsgBox "Welcome to Main Page!" & strUser, vbOKOnly, "Welcome" 'Close logon form and open Main page DoCmd.OpenForm "frmMainPage", acNormal, "", "", , acNormal Else MsgBox "Invalid Username! Please try again.", vbOKOnly, "Invalid Username" intLogAttempt = intLogAttempt + 1 txtUsername.SetFocus End If 'Compare value of txtPassword with the saved Password in tblUser If Me.txtPassword.Value = DLookup("Password", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") Then strUser = Me.txtUsername.Value 'Set the value of the strUser declared as Global variable strRole = DLookup("Role", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") 'set the value of strRole declared as Global Variable DoCmd.Close acForm, "frmUserLogin", acSaveNo MsgBox "Welcome to Main Page!" & strUser, vbOKOnly, "Welcome" 'Close logon form and open Main page DoCmd.OpenForm "frmMainPage", acNormal, "", "", , acNormal Else MsgBox "Invalid Password! Please try again.", vbOKOnly, "Invalid Password" intLogAttempt = intLogAttempt + 1 txtPassword.SetFocus End If 'If the user enters incorrect password for 3 times database will shutdown 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
Comment