How to Use a Global VBA Variable in an Access Form or Report
Question details
The user needs to reference or use a global VBA variable directly within the expressions, control sources, forms, or reports of a Microsoft Access database.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Binding a globally declared VBA variable to a form's control source or passing it into an expression within an Access report.
- Observed behavior
- Microsoft Access expressions and controls cannot directly read or evaluate global VBA variables, resulting in blank fields or expression errors unless a workaround is applied.
Ensure you have your Microsoft Access database open and possess basic familiarity with the VBA Editor (accessible via ALT + F11). Save a backup of your database before modifying modules or control sources.
Expose the Global Variable Using a Public Function
Since Access forms and reports cannot read VBA variables directly, you must wrap the global variable inside a public function. You can then call this function in your control's expression.
This is the most standard method for passing in-memory VBA values to the Access UI. The function must be placed in a Standard Module to ensure it is globally visible to all forms, reports, and queries.
Press ALT + F11 while inside your Access database to open the Visual Basic for Applications (VBA) editor.
In the VBA editor menu, click 'Insert' and select 'Module'. Do not place this code in a Form or Report module, as it will not be globally accessible.
At the top of the module, declare your global variable (e.g., `Public gi_incentivescount As Integer`). Below it, create a public function that returns its value: `Public Function GetIncentivesCount() As Integer \n GetIncentivesCount = gi_incentivescount \n End Function`.
Close the VBA editor and open your form or report in Design View. Select the text box you want to populate, open the Property Sheet, and type `=GetIncentivesCount()` into the Control Source property.

Use the TempVars Collection
TempVars is a built-in collection in Access that allows you to store temporary variables that can be natively read by macros, forms, and reports without needing wrapper functions.
Looking for a versatile and lightweight Office alternative? Try WPS Office
While Microsoft Access manages complex relational databases, many everyday data tracking, automation, and VBA macro tasks can be efficiently handled using advanced spreadsheets. WPS Office provides a lightweight, highly compatible, and free alternative to Microsoft Office, complete with robust spreadsheet capabilities and comprehensive macro support.
- 1. Download the Installer: Visit the official WPS Office website and download the free installer for your operating system.
- 2. Install WPS Office: Run the setup file and follow the quick installation wizard to set up the software on your device.
- 3. Manage Data with Spreadsheets: Open WPS Spreadsheet to import your existing data files, edit formulas, and run VBA macros effortlessly.

Frequently Asked Questions
Why can't I bind a global VBA variable directly to an Access text box?
Microsoft Access forms, reports, and queries evaluate expressions within the Access UI context, which does not have direct access to the VBA memory space. To bridge this gap, you must use a standard Public Function, which the UI can call to retrieve the memory value.
Where should I place the Public Function so my Access form can see it?
The function must be placed in a Standard Module. You can create one by opening the VBA Editor and selecting Insert > Module. If you place the function inside a Form Module or Report Module, it becomes restricted to that specific object and cannot be accessed globally.
What is the main difference between using a Public Function and TempVars?
A Public Function retrieves values stored in VBA memory and can handle complex logic or data types before returning a result. TempVars are stored directly by the Access engine, making them natively readable by macros and form expressions without writing any VBA wrapper, but they are limited to simpler data types like strings and numbers.
Will my global variable lose its value if an error occurs in VBA?
Yes. Unhandled run-time errors in VBA can cause the code execution to reset, which wipes all variables from memory (including Global variables). Using TempVars is a safer alternative in this regard, as they persist even if the VBA project resets.




