Password Validation loop

Splatgore

New Member
Joined
Mar 25, 2011
Messages
2
Hi there,

I've got a minor problem and it is doing my head in. I've spent several hours searching the web for an answer but I've come up dry.

What I am trying to do is create a form which asks for a password. If the password is entered incorrectly then the user gets 3 more tries after which a torrent of abuse is hurled at them. Or something.

Now, the userform and password check is easy peasy but the thing that is frustrating the CRAP out of me is getting the loop to work correctly.

Code:
Private Sub confirm_Click()
 
Dim Response As String
Dim Check As String
Dim Tries As Long
 
Check = "blah"
i = 0
 
StartLoop:
 
Do While i < 3
 
If Response <> Check Then
 
    MsgBox "Incorrect Password! Security are on their way!"
    i = i + 1
    GoTo StartLoop
 
Else
 
    MsgBox "Hmm...lucky guess! I guess you can carry on...!"
    Call Do_Stuff
 
End If
 
Loop
 
End Sub

The problem I am having is that the message box won't disappear and let the user have another go at entering the password and I can't figure out why. I've tried password.setfocus (it is a textbox control) and userform.hide / userform.show but nothing seems to work correctly.

Any thoughts on what I am doing wrong? It isn't vital but it is very bloody irritating not to be able to get it to work. :(

Kind regards,

Splatgore
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
ive just modded your code, but its prolly not the best structure to achieve the outcome, but it should work.

Code:
Private Sub confirm_Click()
 
    Dim Response As String
    Dim Check As String
    Dim Tries As Long
 
    Check = Textbox1.value               ' "blah"
    Tries= 0   ' not needed cos it is reset on Sub startup

    Do While Tries < 3
        If Response <> Check Then
            MsgBox "Incorrect Password! Security are on their way!"
            Tries = Tries + 1
        Else
            MsgBox "Hmm...lucky guess! I guess you can carry on...!"
            Tries =6
            Do_Stuff    ' no need for Call statement
        End If
    Loop
    if tries = 4 then
        msgbox("You have exceded the number of tries")
    end if
End Sub
 
Upvote 0
Thanks for your reply but sadly no dice. :(

I just read through your code and realised I'd missed bits out from when I was copying it into the forum post box. See below with your bits thrown in for good measure...

Code:
Private Sub confirm_Click()
Dim Response As String
Dim Check As String
Dim Tries As Long
 
Check = "blah"
Response = Password.Text
 
Do While Tries < 3
 
If Response <> Check Then
 
    MsgBox "Incorrect Password! Security are on their way!"
    Tries = Tries + 1
 
Else
 
    MsgBox "Hmm...lucky guess! I guess you can carry on...!"
    Tries = 6
 
    Do_Stuff
 
    MsgBox "New data successfully posted!"
 
End If
 
Loop
 
If Tries = 4 Then
 
    MsgBox "bye"
 
End If
 
End Sub

I gave this a try and it still doesn't work and won't let the user go back to enter a new password. Grr. I don't see why it shouldn't work though because it seems pretty logical to me...

If the user enters a wrong password then alert them to the fact, increase the counter by 1 and let them try again. Repeat until the counter hits 3 at which point abuse them and then exit the subroutine.

Any other suggestions?

Thanks for the tip re. the counter and 'call' statement though. I always thought you had to Call another sub but I guess not. :)
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top