Greetings,
I’ve a login form that users have to input username/password to access the DB.
However, here is some scenarios that I’d like to enhance the code to avoid the following error:
1. If an invalid username is entered, I get the following error:
Run-time error ‘3021’
No current record.
2. An attempt to click on password w/o entering a user name will result in the following error:
Run-time error ’94:
Invalid use of Null
3. If password entered is incorrect,
The logon button and password text will grey out. To successfully login with a valid password, one must click ‘enter’ on keyboard.
TIA
Regards,
I’ve a login form that users have to input username/password to access the DB.
However, here is some scenarios that I’d like to enhance the code to avoid the following error:
1. If an invalid username is entered, I get the following error:
Run-time error ‘3021’
No current record.
2. An attempt to click on password w/o entering a user name will result in the following error:
Run-time error ’94:
Invalid use of Null
3. If password entered is incorrect,
The logon button and password text will grey out. To successfully login with a valid password, one must click ‘enter’ on keyboard.
Code:
Private Sub txtUserNm_BeforeUpdate(Cancel As Integer)
'Check To See If User Are Valid. Look Into The Table To Get User Status
Dim strUser As String
Dim strStatus As String
Dim strSQL As String
Dim db As DAO.Database
Dim rstStatus As DAO.Recordset
Set db = OpenDatabase("C:\DB folder path")
Set rstStatus = db.OpenRecordset("tblLOGIN")
strUser = Me.txtUserNm
rstStatus.Index = "USERNM"
rstStatus.Seek "=", strUser
If rstStatus!STATUS = "A" Then
strStatus = rstStatus!STATUS
Me.txtStatus = strStatus
End If
If Me.txtStatus = "A" Then
Me.txtPWD.Enabled = True
' Me.txtPWD.SetFocus
Me.txtValidUser = "Y"
Else
MsgBox "Invalid User Name Try Again" & vbCrLf & " GOOD-BYE", vbCritical + vbOKOnly, "TRY AGAIN LATER"
Me.txtValidUser = "N"
Me.txtPWD.Enabled = False
Me.cmdLOGIN.Enabled = False
DoCmd.Quit
End If
End Sub
Private Sub cmdLOGIN_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.txtUserNm) Or Me.txtUserNm = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtUserNm.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPWD) Or Me.txtPWD = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPWD.SetFocus
Exit Sub
End If
'Enable certain options on the main menu screen depending on access level
DoCmd.OpenForm "frmMainMenu"
If Me.txtPWD Like "SU*" Then
Forms!frmMainMenu!optMaintenance.Enabled = False
Else
If Me.txtPWD Like "US*" Then
Forms!frmMainMenu!optReports.Enabled = False
Forms!frmMainMenu!optSuspCase.Enabled = False
Forms!frmMainMenu!optMaintenance.Enabled = False
End If
End If
TIA
Regards,