Results 1 to 13 of 13
  1. #1
    CementCarver's Avatar
    CementCarver is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    231

    Can't See Global Variable

    I've created a public module where I declare a global variable. So, I'm assuming that I should be able to see this variable strAccountName from other modules, but I can't....!!!



    Do I have to call this module called WhoLoggedIn, to see that value is, or should the strAccountName be present in other private modules?

    Here's my public module.......

    Option Compare Database
    'Access global variables definition

    Global strAccountName As String

    Option Explicit
    Public Sub WhoLoggedIn(empname As String)

    Dim strAccountName As String

    strAccountName = empname

    End Sub

    CementCarver

  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,815
    If the variable is declared in the module header it should be accessible from any module. There is no need to Dim the variable again and actually that should error.
    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
    mrojas is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2013
    Location
    Concord California
    Posts
    72
    Declare the variable above the Sub, below the Option Explicit as follows:
    Public strAccountName as string

    Since it's a global variable, I would rename it to gstrAccountName

    Remove the declaration of it in the Sub

  4. #4
    CementCarver's Avatar
    CementCarver is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    231
    mrojas, thanks for your response. Here's my code so far, but if I call the public variable in another module, with a msgbox to show who's logged in, I still cannot see that global variable:

    Option Compare Database
    'Access global variables definition

    Option Explicit

    Public strAccountName As String
    Public Sub WhoLoggedIn(empname As String)

    strAccountName = empname

    End Sub


    CementCarver

  5. #5
    mrojas is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2013
    Location
    Concord California
    Posts
    72
    What do mean you do not see it? What does the message box show as the content of the variable? Do you get an error message?
    I'm assuming that after a user logs in, you call the WhosLoggedIn routine, correct?
    I also assume that you are talking about a single user database application?

    I don't believe that you can share one computer's memory with another in a network environment.

  6. #6
    CementCarver's Avatar
    CementCarver is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    231
    When I append the global variable name to the of the msgbox with & strAccountName, it does not appear in the message box and there are no errors either.

    Yes, it is a single user database after after the login box is correctly filled in, the module WhosLoggedin is called.

    CementCarver

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Show the procedure code that calls the WhoLoggedIn sub.
    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.

  8. #8
    CementCarver's Avatar
    CementCarver is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    231
    Here it is:

    If Me.txtPassword.Value = DLookup("Password", "Employee", "[Emp_ID]=" & Me.cboEMPLOYEE.Value) Then

    lngMyEmpID = Me.cboEMPLOYEE.Value
    empname = DLookup("Employee", "Employee", "[Emp_ID]=" & Me.cboEMPLOYEE.Value)
    WhoLoggedIn (empname)
    'Debug.Print "This person logged is " & strAccountName


    'Close logon form and open splash screen

    DoCmd.Close acForm, "frm_LOGIN", acSaveNo
    DoCmd.OpenForm "BUSINESS_MAIN"

    Else
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
    Me.txtPassword.SetFocus
    End If

    CementCarver

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    I did a test with two procedures like this and it works. Have you step debugged? Follow the code as it executes, see where it varies from expected behavior, fix, repeat. Debug guidelines in link at bottom of my post.

    But why do you have the WhoLoggedIn sub? Is it called from anywhere else?

    Why not just set the variable in the form procedure? Leave the global variable declaration in the general module but set it in the form code.

    Be careful with global variables. Their value can be lost when code execution is interrupted. This is why I decided not use a global variable for username. Instead I set the value of textbox on form that never closes (MainMenu) and refer to the textbox whenever I need user 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.

  10. #10
    CementCarver's Avatar
    CementCarver is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    231
    June7, the WhoLoggedIn sub is only called once to populate the public variable. I am under the assumption that that variable would be visible to any other sub. But am not sure how to implement your suggestion below:

    Why not just set the variable in the form procedure? Leave the global variable declaration in the general module but set it in the form code.

    CementCarver

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Instead of calling the sub:

    WhoLoggedIn(empname)

    just

    strAccountName = Nz(DLookup("Employee", "Employee", "[Emp_ID]=" & Me.cboEMPLOYEE.Value),"")


    But why set variable to employee name? Are you saving the employee name in records? Shouldn't. Should save Emp_ID.
    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.

  12. #12
    CementCarver's Avatar
    CementCarver is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    231
    Thanks June 7. Yes, I'm tracking who has logged in because I'm using an audit trail module to track all changes made to the database.

    Thanks for the line of code too. I'll try your suggestion.

    CementCarver

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Again, should save Emp_ID instead of employee name. In which case the employee name DLookup is not necessary.
    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.

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

Similar Threads

  1. How to use global/public variable
    By mrbabji in forum Programming
    Replies: 7
    Last Post: 05-18-2013, 10:08 PM
  2. Global variable
    By ramdandi in forum Queries
    Replies: 3
    Last Post: 12-18-2011, 01:01 AM
  3. Username as Global Variable
    By imintrouble in forum Access
    Replies: 3
    Last Post: 10-10-2011, 10:45 AM
  4. Replies: 4
    Last Post: 07-14-2011, 12:55 PM
  5. How to declare Global Variable
    By ganeshvenkatram in forum Access
    Replies: 1
    Last Post: 06-16-2011, 05:18 AM

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