How to Create Global Computer and User Name Variables in Access VBA
Question details
The user needs to store, reuse, and accurately concatenate global computer and user names in Microsoft Access VBA without data dropping out.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Developing an Access database where system information (user and computer names) must be globally available across modules and form subroutines.
- Observed behavior
- When attempting to concatenate the user and computer names, the Immediate window or output only displays the computer name due to inconsistent variable naming or incorrect concatenation syntax.
Verify that your VBA environment can properly retrieve system environment variables by testing Environ("USERNAME") and Environ("COMPUTERNAME") in the Immediate window.
Use TempVars for Global Storage and Concatenation
Using TempVars is the most resilient way to handle global variables in Access, as they do not reset when an unhandled runtime error occurs.
TempVars can be created anywhere in your VBA code and remain in memory until the database is closed or they are explicitly removed. This makes them ideal for storing environment-level data like user and computer names that need to be accessed from any form subroutine.
Press ALT + F11 to open the Microsoft Visual Basic for Applications editor in Access.
Use the Add method to store the system variables. Type: TempVars.Add "strUserName", Environ("USERNAME") and TempVars.Add "strComputerName", Environ("COMPUTERNAME") in your startup routine.
To join the variables with a space, use the ampersand (&) operator: TempVars!strUserName & " " & TempVars!strComputerName. Ensure you are using the exclamation mark syntax for TempVars.
You can reference this directly in a form. For example, assign it to a text box: Me.txtUserInfo.Value = TempVars!strUserName & " " & TempVars!strComputerName.

Declare Public Variables in a Standard Module
For strictly typed variables, you can declare Public variables within a Standard Module and initialize them upon database startup.
Looking for a Lightweight Office Suite Alternative?
While Microsoft Access manages relational databases, everyday data tracking and complex analysis are often faster and easier in a powerful spreadsheet. WPS Office provides a free, lightweight suite featuring advanced Spreadsheets with excellent Microsoft Excel compatibility and robust VBA/Macro support.
- 1. Download the Installer: Visit the official WPS Office website and click the Free Download button.
- 2. Install the Software: Run the lightweight installer to quickly set up Writer, Presentation, and Spreadsheets.
- 3. Open Existing Documents: Easily open, edit, and save your existing Office documents with seamless format compatibility.

Frequently Asked Questions
Why does my concatenation only show the computer name and not the user name?
This happens when the user name variable is empty, undeclared, or misspelled. If you are concatenating 'strUserName & " " & strComputerName', and 'strUserName' has no value, the Immediate window will only return the space and the computer name. Double-check your variable initialization syntax.
Can I use TempVars directly inside an Access query?
Yes, one of the major advantages of TempVars over Public variables is that they can be directly referenced in Access queries and macros. You can use syntax like '[TempVars]![strUserName]' in your query criteria.
Does this global variable approach work in a form's subroutine?
Absolutely. Whether you use TempVars or Public variables declared in a Standard Module, the values are globally accessible. You can call and concatenate them directly within any form's events, such as 'Form_Load' or a button click.
What is the difference between TempVars and Public variables in VBA?
TempVars are more resilient because they retain their values even if the VBA project experiences an unhandled runtime error and resets. Public variables, however, offer the benefit of strict data typing (like String, Integer, etc.), whereas TempVars are always evaluated as Variants.




