Results 1 to 13 of 13
  1. #1
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105

    error in code

    I used code from a video by Steve Bishop to create a login screen for a database.
    https://www.youtube.com/watch?v=356ylQn6kkA
    I put a copy of the code below. When I click the login button I get the following message.
    Compile error:
    Variable not defined and it highlights the following code dbOpenSnapshot

    Can someone please tell me what is wrong. Thanks

    CODE:
    Private Sub BtnLogin_Click()
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
    rs.FindFirst "UserName='" & Me.txtUserName & "'"
    If rs.NoMatch = True Then
    Me.lblWrongUser.Visible = True
    Me.txtUserName.SetFocus
    Exit Sub
    End If
    Me.lblWrongUser.Visible = False
    If rs!Password <> Me.txtPassword Then
    Me.lblWrongPass.Visible = True
    Me.txtPassword.SetFocus
    Exit Sub
    End If
    Me.lblWrongPass.Visible = False
    DoCmd.OpenForm "frmDialog"


    DoCmd.Close acForm, Me.Name
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105
    Same result. Also I try using
    wrong usernames and passwords and it does not give me the error message

    Thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    The code uses late binding to open a DAO recordset object. Don't understand why that is failing. I just tested the original code in Access 2010 and it works. However, I don't have Access 2007 to test with.

    Consider not even using recordset object:

    Code:
    Private Sub BtnLogin_Click()
        If IsNull(DLookup("UserName", "tbl1Employees", "UserName='" & Me.txtUserName & "'")) Then
            Me.lblWrongUser.Visible = True
            Me.txtUserName.SetFocus
            Exit Sub
        End If
        Me.lblWrongUser.Visible = False
        If Nz(DLookup("Password", "tbl1Employees", "UserName='" & Me.txtUserName & "'"),"") <> Me.txtPassword Then
            Me.lblWrongPass.Visible = True
            Me.txtPassword.SetFocus
            Exit Sub
        End If
        Me.lblWrongPass.Visible = False
        DoCmd.OpenForm "frmDialog"
        DoCmd.Close acForm, Me.Name
    End Sub
    Be aware Access is not case sensitive by default. So ABC123$ equals abc123$.

    Also, please post lengthy code within CODE tags to retain indentation and readability. Use # button on the editor toolbar.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105
    Thanks very much .

    I tried it putting in correct username and correct password and was able to login.

    I tried putting the wrong username and it prompted wrong username.
    I corrected the username and put in a wrong password and it did not prompt me but logged me in.
    What I notice, it logs me in with a correct username even if I don't put in a password.

    I am not familiar with codes. I would appreciate your further assistance.

    Regards

  6. #6
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105
    Thanks very much .

    I tried it putting in correct username and correct password and was able to login.

    I tried putting the wrong username and it prompted wrong username.
    I corrected the username and put in a wrong password and it did not prompt me but logged me in.
    What I notice, it logs me in with a correct username even if I don't put in a password.

    I am not familiar with codes. I would appreciate your further assistance.

    Regards

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    You may have read my post before I edited. Review again.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105
    It logs me in with username and password correct. correct
    It gives me message for wrong password, but still logs me in with just a correct username when no password is entered.

  9. #9
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,706
    Code:
    Private Sub BtnLogin_Click()
    	If len(DLookup("UserName", "tbl1Employees", "UserName='" & Me.txtUserName & "'")) & vbnullstring) = 0 Then
    	        Me.lblWrongUser.Visible = True
    	        Me.txtUserName.SetFocus
    	        Exit Sub
        	End If
        	Me.lblWrongUser.Visible = False
    	If len(me.txtPassword & vbnullstring) = 0 then
           		Me.lblWrongPass.Visible = True
            	Me.txtPassword.SetFocus
            	Exit Sub
        	If Nz(DLookup("Password", "tbl1Employees", "UserName='" & Me.txtUserName & "'"),"") <> Me.txtPassword Then
            	Me.lblWrongPass.Visible = True
            	Me.txtPassword.SetFocus
            	Exit Sub
        	End If
        	Me.lblWrongPass.Visible = False
        	DoCmd.OpenForm "frmDialog"
        	DoCmd.Close acForm, Me.Name
    End Sub
    The first line of red code prevents a blank username from working.
    The blue code allows a blank password to work. The red code added above it fixes that.

  10. #10
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105
    Thanks again. I used the new codes. I got this message Compile error: Syntax error with the following code highlighted.

    If len(DLookup("UserName", "tbl1Employees", "UserName='" & Me.txtUserName & "'")) & vbnullstring) = 0 Then

  11. #11
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,706
    Quote Originally Posted by chavez_sea View Post
    Thanks again. I used the new codes. I got this message Compile error: Syntax error with the following code highlighted.

    If len(DLookup("UserName", "tbl1Employees", "UserName='" & Me.txtUserName & "'")) & vbnullstring) = 0 Then
    Ahh, remove the red closing parens...

  12. #12
    chavez_sea is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2011
    Posts
    105
    Thanks very much. It gave another error about "End If" so I added an End If before
    If Nz(DLookup("Password",

    and it worked perfectly. Thanks so much for your patience and assistance.

  13. #13
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,706
    Quote Originally Posted by chavez_sea View Post
    Thanks very much. It gave another error about "End If" so I added an End If before
    If Nz(DLookup("Password",

    and it worked perfectly. Thanks so much for your patience and assistance.
    Good catch. Good luck with your project.

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

Similar Threads

  1. Replies: 0
    Last Post: 10-10-2018, 12:28 PM
  2. Replies: 3
    Last Post: 05-10-2018, 07:48 AM
  3. Error code to prevent error messages
    By sanal in forum Programming
    Replies: 11
    Last Post: 05-09-2018, 11:29 AM
  4. Error in code vba
    By azhar2006 in forum Forms
    Replies: 5
    Last Post: 12-12-2017, 02:02 PM
  5. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 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