Results 1 to 12 of 12
  1. #1
    Hammilton is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    23

    Store logged in username in a variable


    Hello, I have a database that I'm still working on. I've made some progress but I'm hung up on more than I've completed unfortunately. I could use your help with solving this and then I can fix a number of problems I'm having.

    In my database, the user logs in, and the log in works flawlessly. However, once that user logs in I'd like to store his username and fullname in a variable. I can't figure out how to use variable strings from one form to the next. For one, I'd like to display the username of the logged in user once they're logged in.

    I also need that information to compare against in the password changing screen. I don't want the user to need to enter their username in order to change their password, too, just the old password.

    You've been extremely helpful and I am very grateful for your assistance.

    Thanks guys! (and gals)

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Common topic.

    I save the user info on a form that never closes (MainMenu) so that it is always available for reference.

    Alternatives:

    1. declare a public variable in a general VBA code module - the variable can be referenced from any VBA procedure in the db - drawbacks are cannot be referenced in Access objects and if code execution is interrupted, the variable loses value (this can be annoying during development and debugging)

    2. TempVars - advantages are can be referenced in Access objects and do not lose value if code execution is interrupted but may be a little harder to understand how to use than VBA variables
    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
    Hammilton is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    23
    I like the saving it on the main form, but how do I get it from the login form to the main form? It's a variable on the login screen, you log in, that form closes and the new form opens.

    I see the question asked but maybe I'm dumb, and I probably I am, but I can't figure it out.

    Thanks!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    1. open MainMenu

    2. set value of textbox on MainMenu

    3. close login form

    Or pass info using OpenArgs https://www.accessforums.net/access/...gin-23585.html
    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
    Hammilton is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    23
    Okay, this is the code I have:

    Dim rs As Recordset
    Dim db As Database
    Dim tempUserID As String

    Set db = CurrentDb
    Set rs = db.OpenRecordset("Users", dbOpenSnapshot, dbReadOnly)

    rs.FindFirst "Username = '" & Me.txtUserName & "'"

    If rs.NoMatch = True Then
    Me.lblIncorrectUsername.Visible = True
    Me.txtUserName.SetFocus
    Exit Sub
    End If
    Me.lblIncorrectUsername.Visible = False

    If rs!Password <> Nz(Me.txtPassword, "") Then
    Me.lblIncorrectPassword.Visible = True
    Me.txtPassword.SetFocus
    Exit Sub
    End If
    Me.lblIncorrectPassword.Visible = False
    If rs!Admin = True Then
    DoCmd.OpenForm "frmMain"
    frmMain.lblUser = Me.txtUserName
    DoCmd.Close acForm, Me.Name

    Else
    DoCmd.OpenForm "frmUserMain"
    DoCmd.Close acForm, Me.Name
    End If



    The code highlighted in red is what's not working. What am I doing wrong?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Try:

    Forms!
    frmMain.lblUser.Caption = Me.txtUserName
    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.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Because of the "lbl" prefix, "lblUser" would indicate to me the object is a label. You have to use the "Caption" property for labels. Labels do not have a "Value" property.

    Forms!frmMain.lblUser.Caption = Me.txtUserName




    June beat me ....again

  8. #8
    Hammilton is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    23
    10-4, works like a charm.

    When do you use the ! symbol? Anytime I'm referencing something that's open on another form or report?

    Can I use rs!Full Name instead of the value that's in the textbox? That's what I'd really like to do. However, there's a space in that, and I'm not sure how to use it when there's a space in it.

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Best option - remove all spaces in object names.

    Option #2 - try rs![Full Name] or rs("Full Name")

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I use the dot to invoke intellisense popup tips.

    I use the bang (!) when referencing field names and in some other uses as in referencing object such as the Forms!formname syntax.

    I would probably use a DLookup() instead of opening a recordset object.
    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.

  11. #11
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    post #2 is one of the best explanations of ! versus . that I've seen. What I've read on the subject at MS for VB4 seems to be echoed here, but in a way that the writer expounds on how the use of either character is translated by the application interpreter.
    http://www.utteraccess.com/forum/Me-Me-t1644997.html&pid=1645019&mode=threaded#entry16450 19

    For those who may be quick to point out, I realize VB4 (or any generation of it) is not VBA per se.

  12. #12
    Hammilton is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2015
    Posts
    23
    Thanks, your help here enabled me to solve the one problem I had initially, and thanks to what I learned I was able to do a lot of other things I was hung up on.

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

Similar Threads

  1. How to creae username for people logged on to Access
    By Mubashir sabir in forum Security
    Replies: 3
    Last Post: 01-21-2016, 12:27 PM
  2. Replies: 6
    Last Post: 12-20-2015, 01:52 AM
  3. Date variable Store
    By drunkenneo in forum Programming
    Replies: 1
    Last Post: 07-01-2013, 05:57 AM
  4. Replies: 5
    Last Post: 08-08-2012, 01:28 PM
  5. Username as Global Variable
    By imintrouble in forum Access
    Replies: 3
    Last Post: 10-10-2011, 10:45 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