Access Password check code

mdgibbs88

Registered User.
Local time
Today, 18:22
Joined
Oct 27, 2009
Messages
31
Hi everyone,

I am really stumped here and I am not sure how to make this work.

If my password is correct, it works fine. If the password is incorrect, it shows the MsgBox and then cycles through the loop until the count is 4 and exits.

I can not set it to allow me to re-enter another password.

Thanks in advance for looking!

Here is my code:

Dim Var1 As Integer

If Me.txtPassword.Value <> DLookup("Password", "Analysts", _
"[Employee_ID]=" & Me.cboEmployee.Value) Then
Do While Var1 < 4
MsgBox "Password Invalid. Please Try Again", vbExclamation, _
"Invalid Entry!"
Var1 = Var1 + 1
MsgBox "Password Attempts = " & Var1, vbInformation
If Var1 <= 2 Then
Me.txtPassword.SetFocus
Else
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
Loop
Else
lngMyEmpID = Me.cboEmployee.Value
DoCmd.Close acForm, "frmLogon", acSaveNo
DoCmd.OpenForm "frmMenu"
End If
 
Should this line be higher

If Var1 <= 2 Then 'Should it be 4 not 2
 
You need an Exit Loop where you want it to exit the loop, otherwise it will repeat until it's While condition is no longer true.

Also, as it will be exiting the VBA it will forget the contents of Var1, so I'd send it to a hidden textbox control on the password form if I were you, have it default to 0 and the VBA can add 1 each time the password is incorrect.
 
Should this line be higher

If Var1 <= 2 Then 'Should it be 4 not 2

I tried your suggestion and when I did that I never reached a point where the max attempts message showed.

The real problem that I have here is not the logic of attempts. It is more that I never have an opportunity to refocus on the password field to enter another password. The loop cycles through the invalid password messages without giving me a chance to put in a new password.
 
I tried your suggestion and when I did that I never reached a point where the max attempts message showed.

The real problem that I have here is not the logic of attempts. It is more that I never have an opportunity to refocus on the password field to enter another password. The loop cycles through the invalid password messages without giving me a chance to put in a new password.

As above, that's because at no point does the code state that an incorrect password means the code should strop executing, so it continues, reaches the Loop command, increases Var1, reaches the loop command, increases Var1, etc, etc.
 
The exit IF in the loop was the answer! I added the invisible box on the form as you suggested and updated that variable as it went through.

Thanks so much!
 

Users who are viewing this thread

Back
Top Bottom