Results 1 to 6 of 6
  1. #1
    jmitchelldueck is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    51

    VBA Public variables

    Hi there,

    I don't know quite how to phrase this question, but hopefully I can get the point across. I have two forms: an administrator login form and a main navigation form.

    I want my system to be able to recognize if the administrator is logged in to then display a "logout" button on the main navigation form. The way I had thought to accomplish this was by creating a variable (loggedIn as Boolean) in the code for my login form which sets to True if the username and password provided are correct. How do I then use my loggedIn variable in the code for my main navigation form?

    I know it has something to do with defining it as a public variable, but when I call the variable on the navigation form it gives me False (which I assume is the default value).

    Here is the login form code:

    Code:
    Public loggedIn As Boolean
    
    Public Sub Command4_Click()
    
    
    If Me.username = "a" And Me.password = "0" Then
        DoCmd.SelectObject acTable, , True
        DoCmd.ShowToolbar "Ribbon", acToolbarYes
        DoCmd.Close acForm, "AdminLoginF"
        loggedIn = True
        DoCmd.Close acForm, "Switchboard"
        DoCmd.OpenForm "Switchboard"
    Else
        MsgBox "Incorrect username and/or password."
        Me.password = ""
        Me.username.SetFocus
        loggedIn = False
    End If
    Navigation:



    Code:
    Public Sub Form_Activate()
    
    
    If loggedIn = False Then 'loggedIn from forms!AdminLoginF - public variable indicating whether or not the user is the admin or not.
        DoCmd.SelectObject acTable, , True
        DoCmd.RunCommand acCmdWindowHide 'hide navigation pane
        DoCmd.ShowToolbar "Ribbon", acToolbarNo 'hide ribbon
        
        
        Command7.Enabled = True
        Command7.Visible = True
        Command8.Enabled = False
        Command8.Visible = False
        
    Else
        
        Command7.Enabled = False
        Command7.Visible = False
        Command8.Enabled = True
        Command8.Visible = True
        
        MsgBox "Hello"
    End If
    
    
    End Sub
    thanks,

    jmd

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is the code to set the value to true running? I always declare public variables in a standard module, so you might try that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jmitchelldueck is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    51
    Yes, I had a message box give me the value of loggedIn from the admin login code (True) and the value of loggedIn from my navigation code (False). I'll try the declaration in a module - does that mean I do not need to declare it elsewhere?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, just once in a standard module.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jmitchelldueck is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    51
    Putting it in a standard module did it for me, thanks!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Using Public variables between forms/queries/reports
    By dcrake in forum Sample Databases
    Replies: 2
    Last Post: 12-25-2015, 05:44 PM
  2. How to use global/public variable
    By mrbabji in forum Programming
    Replies: 7
    Last Post: 05-18-2013, 10:08 PM
  3. public variables
    By zul in forum Programming
    Replies: 3
    Last Post: 08-23-2011, 11:11 AM
  4. Creating/Using Public Subs
    By sparlaman in forum Programming
    Replies: 3
    Last Post: 05-19-2011, 03:29 PM
  5. public instead of dim not working
    By DKY in forum Access
    Replies: 1
    Last Post: 10-14-2008, 11:42 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