Solved

Difference between a Global variable and Public in declaration section of module

Posted on 2012-03-23
8
629 Views
Last Modified: 2012-03-24
Is there a difference if a variable is dimentioned as:
Global gstrUserID   vs  strUserID
when declared at the top of a module (not a form or report module)?  I want the variable to be used throughout the application.

Sandra
0
Comment
Question by:ssmith94015
8 Comments
 
LVL 57
ID: 37758368
Sandra,

 Nothing really.  It's just that with a module level variable, you do have the option of using the Public or Private keyword with it, so you can restrict it's scope to the module.

With a global, it's always public.

Jim.
0
 

Author Comment

by:ssmith94015
ID: 37758394
Jim, I think I am getting confused then on how to use it.  I want the variable to be available to all procedures throughtout - that is, all modules in the database, forms, reports, etc..  What I want to happen is when the user opens the database, the opening form grabs the user's ID and sets this variable to its value.  At that point, I am hoping this value (gstrUserID) is now available throughout the database and for whatever processes or procedures, forms, reports that the user selects.  The userid is teh basis for granting or restricting access to various forms/reports.  Or, when the main menu is closed and another form is opened, does the value of this global variable go out of scope?  It seems I am losing this value and am not sure why.

Sandra
0
 
LVL 33

Expert Comment

by:Norie
ID: 37758443
Sandra

Why can't you 'grab' the ID each time you need it?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 37758497
Public/Global variables as Jim mentioned earlier are pretty much the same.

These are visible throughout your application:

Global gYourVariable
Public YourVariable


The DIM statement on its own at the very top of a code module will make a variable visible to all subs and functions in a given module:

Dim YourVariable


Global/Public hold their values as long as the application is open... sort of.

Errors in your code can cause these values to be lost.

An alternative solution I've seen used by some experts is to leave a form open/hidden in the background as long as your application is open, and to keep values you want to persist in controls on that form, so that they can be referred to at any time as:

Forms!YourHiddenForm!txtYourUserID
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758520
0
 

Author Comment

by:ssmith94015
ID: 37758538
I do not want to just "grab" it as it is used so often, it simply would make repeated calls to the VBUserName procedure.  It is much tidier this way.  However, I do like the idea of a hidden form and it seems to work.

Sandra
0
 
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 37758542
Sandra,

<< It seems I am losing this value and am not sure why.>>

  Global variables do not go out of scope.  Doing this in a standard module:

Public myString

is the same as

Global mystring

  The variable will be initialized as soon as the module loads and the variable will be available from that point forward until the app closes.

  BUT (and this is the big one), if code execution is stopped, all global variable loose their values and this is what your running into.

  You've got two choices in this case:

a. use a hidden form to hold the user ID (with a control) - as long as the form remains open, you'll have a value.

b. Use a function to return the value and populate it if empty.  For example:

Private objcurDB As DAO.Database

Public Function CurDb(Optional bolRefresh As Boolean = False) As DAO.Database

10     If objcurDB Is Nothing Or bolRefresh = True Then
20        Set objcurDB = CurrentDb()
30     End If
       
40     Set CurDb = objcurDB

End Function

 This is a slightly better approach to global variables as you can control the set/getting of them.  Personally (and I'm in the minorty on this), I don't mind global variables in my app, but many feel that variables should be as limited in scope as possible.

 Doing the above is a good way of doing that.  

 Note that even with the above, once a module is loaded, a module level variable is never removed, so declaring it private doesn't do anything other then restrict it's use to the module where it's declared.

  In this case, I could have just as easily (and proably would have been better), declared the variable in the CurDb() procedure with the keyword STATIC, which in essense turns it into a global variable, but it's scope is then limited to that one procedure.

Jim.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now