Results 1 to 6 of 6
  1. #1
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215

    Log In Form

    Hi,

    I have a LogIn form that I use for my database and I just noticed a major flaw. Here is my code for what I use currently.
    Code:
    Private Sub LogIn_Click()
       If IsNull(Me.txtUserName) Then
          MsgBox "Please enter a User Name...", 16, "Missing User Name"
          [txtUserName].SetFocus
          Exit Sub
       End If
       If IsNull(Me.txtPassword) Then
          MsgBox "Please enter a Password...", 16, "Missing Password"
          [txtPassword].SetFocus
          Exit Sub
       End If
       If Not StrComp(DLookup("Password", "tblUsers", "UserName = '" & txtUserName & "'"), txtPassword, 0) = 0 Then
          MsgBox "Invalid User Name and/or Password. Try Again.", 16, "Invalid Attempt"
          [txtPassword].SetFocus
       Else
          If txtUserName = "admin" Then
             DoCmd.ShowAllRecords
             DoCmd.Close acForm, "frmLogOn"
          Else
             DoCmd.OpenForm "frmMenu"
             DoCmd.Close acForm, "frmLogOn"
             DoCmd.ShowToolbar "Ribbon", acToolbarYes
          End If
       End If
    End Sub
    Here are a couple situations and the outcome of each.
    1) Nothing entered, works perfectly
    2) Just UserName entered, works perfectly
    3) Just Password, works perfectly
    4) UserName & Password entered correctly, works perfectly


    5) UserName & Password entered, Password is entered incorrectly, works perfectly
    6) UserName & Password entered incorrectly, it still lets me in. This is my problem, and it's a big one and I don't know how I didn't test for that before.

    What do I need to do to fix this? I can't figure it out.

    Thanks.
    Last edited by RuralGuy; 10-11-2012 at 12:19 PM. Reason: Added indenting

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    You could try this:
    Code:
    Private Sub LogIn_Click()
    If IsNull(Me.txtUserName) Then
    MsgBox "Please enter a User Name...", 16, "Missing User Name"
    [txtUserName].SetFocus
    Exit Sub
    End If
    If IsNull(Me.txtPassword) Then
    MsgBox "Please enter a Password...", 16, "Missing Password"
    [txtPassword].SetFocus
    Exit Sub
    End If
    If DLookup("Password", "tblUsers", "UserName = '" & txtUserName & "'") <> txtPassword Then
    MsgBox "Invalid User Name and/or Password. Try Again.", 16, "Invalid Attempt"
    [txtPassword].SetFocus
    Else
    If txtUserName = "admin" Then
    DoCmd.ShowAllRecords
    DoCmd.Close acForm, "frmLogOn"
    Else
    DoCmd.OpenForm "frmMenu"
    DoCmd.Close acForm, "frmLogOn"
    DoCmd.ShowToolbar "Ribbon", acToolbarYes
    End If
    End If
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    Bob,

    I can't do your suggestion. If I make your changes, the string no longer looks to compare the different case sensitivities that the passwords are. I need keep the strComp in there as it is.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Try this code then:
    Notice: No Exit Sub's, just structured code.
    Code:
    Private Sub LogIn_Click()
       If Len(Me.txtUserName & "") <> 0 Then
          '-- There is an entry in the UserName
          If Len(Me.txtPassword & "") <> 0 Then
             '-- There is an entry in *both* UserName and Password
             If StrComp(DLookup("Password", "tblUsers", "UserName = '" & txtUserName & "'"), txtPassword, 0) = 0 Then
                '-- Password for this UserName is valid
                If txtUserName = "admin" Then
                   DoCmd.ShowAllRecords
                   DoCmd.Close acForm, Me.Name
                Else
                   DoCmd.OpenForm "frmMenu"
                   DoCmd.ShowToolbar "Ribbon", acToolbarYes
                   DoCmd.Close acForm, Me.Name
                End If
             Else
                MsgBox "Invalid User Name and/or Password. Try Again.", 16, "Invalid Attempt"
                Me.txtUserName.SetFocus
             End If
          Else
             MsgBox "Please enter a Password...", 16, "Missing Password"
             Me.txtPassword.SetFocus
          End If
       Else
          MsgBox "Please enter a User Name...", 16, "Missing User Name"
          Me.txtUserName.SetFocus
       End If
    End Sub

  5. #5
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    Thanks RuralGuy, that works.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Great! Glad we could help.

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

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