Others

How to Return the Computer Name with Access VBA

Ayan MasoodAyan Masood Nov 2, 2026 869 views

Question details

The user needs to retrieve and record the computer name whenever a system is accessed via an Access database, requiring a VBA solution usable within an Access query.

How to Return the Computer Name with Access VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
Recording user access logs in a database query or table.
Observed behavior
Needs a custom VBA function that fetches the computer name because environment variables cannot be called directly within an Access query expression.
Before you start

Ensure you have enabled Macros and have access to the VBA editor (ALT + F11) in your Access database before adding custom functions.

Solution 1Recommended

Create a Custom VBA Function using Environ

Since environment variables cannot be called directly in a table or query, you must wrap the Environ function inside a custom VBA module.

The standard method for retrieving system environment details in VBA is using the Environ function. By placing this inside a public function, it becomes accessible to all queries within your Access database.

1
Open the VBA Editor

Press ALT + F11 while your Access database is open to launch the Visual Basic for Applications editor.

2
Insert a New Module

From the top menu, click on 'Insert' and select 'Module' to create a blank script window.

3
Write the Custom Function

Type the following code into the module: Function ComputerName() As String ComputerName = Environ("computername") End Function

4
Save and Use in a Query

Save your VBA module and return to the main Access window. Open your query in Design View, and create a new field by typing: TheName: ComputerName()

Create a Custom VBA Function using Environ
Security Consideration: Environment variables can sometimes be spoofed by users. If high security and strict audit trails are required, consider using the WScript.Network method instead.
Free Microsoft Office alternative

Switch to WPS Office for Powerful Data Management

While WPS Office does not include a direct relational database replacement for Microsoft Access, it offers an incredibly robust, free alternative for Excel, Word, and PowerPoint. If you manage extensive data primarily through spreadsheets, WPS Spreadsheets provides advanced VBA and macro support to handle complex automation without the heavy overhead of Microsoft Office.

  1. 1. Download WPS Office: Visit the official WPS Office website and download the free installation package for your operating system.
  2. 2. Install the Software: Run the installer and follow the quick setup wizard to install the suite in minutes.
  3. 3. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing data files (.xlsx, .csv) to begin utilizing advanced VBA macros instantly.
Seamless compatibility with Microsoft Office formats including XLSX, DOCX, and PPTXAdvanced VBA and macro support in WPS Spreadsheets for custom automation tasksFree, lightweight, and fast installation processFamiliar tabbed user interface requiring zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why can't I use Environ directly in an Access query?

Microsoft Access restricts the direct execution of certain VBA environment functions within query expressions or table default values for security and parsing reasons. You must wrap the command in a public VBA function first so the query engine can evaluate it.

How do I log the current Windows user instead of the computer name?

You can create an identical VBA function but use Environ("USERNAME") to retrieve the currently logged-in Windows username. Alternatively, CreateObject("WScript.Network").UserName will provide the same result more securely.

Does this VBA code work in Excel or WPS Spreadsheets?

Yes, both the Environ function and the WScript.Network object are standard VBA components and can be utilized in Excel and WPS Spreadsheets VBA modules to fetch system information.