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

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
Sandra SmithRetiredAsked:
Who is Participating?
 
mbizupCommented:
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
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
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
 
Sandra SmithRetiredAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
NorieVBA ExpertCommented:
Sandra

Why can't you 'grab' the ID each time you need it?
0
 
Paul JacksonSoftware EngineerCommented:
0
 
Sandra SmithRetiredAuthor Commented:
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
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.