I am designing a login page in visual basics 6.0. I have to include a counter such that after three unsuccessful attempts the page will close itself. I have written following code in command button. Although there is no errors but the counter is not working. Here is the code:
Private Sub Command1_Click()
Dim count As Integer
count = 0
If Form1.Text1 = "admin" And Form1.Text2 = "admin" Then
MsgBox "Login Succesfull", vbOKOnly + vbInformation, "Welcome"
Else
count = count + 1
If count = 3 Then
End
Else
MsgBox "Login Unsuccesfull", vbOKOnly + vbCritical, "Try Again"
Form1.Text1 = ""
Form1.Text2 = ""
Form1.Text1.SetFocus
End If
End If
End Sub
Just change Dim to Static so it keeps its value across calls. And also get rid of the line that sets it to zero:
Private Sub Command1_Click()
Static count As Integer
If Form1.Text1 = "admin" And Form1.Text2 = "admin" Then
MsgBox "Login Succesfull", vbOKOnly + vbInformation, "Welcome"
Else
count = count + 1
If count = 3 Then
End
Else
MsgBox "Login Unsuccesfull", vbOKOnly + vbCritical, "Try Again"
Form1.Text1 = ""
Form1.Text2 = ""
Form1.Text1.SetFocus
End If
End If
End Sub
You set your count to 0 each time that Command1 is clicked. You need to declare count
outside of that sub & initialise it to 0 elsewhere.
Private count As Integer
Private Sub Command1_Click()
If Form1.Text1 = "admin" And Form1.Text2 = "admin" Then
MsgBox "Login Succesfull", vbOKOnly + vbInformation, "Welcome"
Else
count = count + 1
If count = 3 Then
End
Else
MsgBox "Login Unsuccesfull", vbOKOnly + vbCritical, "Try Again"
Form1.Text1 = ""
Form1.Text2 = ""
Form1.Text1.SetFocus
End If
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.