UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Global Variables    
Global Variables



Global variables are variables that a available throughout a VBA application. So, regardless of the module in which they are created, you can modify their values from any other module. Note: global variables are common to most programming languages. The language context for this article is Access VBA.

Creating Global Variables

To create a global variable, declare it at the module level of any standard module using the 'Public' key word:

Public gtagNAME as datatype

Substitute the name you want to use for Name and specify the data type you want for the variable. The RVBA naming convention suggests that the name be prefixed with 'g,' to indicate that this is a global variable followed by a three character data type tag. For example, if you wanted a global variable to store the current user name you might declare it like this :

Public gstrUserName as String


The main advantage of a global variable is that you can refer to it in any expression in any code module. For example, if you want to display the user name in a textbox on a form, you would make this assignment:

me.txtUser = gstrUserName


The main disadvantage of a global variable is that you can modify its value from any procedure. All it takes is an simple assignment statement:

gstrUserName = "John Smith"

In smaller projects this may not be an issue but in any non-trivial project, using a global variable can result in hours of work tracking down a program bug. Remember that the global can be declared in any module and have its value changed in any module. So tracking down just which statement changed the value when you have thousands of statements can be difficult.

Global Variables Object

Centralizing the management of globals in a custom class can simplify using globals. Just create a class module with a property defined for each global variable you want to use in your project. Use a read-only property to refer to when you want to read the value and write-only property with a slightly different name for assigning values to the variable.


dim mstrUserName as String

Public Property Get UserName() As String
  UserName = mstrUserName
End Property

Public Property Let UserName_(rData As String)
     mstrUserName_ = rData
End Property

Declare an object of this class as a global:

Public gGlobals as clsMyGlobals

and instantiate it :

Set gGlobals = new clsMyGlobals

You can then create local instances of this object in each Form module that needs to refer to it:

dim mGlobals as clsMyGlobals

and create a pointer to the global in one of the module's procedures

Set mGlobals = gGlobals

A form's Open event handler is a good place for this last statement.

Using a single object to contain all global variables will reduce, but not completely eliminate the negative side effects of global variables yet allow you to take advantage of their availability program-wide for the small cost of using object style referencing when you need to use their values or change them.

Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 4,486 times.  This page was last modified 11:34, 9 October 2014 by Glenn Lloyd.   Disclaimers