I have added a login form to an Access Database. I am trying to figure out a way to limit the number of times a user can enter an incorrect password. In the VBA code shown below, when the user inputs a password that is incorrect, the following message box triggers, "MsgBox "Incorrect password", vbOKOnly vbExclamation".
I would like the user to get three failed attempts before a different message box appears. Something like, "Please contact administrator"...
Thanks in advance for the help.
Private Sub OkBTN_Click()
'Check that User is selected
If IsNull(Me.cboUser) Then
MsgBox "You forgot to select your name from the drop down menu!", vbCritical
Me.cboUser.SetFocus
Else
'Check for correct password
If Me.txtPassword = Me.cboUser.Column(2) Then
'Check if password needs to be reset
If Me.cboUser.Column(4) Then
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
Me.Visible = False
Else
MsgBox "Incorrect password", vbOKOnly vbExclamation
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
End If
End Sub
CodePudding user response:
In this situation I think I'd declare a Static
counter variable. The value of a static variable is preserved between procedure calls. Other alternatives would be to store the count in a a global variable, use a TempVar, or write them to a table.
Private Sub OkBTN_Click()
Static intIncorrectCount As Integer ' The value of a static variable is preserved between procedure calls.
'Check that User is selected
If IsNull(Me.cboUser) Then
MsgBox "You forgot to select your name from the drop down menu!", vbCritical
Me.cboUser.SetFocus
Else
'Check for correct password
If Me.txtPassword = Me.cboUser.Column(2) Then
'Check if password needs to be reset
If Me.cboUser.Column(4) Then
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
Me.Visible = False
intIncorrectCount = 0
ElseIf intIncorrectCount > 2 Then
MsgBox "Too many incorrect attempts. Closing Access.", vbOKOnly vbExclamation
'DoCmd.Quit ' ### Save your code before enabling this line. ###
Else
MsgBox "Incorrect password", vbOKOnly vbExclamation
Me.txtPassword = Null
Me.txtPassword.SetFocus
intIncorrectCount = intIncorrectCount 1
End If
End If
End Sub
CodePudding user response:
Okay thank you Ben . I have made some changes and am now getting error, "Compile error: Else without if". I think I got things out of order, but am having a tough time getting it back.
Private Sub OkBTN_Click()
Static intIncorrectCount As Integer
Dim AuthorityNumber As Integer
'Dim rs As Recordset
'Column definitions
'UserID = 0
'FullName = 1
'Password = 2
'PWReset = 3
'AccessLevelID = 4
'Set rs = CurrentDb.OpenRecordset("UserNameQuery", dbOpenSnapshot)
'N = Nz(DLookup("Fullname", "UserNameQuery", "Fullname=""" & Me.cboUser & """"), " ")
If IsNull(Me.cboUser) Then 'Check that User is selected
MsgBox "You forgot to select your name from the drop down menu!", vbCritical
Me.cboUser.SetFocus
Else
If Me.txtPassword = Me.cboUser.Column(2) Then 'Check for correct password
If Me.cboUser.Column(3) Then 'Check if password needs to be reset
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
If Me.cboUser.Column(4) Then
DoCmd.OpenForm "SRL1MainMenu"
Forms!SRL1MainMenu!FullNameLoggedIn = Forms!frmLogin!cboUser.Column(1)
Else
DoCmd.OpenForm "L2MainMenu2"
Forms!L2MainMenu2!FullNameLoggedIn = Forms!frmLogin!cboUser.Column(1)
ElseIf intIncorrectCount > 1 Then
MsgBox "Too many failed login attempts. Click OK to set new password", vbOK vbExclamation
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
'DoCmd.Close acForm, "frmLogin"
Else
MsgBox "Incorrect password", vbOKOnly vbExclamation
Me.Visible = False
intIncorrectCount = 0
Me.txtPassword = Null
Me.txtPassword.SetFocus
intIncorrectCount = intIncorrectCount 1
End If
End If
TempVars("Username") = Me.cboUser.Value
End If
End If
End Sub