Solved

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

Posted on 2012-03-23
8
619 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

12 Experts available now in Live!

Get 1:1 Help Now