The part of three failed attempts is not working. Please help with this. Thank you!
Private Sub cmd_ok_Click() Dim attempt As Integer Dim valid As Boolean attempt = 0 valid = False Do attempt = attempt + 1 If Me.cbo_User.Column(2) = Me.txt_Password Then valid = True MsgBox "Password accepted" DoCmd.OpenForm "Student", acNormal DoCmd.Close acForm, "Login" Else valid = False MsgBox "Wrong password entered." & _ vbCrLf & "Please re-enter password.", _ vbExclamation, "Invalid Password" Me.txt_Password.SetFocus 'places the cursor in password control Exit Sub End If Loop Until (valid = True) Or (attempt >= 3) If attempt >= 3 Then MsgBox "You do not have access to this database. Please contact your system administrator.", vbCritical, "Restricted Access!" Exit Sub End If End Sub
Without testing, I think its this line:
Try removing the paranetheses. If not, then please explain what the problem is exactly?Loop Until (valid = True) Or (attempt >= 3)
abousetta
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
The problem is that after the 3rd attempt, the message box" "You do not have access to this database. Please contact your system administrator." does not come up.
This might sound like a dumb question, but does it come up after the fourth attempt or does it never come up at all.
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
Try changing the way the do... loop works:
Do While valid = False And attempt < 3abousettaLoop
P.S. It would help if you could upload a dummy workbook to test on.
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
I believe you need to remove the Exit Sub from the Else part of the code. It is defeating the purpose of the loop.
Good luck.
I'm going to agree with OnError. Exit sub in your else block is causing you to exit and when you come back into the sub, so your attempt variable is reset to 0. Which kind of makes me think that you need to declare the attempt variable as static. You would also need to initialize that variable on the form load event to 0.
My suggestion,
I'm really leaning at you have a variable scope issue. Each time the user clicks go, the attempts are reset to 0. With static, the number should be saved until the form is closed and is re-initialized.Static attempt as integer Dim valid as Boolean If attempt >= 3 Then 'messagebox and events for no access cmd_ok.enabled = false 'This will cause them to have to reopen the form to submit again. else 'check the password if password = me.txtPassword Then valid = true else me.txtPassword.value = "" 'messagebox and events for this attempt = attempt + 1 End If End If
If you do not exit the sub, there should be no issue with the counter being reset. If you use a static variable, then having got the password wrong three times, you would only get one attempt the next time. Equally, having used the code correctly three times, you would only have one attempt next time - unless you restart the application.
Good luck.
I was assuming that the user was clicking the button again to enter the password that would re-fire the event. Just setting the focus to the textbox wont do anything but point the user to the wrong input. They still have to resubmit the new information somehow. Having it inside a loop esentially doesn't do anything. I can only assume that without exit sub it would hit the 3 attempts quickly because there isn't anything stopping execution when the event fires, while the user enters new data. Unless .setFocus does something I am not aware of for validation. If .setFocus does halt execution then yes. Getting rid of exit sub will work.
To fix the attempts you could reset the static variable to =0 after a sucessful login then you always start fresh after a good login.
I think you are correct - at least that is how it ought to be working. I confess I did not really examine what the original loop was doing - I had input boxes in my mind for some reason. So yes I think you would need something like
if password = me.txtPassword Then valid = true attempt = 0
Good luck.
Maybe this will helpPASSWORD FORM6.xls
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks