Results 1 to 12 of 12
  1. #1
    dharmacloud is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2011
    Posts
    21

    Login form code not working

    Can anyone look at the code and help me figure out what's wrong with it? This seems to be a very standard code but somehow it is not working properly. The code is suppose to shutdown the database after >3 incorrect attempts at login. However, it doesn't do that. Also, I was able to close the form and bypass the whole login process. Please help!



    Code:
    Private Sub cmdLogin_Click()
    
    'Check to see if data is entered into the UserName combo box
    
        If IsNull(Me.Combo2) Or Me.Combo2 = "" Then
          MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
            Me.Combo2.SetFocus
            Exit Sub
        End If
    
        'Check to see if data is entered into the password box
    
        If IsNull(Me.Text4) Or Me.Text4 = "" Then
          MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
            Me.Text4.SetFocus
            Exit Sub
        End If
    
        'Check value of password in tblEmployees to see if this
        'matches value chosen in combo box
    
        If Me.Text4.Value = DLookup("Password", "Employee", _
                "[EmployeeID]=" & Me.Combo2.Value) Then
    
            EmployeeID = Me.Combo2.Value
    
            'Close logon form and open splash screen
    
            DoCmd.Close acForm, "LogOn", acSaveNo
            DoCmd.OpenForm "Switchboard"
    
        Else
          MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
                "Invalid Entry!"
            Me.Text4.SetFocus
        End If
    
        'If User Enters incorrect password 3 times database will shutdown
    
            intLogonAttempts = intLogonAttempts + 1
        If intLogonAttempts > 3 Then
          MsgBox "You do not have access to this database.Please contact admin.", _
          vbCritical, "Restricted Access!"
        Application.Quit
        End If
              
    End Sub
    
    Private Sub Combo2_AfterUpdate()
    'After selecting user name set focus to password field
        Me.Text4.SetFocus
    End Sub

  2. #2
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Try putting your logonattempt counter inside the ELSE of the password checker.
    And depending on how you have your Options set, you may have to declare intLogonAttempts before you can use it.

  3. #3
    dharmacloud is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2011
    Posts
    21
    I'm really a newbie and don't know how to do what you had just suggested. Is this what you mean?

    Else
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
    "Invalid Entry!"
    Me.Text4.SetFocus
    'If User Enters incorrect password 3 times database will shutdown

    intLogonAttempts = intLogonAttempts + 1
    If intLogonAttempts > 3 Then
    MsgBox "You do not have access to this database.Please contact admin.", _
    vbCritical, "Restricted Access!"
    Application.Quit
    End If

    End Sub

  4. #4
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Yea and also put a:
    Dim intLogonAttempts as Integer
    at the very beginning. You should get in the habit of declaring variables. It makes life easier.

  5. #5
    dharmacloud is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2011
    Posts
    21
    I got an error: "block if without end if" with this:

    Else
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
    "Invalid Entry!"
    Me.Text4.SetFocus

    Dim intLogonAttempts As Integer

    'If User Enters incorrect password 3 times database will shutdown

    intLogonAttempts = intLogonAttempts + 1
    If intLogonAttempts > 3 Then
    MsgBox "You do not have access to this database.Please contact admin.", _
    vbCritical, "Restricted Access!"
    Application.Quit
    End If

  6. #6
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    You need one last End If at the very end. You have 2 If statements but are only closing one of them.

  7. #7
    dharmacloud is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2011
    Posts
    21
    okay, I put another End If at the very end and no more error message. But it still allows me to have make multiples attempts at login without shutting down the application after 3 attempts.

  8. #8
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Remove the Dim intLogonAttempts As Integer.

    In the OnOpen event of your form enter:
    Private intLogonAttempts As Integer

    I think you're using the code found here. You need to implement the whole thing. I believe what's happening is that intLogonAttempts is just resetting everytime you run the Sub. The variable needs to exist outside the sub so that it can stay. Other than that, you should be able to use the original code you used, assuming it's from the link.

  9. #9
    dharmacloud is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2011
    Posts
    21
    Yes, it is almost exactly the same code except I didn't have the "Private intLogonAttempts As Integer" statement.

    I tried what you had suggested and got this error: Compile error: invalid attribute in Sub or Function.

    Here's what the code that I put in:

    Code:
    Private Sub cmdLogin_Click()
    
    Private intLogonAttempts As Integer
    
    'Check to see if data is entered into the UserName combo box
    
        If IsNull(Me.Combo2) Or Me.Combo2 = "" Then
          MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
            Me.Combo2.SetFocus
            Exit Sub
        End If
    
        'Check to see if data is entered into the password box
    
        If IsNull(Me.Text4) Or Me.Text4 = "" Then
          MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
            Me.Text4.SetFocus
            Exit Sub
        End If
    
        'Check value of password in tblEmployees to see if this
        'matches value chosen in combo box
    
        If Me.Text4.Value = DLookup("Password", "Employee", _
                "[EmployeeID]=" & Me.Combo2.Value) Then
    
            EmployeeID = Me.Combo2.Value
    
            'Close logon form and open splash screen
    
            DoCmd.Close acForm, "LogOn", acSaveNo
            DoCmd.OpenForm "Switchboard"
    
        Else
          MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
                "Invalid Entry!"
            Me.Text4.SetFocus
          
          End If
          
        'If User Enters incorrect password 3 times database will shutdown
     
            intLogonAttempts = intLogonAttempts + 1
        If intLogonAttempts > 3 Then
          MsgBox "You do not have access to this database.Please contact admin.", _
          vbCritical, "Restricted Access!"
        Application.Quit
        End If
        
        
               
    End Sub
              
    
    Private Sub Combo2_AfterUpdate()
    'After selecting user name set focus to password field
        Me.Text4.SetFocus
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
        Me.Combo2.SetFocus
            intLogonAttempts = 0
    End Sub

  10. #10
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    The Private intLogonAttempts As Integer goes into the Form_Open event.

  11. #11
    dharmacloud is offline Novice
    Windows Vista Access 2007
    Join Date
    Aug 2011
    Posts
    21
    Thanks TheShabz for helping me through this! I finally got it to work.

    Here's what I did to make it work:

    Code:
    Option Compare Database 
    Private intLogonAttempts As Integer
    
    Private Sub Form_Open(Cancel As Integer)
        Me.Combo2.SetFocus
            intLogonAttempts = 0
    End Sub
    The rest of the code in between remained the same.

  12. #12
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Glad you got it to work.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Login ID Code for Access 2007 not working
    By amangupts in forum Programming
    Replies: 25
    Last Post: 07-07-2011, 01:28 PM
  2. Login Form Code
    By paddon in forum Programming
    Replies: 4
    Last Post: 04-08-2011, 06:48 PM
  3. Replies: 21
    Last Post: 02-14-2011, 02:51 PM
  4. Login/Password Code not working
    By eww in forum Programming
    Replies: 3
    Last Post: 09-21-2010, 10:49 AM
  5. Simple Nav Form Code Not Working
    By alsoto in forum Forms
    Replies: 10
    Last Post: 04-10-2009, 09:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums