Solved

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

Posted on 2012-03-23
8
653 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 58
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 34

Expert Comment

by:Norie
ID: 37758443
Sandra

Why can't you 'grab' the ID each time you need it?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 58

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

628 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