Page 1 of 4 1234 LastLast
Results 1 to 15 of 48
  1. #1
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272

    Display User First name on main form when login with username

    I have a database with table name tbl_login.

    Fields in the table include: FirstName, LastName, UserName & Password.



    Am able to login to open my main form successfully.

    Now my issue is, I will like to let the users First name be displayed when logged in.

    Eg. if Samson is the FirstName and Sam is the UserName, when I log in with Sam, Samson should be displayed on my main form as the current active user.

    Any help with this will be greatly appreciated.

    Thank you

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,946
    First save the user name as a public variable e.g. strUserName... or as a tempvar.
    Now reference this using a DLookup function. The control source of your textbox will be something like
    Code:
    =DLookup("FirstName", "tbl_Users", "UserName = '" & strUserName & "'")
    Or if you want to do this on multiple forms, create a function GetUserName in a standard module using the same DLookup expression and use =GetUserName() as your textbox control source
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    So if I understand you
    I will create a textbox on the form
    Then I will right-click on the textbox to go to properties
    The I will input this code in the control source right?

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,946
    Correct.
    Or you can use the form load event to assign the expression to the unbound textbox as you originally suggested (that was in the notification email I received)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Alrite
    Please I will give it a try and get back to you

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,819
    I got all the info I might need multiple times at login, and placed it into TempVars, then referred to them whenever needed.?
    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

  7. #7
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Please I entered the code into the control source as you instructed
    But I get this “#Name?” displayed in the textbox when I log in

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,702
    Did you use the names of your table and field or just paste in what was given to you? You were given a sample with tbl_Users but you said your table name is tbl_Login.

    You should always post what you tried - using code tags (# on posting toolbar)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    =DLookup("FirstName", "tbl_login", "UserName = '" & strUserName & "'")

    This was what I put in the control source.
    Or will I have to change the name of the textbox too?

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,100
    Did you declare strUserName as a public variable (in a standard module) and assign its value in your login form (after successfully validating the user name and password)?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    How do I do that please?

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,946
    Apologies for using the wrong table name.
    I don't understand your comment about renaming the textbox so a few questions...

    1. Can you confirm that the textbox with the DLookup expression is on a textbox on the main form.
    2. Did you define strUserName in a standard module? Public strUserName As String
    3. Did you save the entered user name on the login form
    For example strUserName=Me.txtUserName (or whatever the textbox is called)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  13. #13
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    The textbox is on the main form. About the standard module, I have no idea how is done.

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,702
    Did you create the variable strUserName somewhere and then assign a value to it first? You might have missed that point. Also, I suspect Isladogs meant for you to assign the control source in the load event because of the use of a variable, not in the control itself. But I don't see this working when that expression is entered directly into the control because I don't see how the variable is exposed to the unbound control on the form. Also, there's no point in doing that in code (in this case) even if it works. I would set the value of the control according to that expression, not assign the control source to it.

    e.g. Me.someControlName = DLookup("FirstName", "tbl_login", "UserName = '" & strUserName & "'")

    but as noted, the variable needs to be declared in the load event and a value assigned to it.

    You might find that using Tempvars is easier.
    Last edited by Micron; 05-09-2021 at 10:16 AM. Reason: clarification and correction
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,946
    Also, I suspect Isladogs meant for you to assign the control source in the load event because of the use of a variable, not in the control itself. But I don't see this working when that expression is entered directly into the control because I don't see how the variable is exposed to the unbound control on the form. Also, there's no point in doing that in code (in this case) even if it works. I would set the value of the control according to that expression, not assign the control source to it.
    Sorry. Brain wasn't in gear when I wrote post #2 before breakfast!
    To clarify:
    1. Using an expression like Me.txtFirstName = DLookup("FirstName", "tbl_login", "UserName = '" & strUserName & "'") WILL work in the Form_Load event
    BUT as Micron stated it won't work in the control source of the textbox

    2. However if you create a function in a public module like this:

    Code:
    Public Function GetUserName()
         GetUserName=strUserName
    End Function
    Then you can use the following
    in the Form_Load event OR the control source property
    Code:
    = DLookup("FirstName", "tbl_login", "UserName = '" & GetUserName() & "'")
    3. Don't forget to include this the declarations section at the top of a standard module
    Code:
    Public strUserName As String
    OR you can use TempVars instead of all the above!

    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Page 1 of 4 1234 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 13
    Last Post: 10-02-2014, 09:29 AM
  2. Replies: 3
    Last Post: 03-17-2014, 10:23 AM
  3. Replies: 6
    Last Post: 02-21-2013, 10:52 AM
  4. Username and passwod login using Form
    By Tom1 in forum Programming
    Replies: 4
    Last Post: 07-04-2012, 12:29 PM
  5. Replies: 3
    Last Post: 05-31-2012, 02:49 PM

Tags for this Thread

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