Results 1 to 3 of 3

VBA code for login screen

  1. #1
    JoeJr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    44

    VBA code for login screen

    hello all,



    I have created a login screen form. I have two unbound txtfields that is being used to match username and password on a table. It works well up to the point were if the txtfields are blank. If the username is blank and the password is filled in, the first if statment takes over. If the username is filled in and the password is plank nothing happens and the doCmd runs and opens the called form which it is not supposed to do since the password is blank. Below is my code. I can't figure out why its not stoping and promting the user to input a password.

    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub btnLogin_Click()
    Dim rs As Recordset
    
    
    
    
        Set rs = CurrentDb.OpenRecordset("tblUserPass", dbOpenSnapshot, dbReadOnly)
        
        rs.FindFirst "UserName='" & Me.TxtUserName & "'"
    
    
        If rs.NoMatch = True Then
            Me.lblWrongUN.Visible = True
            Me.TxtUserName.SetFocus
            Exit Sub
        End If
            Me.lblWrongUN.Visible = False
        If rs!Password <> Me.TxtPassword Then
            Me.lblWrongPas.Visible = True
            Me.TxtPassword.SetFocus
            Exit Sub
        End If
        Me.lblWrongPas.Visible = False
        DoCmd.OpenForm "AllTrackMainPage"
        DoCmd.Close acForm, Me.Name
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,355
    Have you step debugged? If control is blank it is likely Null. Cannot compare anything to Null - it only returns Null, not True or False. So it doesn't matter if you use = or <>, when comparing to Null condition never evaluates as True and code inside If block never runs.

    Don't process if values not provided. Consider:
    Code:
    Private Sub btnLogin_Click()
    Dim rs As Recordset
    
    Me.lblWrongUN.Visible = False
    Me.lblWrongPas.Visible = False
    
    If Not IsNull(Me.TxtUserName) And Not IsNull(Me.TxtPassword) Then
        Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblUserPass WHERE UserName='" & Me.TxtUserName & "'", dbOpenSnapshot, dbReadOnly)
        If rs.EOF Then
            Me.lblWrongUN.Visible = True
            Me.TxtUserName.SetFocus
        ElseIf rs!Password <> Me.TxtPassword Then
            Me.lblWrongPas.Visible = True
            Me.TxtPassword.SetFocus
        Else
           DoCmd.OpenForm "AllTrackMainPage"
           DoCmd.Close acForm, Me.Name
        End If
    Else
        MsgBox "Must enter " & IIf(IsNull(Me.TxtUserName), "Username", "Password")
    End If
    End Sub
    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
    JoeJr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    44
    Awesome! I got it to work. Thanks for the input.

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

Similar Threads

  1. Login Screen on form
    By stu_C in forum Forms
    Replies: 1
    Last Post: 09-23-2019, 05:19 AM
  2. VBA login screen
    By Batselot in forum Access
    Replies: 3
    Last Post: 10-21-2018, 05:01 AM
  3. LogIn Screen
    By Bongobob21 in forum Forms
    Replies: 2
    Last Post: 05-18-2015, 08:28 AM
  4. Login Screen in Access
    By Di7bash in forum Access
    Replies: 3
    Last Post: 03-18-2014, 11:57 PM
  5. Login screen coding
    By thinkkwise in forum Access
    Replies: 1
    Last Post: 08-05-2013, 05:08 PM

Posting Permissions

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