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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
NorieAnalyst Assistant Commented:
Sandra

Why can't you 'grab' the ID each time you need it?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.