VBA & Macro Problems

How to Create Global Computer and User Name Variables in Access VBA

Anum MushtaqAnum Mushtaq Nov 3, 2026 869 views

Question details

The user needs to store, reuse, and accurately concatenate global computer and user names in Microsoft Access VBA without data dropping out.

How to Create Global Computer and User Name Variables in Access VBA
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.
Before you start

Verify that your VBA environment can properly retrieve system environment variables by testing Environ("USERNAME") and Environ("COMPUTERNAME") in the Immediate window.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to open the Microsoft Visual Basic for Applications editor in Access.

2
Initialize the TempVars

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.

3
Concatenate the TempVars

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.

4
Apply in a Form Subroutine

You can reference this directly in a form. For example, assign it to a text box: Me.txtUserInfo.Value = TempVars!strUserName & " " & TempVars!strComputerName.

Use TempVars for Global Storage and Concatenation
Consistent Naming: Using exact and consistent names like TempVars!strUserName eliminates the issue where only one part of the string evaluates correctly.
Free Microsoft Office alternative

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. 1. Download the Installer: Visit the official WPS Office website and click the Free Download button.
  2. 2. Install the Software: Run the lightweight installer to quickly set up Writer, Presentation, and Spreadsheets.
  3. 3. Open Existing Documents: Easily open, edit, and save your existing Office documents with seamless format compatibility.
Fully compatible with Microsoft Excel (.xlsx), Word (.docx), and PowerPoint (.pptx) formats.Features advanced Spreadsheet tools and Macro/VBA capabilities for automating daily data tasks.Lightweight architecture ensures fast installation and smooth operation on older hardware.
microsoft office alternative - wps office

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.