Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do Loops 1

Status
Not open for further replies.

Mattjai

Technical User
Jan 3, 2001
2
CA
I doing some VB practice projects and one of them is to design a simple Login form with a Validate button, to see if password is correct, and if so exit sub and if password is not correct let the user know they only have 3 tries left.
Here's where iam having trouble, I get the MsgBox to display when the wrong password was entered to tell them they have 3 tries left but it won't allow me to re-enter a password. I have to click the Retry button all the while that it counts down to o tries left then I can re-enter a password.
My question is, once the first MsgBox is displayed telling them they have 3 tries left and the Retry button is click, how do I get to re-enter the password attempt after the first attempt.
Heres some of my code:
**********************************************
Private Sub cmdExit_Click()
End
End Sub
*************************************************
Private Sub cmdValid_Click()
Dim Response As Integer
Dim attempts As Long
attempts = 3
Do While attempts > 0
If txtPassword.Text = txtPassword.Tag Then
MsgBox "You've Passed security !", vbOKOnly + vbExclamation, "Access Granted"
Call cmdExit_Click
Exit Do
Else
Response = MsgBox("Incorrect Password You Have " & attempts & " Tries left ", vbRetryCancel + vbCritical, "access Denied")
If Response = vbRetry And attempts > 0 Then
attempts = attempts - 1
txtPassword.SetFocus
txtPassword.Text = ""
txtPassword.SelStart = 0
Else
End
End If
End If
Loop
End Sub
**************************************
Private Sub Form_Activate()
txtPassword.SetFocus
End Sub
**********************************
PS: I hope I have explained myself clearly and thank you in advance if you can show me the correct way to do this."
 
Code:
Private mlAttempts As Long

Private Sub Form_Activate()
    'give the user 3 attempts
    mlAttempts = 3
    
    txtPassword.SetFocus
End Sub

Private Sub cmdExit_Click()
    End
End Sub

Private Sub cmdValid_Click()
    Dim Response As Integer
    
    'decrement the Attemtps counter
    mlAttempts = mlAttempts - 1
    
    If txtPassword.Text = txtPassword.Tag Then
        'success
        MsgBox "You've Passed security !", vbOKOnly + vbExclamation, "Access Granted"
        Call cmdExit_Click
    Else
        If mlAttempts > 0 Then
            'failed... give another chance
            MsgBox "Incorrect Password You Have " & mlAttempts & " Tries left ", _
                              vbCritical, _
                              "access Denied"
            txtPassword.SetFocus
            txtPassword.Text = ""
            txtPassword.SelStart = 0
        Else
            'failed... no more chances!
            MsgBox "You are a complete failure !", vbOKOnly + vbCritical, "Access Denied"
            Call cmdExit_Click
        End If
    
    End If
End Sub
 
The problem is because the Do...Loop is in the cmdValid_Click event handler, so you never exit the handler to try again until the loop has finished processing.

Make the "attempts" variable global for the form (i.e., put the Dim in the General-Declarations section at the top of the code and remove the Dim and the "attempts=3" from cmdValid_Click) and set it to 3 in the Form_Load or Form_Initialize event. Then, get rid of the Do...Loop (keep the code from inside the loop, just get rid of the loop itself) and try it again.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Sometimes it is faster to write the code than the words to describe it.
 
You don't want to set attempts = 3 in the Form_Activate event, because that will reset the variable to 3 if the form loses focus and then regains focus...poor security.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
yeah, the load or the init is where it should be.
 
Might be a good place to use a static variable.

Private Sub cmdValid_Click()

Static Attempts As Long
Dim Response As Integer

If txtPassword.Text = txtPassword.Tag Then
'success
MsgBox "You've Passed security !", vbOKOnly + vbExclamation, "Access Granted"
Call cmdExit_Click
Else
If Attempts < 3 Then
'failed... give another chance
MsgBox "Incorrect Password You Have " & 3 - Attempts & " Tries left ", _
vbCritical, _
"access Denied"
txtPassword.SetFocus
txtPassword.Text = ""
txtPassword.SelStart = 0
Else
'failed... no more chances!
MsgBox "You are a complete failure !", vbOKOnly + vbCritical, "Access Denied"
Call cmdExit_Click
End If

End If

Attempts = Attempts + 1
End Sub
 
YES, gmmastros, this is what I have used in the past and it has worked nicely for me in this very same arrangement.

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top