Results 1 to 4 of 4
  1. #1
    Jamesy_007 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Location
    Caribbean
    Posts
    109

    Login Form that displays users FirstName in a message box

    Hey Guys how’s everyone been

    Need some guidance, please. I have this login form with three assigned users each have their own password. I want to have a feature where someone logs in and a message box pops up saying Welcome with the First Name of whoever is logged in. It does want I want but it keeps displaying only the first user name to primary key ID:1 and for the other users also. What am I evaluating wrong here, Any suggestions please?.



    This is my code below am trying to figure out my error, should I create a private function of UserName text box i.e. txtUserName?

    Code:
    Private Sub btnLogin_Click()      
         Dim rs As Recordset
          Dim userx As String, user As String
           userx = "WELCOME"
           user = DLookup("FirstName", "tblUsers", Me.txtUserName)
        Set rs = CurrentDb.OpenRecordset("tblUsers", 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 = True Then
      Me.lblWrongPass.Visible = True
      Me.txtPassword.SetFocus
          Exit Sub
          End If
        If IsNull(Me.txtPassword) Then
      MsgBox "Please Enter Valid Password!", vbInformation, "Database Management System"
           Me.lblWrongPass.Visible = True
          Me.txtPassword.SetFocus
         Exit Sub
        End If
        If rs!Password = Me.txtPassword = True Then
       Me.lblWrongPass.Visible = False
       Me.lblWrongUser.Visible = False
    DoCmd.OpenForm "frmMAIN"
        MsgBox userx & ", " & user, , " Database Management System"
      
    DoCmd.Close acForm, Me.Name
    End If
    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,772
    Have you attempted any debugging?

    Set a breakpoint and step debug - follow the code as it executes 1 line at a time. Are variables populated as expected? Does code flow as expected?

    For a start, the DLookup() syntax is incomplete.

    Second, user is declared as a string. If DLookup() does not find a match, it returns Null. String variable cannot hold Null so triggers a run-time error.

    user = Nz(DLookup("FirstName", "tblUsers", "UserName='" & Me.txtUserName & "'"), "New Person")

    If user has to login, how could code know them anyway? Need to first validate username/password login then lookup first name.
    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
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,575
    You need to compare something against Me.txtUserName ?

    Perhaps "UserName = '" &
    Me.txtUserName & "'"

    When you do not know how something works, look it up.

    https://support.microsoft.com/en-us/...b-bed10dca5937
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Jamesy_007 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Location
    Caribbean
    Posts
    109
    Thank you this is the line of code. Strange its the same line of code am asking the Recordset to look up a few lines down.. At first I put that line in but was getting a syntax mismatch error and there it goes you keep altering the code after that, because something didn't work I you thought it should have. This is where you guys come in a fresh pair of eyes to look at something. Thanks Welshgasman and June 7!

    Quote Originally Posted by Welshgasman View Post
    You need to compare something against Me.txtUserName ?

    Perhaps "UserName = '" &
    Me.txtUserName & "'"


    Quote Originally Posted by Welshgasman View Post
    When you do not know how something works, look it up.

    I do, usually when am here it's because something just can't crack and I don't know why. I just love you'll and Access databases...Ahh... the feeling of building things

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

Similar Threads

  1. Multi users login form
    By johnwatkins35 in forum Forms
    Replies: 3
    Last Post: 05-15-2012, 11:39 PM
  2. Replies: 10
    Last Post: 03-02-2012, 11:06 AM
  3. Replies: 6
    Last Post: 12-12-2011, 01:28 PM
  4. Replies: 1
    Last Post: 12-11-2011, 11:48 AM
  5. Replies: 1
    Last Post: 01-04-2008, 11:40 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