Results 1 to 3 of 3
  1. #1
    sstiebinger is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Mar 2015
    Posts
    105

    Label Caption string with global variable

    Hi all, I have a label on my navigation form named lblLoggedInAs and I have this code in my navigation form:



    Code:
    Option Compare Database
    Public GBL_strUser As String
    Private Sub Form_Open(Cancel As Integer)
        Me.lblLoggedInAs.Caption = "Logged in as: " & GBL_strUser
    End Sub
    I'm setting the global variable from the login form... code is here for completeness:

    Code:
    Option Compare Database
    Private Sub cmdLogin_Click()
        Dim strUserName As String
        If IsNull(Me.txtLoginName) Or IsNull(Me.txtLoginPass) Then
            MsgBox "Both fields required.", vbOKOnly, "Missing Data"
            Exit Sub
        End If
        If IsNull(DLookup("[UserName]", "tblUsers", "[UserName]='" & Me.txtLoginName.Value & "' And [Pass]='" & Me.txtLoginPass & "'")) Then
            MsgBox "Invalid Entry, Please Try Again.", vbOKOnly, "Invalid Entry"
            Exit Sub
        End If
        strUserName = Me.txtLoginName.Value
        DoCmd.Close
        DoCmd.OpenForm "frmNavigation"
        Forms("frmNavigation").GBL_strUser = strUserName
    End Sub
    Private Sub cmdQuit_Click()
        DoCmd.Quit
    End Sub
    When I open the login form, the label shows "Logged in as: " with no username but if I open the form in Design view, it shows the username... what am I doing wrong??

    I'm putting the username in the global variable so it's available for other forms, if needed.
    If I set the caption at the login forms (bypassing the global variable) it displays properly. i.e.
    Code:
    Forms("frmNavigation").lblLoggedInAs.Caption = "Logged in as: " & strUserName
    but then I don't have that available for other forms.

  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,929
    Problem with global variables is if code execution is interrupted, they lose their value. Alternatives are:

    1. TempVars - do not lose value if code is interrupted

    2. I set a textbox on a form that never closes (MainMenu) then the value is always available for reference
    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
    sstiebinger is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Mar 2015
    Posts
    105
    Ahh.... Didn't know how to use TempVars, but that solved my issue! thanks June7

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

Similar Threads

  1. Result of query into label.caption ?
    By SpookiePower in forum Queries
    Replies: 7
    Last Post: 12-17-2013, 01:56 PM
  2. loading a button caption to a global variable
    By texanmike02 in forum Access
    Replies: 7
    Last Post: 11-14-2013, 11:36 AM
  3. Getting Value as label Caption
    By Naveen Marapaka in forum Forms
    Replies: 4
    Last Post: 09-18-2013, 12:22 AM
  4. Replies: 6
    Last Post: 01-16-2013, 12:12 PM
  5. Set Label.Caption to field of recordsource
    By ngruson in forum Forms
    Replies: 1
    Last Post: 08-11-2010, 09:59 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