-
March 6th, 2002, 09:55 AM
#1
Loops
Hi there,
I am quite new to VB and I am trying to write a loop in my program that allows you to have five attempts in getting the password correct, after which it will exit you.
Can someone please show me how this can be done using:
1) Do-While loop.
2) For loop.
3) Any other method
Any help will be appreciated.
Thank you in advance.
I have included below the code that I have been trying to use, but it does not work.
Private Sub CmdValid_Click()
'This procedure checks the input password
Dim Response As Integer
Do
If txtPassword.Text = txtPassword.Tag Then
'If correct, display message box
MsgBox "You've passed security!", vbOKOnly + vbExclamation, "Access Granted"
Else
'If incorrect, give option to try again
Response = MsgBox("Incorrect password", vbRetryCancel + vbCritical, "Access Denied")
End If
If Response = vbRetry Then
txtPassword.SelStart = 0
txtPassword.SelLength = Len(txtPassword.Text)
Response = Response + 1
lblCounter.Caption = Response
Print Response
Else
End
End If
txtPassword.SetFocus
Loop While Response < 5
End Sub
-
March 6th, 2002, 10:38 AM
#2
Re: Loops
declare a long value at form level, in Form_Load event set this long = 1 and increment it at every failed login. Once incremented to 5 just msgbox and exit.
Andy
Don't forget to Rate!!!!
'Dim numTries as long
private Sub Form_Load()
numTries = 1
End Sub
private Sub CmdValid_Click()
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
If numTries = 5 then
MsgBox "You have Exceeded to amount of attempts allocated", vbOKOnly, App.Title
Exit Sub
End If
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'This procedure checks the input password
Dim Response as Integer
If txtPassword.Text = txtPassword.Tag then
'If correct, display message box
MsgBox "You've passed security!", vbOKOnly + vbExclamation, "Access Granted"
else
'If incorrect, give option to try again
Response = MsgBox("Incorrect password", vbRetryCancel + vbCritical, "Access Denied")
End If
If Response = vbRetry then
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
numTries = numTries + 1
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
txtPassword.SelStart = 0
txtPassword.SelLength = len(txtPassword.Text)
Response = Response + 1
lblCounter.Caption = Response
print Response
else
End
End If
txtPassword.SetFocus
End Sub
Andrew Lennon (Berlitz)
Automation Dev Engineer
-
March 6th, 2002, 10:47 AM
#3
Re: Loops
Here is your code slightly modified
option Explicit
private Sub CmdValid_Click()
'This procedure checks the input password
static Response as Integer ' MODIFIED
If txtPassword.Text = txtPassword.Tag then
'If correct, display message box
MsgBox "You've passed security!", vbOKOnly + vbExclamation, "Access Granted"
else 'NOT TXTPASSWORD.TEXT...
'If incorrect, give option to try again
MsgBox "Incorrect password", vbOK + vbCritical, "Access Denied" ' MODIFIED
txtPassword.SelStart = 0
txtPassword.SelLength = len(txtPassword.Text)
Response = Response + 1
lblCounter.Caption = Response
txtPassword.SetFocus
End If
If Response > 5 then Unload me
End Sub
private Sub Form_Load()
txtPassword.Tag = "Hello"
End Sub
Please rate it if it answers the question
or is useful.
'
John G
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|