Home  Fees/Services  Access 2010 Template  Access Tutorial  Access Download  Articles  Search  Forums  Blockchain Tech  Contact  Privacy  Links

Visual Basic Global Variables
VBA Global Variables

Visual Basic Tutorials:
Access-Google Earth
Age Calculation
Change To Proper Case
Email via Gmail #1
Send Gmail Email #2
Inactivity Logout Code
Outlook Email
Read Email Access
Email Attachment
Send Outlook Email
Running Sum
Denormalize Records
Stock Quotes
Find Database Path
Detail-Master Update
Data Field Validation
Field Value New-Old
Access Version
Global Variable Parameter
Global Variables
Active Labels
Files List Box
Mail Merge
Quick Sort
Recordset Filters
Reference Form Field
Select Case
Access Transactions

Visual Basic Function Examples

VBA Global Variables / Program-Wide Data Storage

VBA Global variables are a powerful feature in Microsoft Access. There are two levels of these Visual Basic global variables:

  • Local variables defined in a form instance, usually when the form opens. These variables persist as long as the form remains open. These variables and constants are also known as Public Variables and Public Constants since they are known only to the form and subforms attached to a main form.

  • Are you just starting out on your quest for a small business database? Need some advice or someone to bounce ideas off of? We are happy to chat with you to discuss options to your situation.

    Are you looking for an MS Access template database? We have many for sale in our Access Template and Examples Software section.

  • Instance-wide variables persist for the duration of the Microsoft Access database session.  These variables, including global constants, are defined in association with a module and become active as soon as the module is called.

Here's an example of the Visual Basic code required to setup public form-level global variables:

Access Global Variable

Public gbl_previous_tab As String
Public gbl_username As String
Option Explicit

Private Sub Form_Open(Cancel As Integer)
    ' doing something here
End Sub

New! Download Access example of Global Variables as Query Parameters

As this example show the declaration of a database global variable come right after the 'Option Compare Database' statement and before all declarations of (any) subroutines and functions within the form.

The definition of instance-wide Access global variables is carried out in a module.  The following is the code we use to initialize our VBA global variables:

Global Variable Example
' Access global variables definition
Global GBL_Username As String
Global GBL_Previous_Tab As String

Option Explicit

Public Sub Init_Globals()
' Access global variable initialization
    GBL_Username = Environ("username")
    GBL_Previous_Tab = " "
End Sub

Then we make a call to the Init_Globals subroutine in our first-opening form as follows:

Global Variable Example
Option Explicit

Private Sub Form_Open(Cancel As Integer)
    Call Init_Globals
End Sub

As you can see there are many things you can keep track of with Access global variables.  You can also do some neat tricks - like passing parameters to SQL queries using global variables.

Try our downloadable Access database demonstration of using Global variables as query parameters.

All that said... global variables can be volatile and will be lost if your database VBA code has an unhandled error condition.  So, unless you are very careful with your VBA coding and ALWAYS have error handlers then you may find it easier and safer to use a persistent form in your database with fields (which can be hidden) to store values you want to keep track of and pass to sql queries, forms and vba code.

More Global Variable examples:

Global Variables as Query Parameters
Using global variables to pass user selection criteria to queries is a useful programming technique to master. This Access download will provide you with ...

Access Database Download: Access Security Alternative
Access Example Download, Download Access Examples Free. Home > Tutorials > Access Example Downloads ... Set global variables. Use the Dlookup function ...

Access Database Security Alternative
initialize global variables Call set_globals ' Hide tabs Me.TabCtl0.Pages. ... set global access level to failsafe no access then lookup access level ...

Blue Claw Database Design Downloadable Tutorial:
Choose Command Dynamic SQL Order By  

A Blue Claw Software Design Template:

Asbestos Survey Project Database

A Blue Claw Database Design Article:

How To Create Faster Access Databases

Microsoft Office:
MS Access 2000 Through 2016 and Office 365 & Sharepoint

Contact Information

Copyright 2000-2018 Blue Claw Database Design

Microsoft Access 2007, 2010, 2013 & 2016