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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increment Counter

Status
Not open for further replies.

PATRON58

Technical User
Jun 8, 2007
16
GB
Hello all,

Am having a little trouble. I am not particuarly technically minded and so I am looking for a little help as despite trying a number of things, nothing seems to help. I am trying to use a counter in a password access form so that after a certain quantity of tries, the application automatically closes. I was wondering if anyone would know what I;m doing wrong:
Private Sub CommandButton1_Click()
Dim Count As Integer
Count = 0

If txtPD = "Shutter" And txtUN = "Shutter" Then
UserForm3.Show
UserFormPassword.Hide

Else: Count = Count + 1
txtPD = ""
txtUN = ""

End If
'End Sub

If Count = 3 Then
UserFormPassword.Hide
End If
End Sub

Any help would be very much appreciated!

Thank you,

Patron58
 
Perhaps this ?
Private Sub CommandButton1_Click()
Static Count As Integer
Count = Count + 1
If txtPD = "Shutter" And txtUN = "Shutter" Then
UserForm3.Show
UserFormPassword.Hide
ElseIf Count = 3 Then
UserFormPassword.Hide
Application.Quit
Else
txtPD = ""
txtUN = ""
End If
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The key being to dimension the count as a static variable.

When you dimensioned the count within the button's click event procedure the variable disappears when the procedure ends.

See life of variables in the VBA help.

Also, you should unload the form, not just .hide it.
 
Hello PHV and mintjulep,

Thank you both very much, that did the trick brilliantly.
Got the integer part but not to tell it to temporarily store the count value so it could work on it in the future.

Never mind, almost there but not quite. Will keep in mind the unload aspect as well! That is a very good point!

Thank you both very much for such a speedy response and for taking the time to help me!

Patron58
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top