Declaring a variable outside a procedure with the Private
access modifier allows it to be used throughout the module/form:
'// this variable is visible to all code in the UserForm:
Private mCounter As Long
'// keep the magic number easily modifiable
Private Const MAX_LOGIN_ATTEMPTS As Long = 3
Private Sub CommandButton1_Click()
// your validation logic
ok = false
If Not ok Then '// test
mCounter = mCounter + 1
If mCounter < MAX_LOGIN_ATTEMPTS Then
MsgBox "Access denied, You have " & (MAX_LOGIN_ATTEMPTS - mCounter) & " attempts left until the system closes"
Else
MsgBox "bye"
'//...
End If
Else
MsgBox "access granted"
'//...
End If
End Sub