By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,456 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,456 IT Pros & Developers. It's quick & easy.

Private Sub cmdEnter_Click() On Error Resume Next Dim Attempts As Integer Dim UserTyp

P: 6
3 times attempt is not working, please someone can help me

Private Sub cmdEnter_Click()
On Error Resume Next
Dim Attempts As Integer
Dim UserType As Variant

If IsNull(Me.cmbUFName) Or Me.cmbUFName = "" Then
MsgBox "Select Your Name First.", vbOKOnly, "Required Data"
Me.cmbUFName.SetFocus
Exit Sub
End If
If IsNull(Me.txtUserPass) Or Me.txtUserPass = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtUserPass.SetFocus
Exit Sub
End If
If Me.txtUserPass = DLookup("UserPass", "tblUser", _
"[UserId]=" & Me.cmbUFName.Value) Then
UserId = Me.cmbUFName.Value
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "frmMain"
Else
MsgBox "Password is Invalid Please Check your Character and Try Again", vbOKOnly, _
"Invalid Entry!"
Attempts = Attempts + 1
Me.txtUserPass.SetFocus
If Attempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End If
End Sub
Dec 27 '16 #1

✓ answered by ADezii

  1. I only had time for a quick glance but Attempts is a Local Variable (Local within the Click() Event of the Command Button) and will subsequently have no meaning once you Exit the Click() Event of the Command Button. Attempting to increment it is useless since it will be reinitialized each time and have an Initial Value of 0. The only way to accomplish what you are requesting is to either make Attempts a Global variable in a Standard Code Module or Declare it as Static within the Click Event of the Command Button.
    Expand|Select|Wrap|Line Numbers
    1. 'Global Variable in a Standard Code Module
    2. Public Attempts as Integer
    Expand|Select|Wrap|Line Numbers
    1. 'Declare as Static in the Click() Event of a Command Button
    2. Static Attempts As Integer
  2. Either Method will enable Attempts to retain its Value between successive Clicks.

Share this Question
Share on Google+
6 Replies


PhilOfWalton
Expert 100+
P: 1,430
What is the value of cmbUFName?

In the first line of your code it would appear to be returning text, in the line
Expand|Select|Wrap|Line Numbers
  1. DLookup("UserPass", "tblUser", _
  2. "[UserId]=" & Me.cmbUFName.Value)
  3.  
it would appear that you are expecting a Long Integer (UserID)

You don't state where your code is failing which doesn't help.

Phil
Dec 27 '16 #2

ADezii
Expert 5K+
P: 8,597
  1. I only had time for a quick glance but Attempts is a Local Variable (Local within the Click() Event of the Command Button) and will subsequently have no meaning once you Exit the Click() Event of the Command Button. Attempting to increment it is useless since it will be reinitialized each time and have an Initial Value of 0. The only way to accomplish what you are requesting is to either make Attempts a Global variable in a Standard Code Module or Declare it as Static within the Click Event of the Command Button.
    Expand|Select|Wrap|Line Numbers
    1. 'Global Variable in a Standard Code Module
    2. Public Attempts as Integer
    Expand|Select|Wrap|Line Numbers
    1. 'Declare as Static in the Click() Event of a Command Button
    2. Static Attempts As Integer
  2. Either Method will enable Attempts to retain its Value between successive Clicks.
Dec 27 '16 #3

P: 6
cmbUFname is list of users registered in tblUser, its only use to select user account with dlookup codes.

thanks
Dec 28 '16 #4

P: 6
Thanks, static Attemps As Integer is working. I replace the Dim into Static.

thank you so much
Dec 28 '16 #5

P: 6
Thanks, static Attemps As Integer is working. I replace the Dim into Static.

thank you so much
Dec 28 '16 #6

P: 6
Private Sub cmdEnter_Click()
On Error Resume Next
Static Attempts As Integer
Dim UValue As Variant
Dim UType As Variant

If IsNull(Me.cmbUFName) Or Me.cmbUFName = "" Then
MsgBox "Select Your Name First.", vbOKOnly, "Required Data"
Me.cmbUFName.SetFocus
Exit Sub
End If
If IsNull(Me.txtUserPass) Or Me.txtUserPass = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtUserPass.SetFocus
Exit Sub
End If
If Me.txtUserPass = DLookup("UserPass", "tblUser", _
"[UserId]=" & Me.cmbUFName.Value) Then
UserId = Me.cmbUFName.Value
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "frmMain", acNormal, , , , _
, UType
Else
UType = Me.txtUType.Value
MsgBox "Password is Invalid Please Check your Character and Try Again", vbOKOnly, _
"Invalid Entry!"
Attempts = Attempts + 1
Me.txtUserPass.SetFocus
If Attempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End If
End Sub
Dec 28 '16 #7

Post your reply

Sign in to post your reply or Sign up for a free account.