Custom Search
 


How to find out your computer name and username by VBA



There are times you need to log data for record-keeping and/or auditing purposes. The information you want to collect most likely includes run-time related data such as date and time, computer name and the logged in username of the Access program (or any office applications). This can be easily achieved by using VBA Environ function to obtain Environment Variables of the operating system your Access program is running on.

To get username or computer name, simply use the following environ function snippet.

Environ$("USERNAME")

OR

Environ$("COMPUTERNAME")

The Environ function takes a number or the name of an environment variable as the input argument and returns the text value of the environment variable of the computer, either system or user-defined environment variables.

Syntax

  1. Environ(number)

    When the input argument is a number, the function returns a text value with an equal sign (=) separating the name of the environment variable and its value, e.g. Environ$(1) returns ALLUSERSPROFILE=C:\ProgramData

  2. Environ(name of the environment variable)

    When the input argument is the name of the environment variable, the function returns the variable's value, e.g. Environ$("ALLUSERSPROFILE") returns C:\ProgramData

If the number or variable name can't be found in the environment string table, a zero-length string ("") is returned.

To view environmental variables, in your windows search bar, search "environ".

Use VBA to get the full list of environment variables

We're going to retrieve all the environment variables of your computer and store them in an Access table.

First, in Access create a table as below.

Then, create a button and add the following VBA code to the button's On Click event.

This code basically loop through numbers from 1 to 255 to extract each variable by passing in a number to the function. Because we don't know how many environment variables on your computer (as there could be user-defined environment variables), we use 255 as a maximum number. We store the returned value in a table when Environ function returns a non-empty value.

Note that the data in the Variable_Name column can be used in your VBA program as input argument to Environ function to get the relevant value for the environment variable.

Option Explicit

Private Sub Command0_Click()
    Call PopulateEnvironmentVariables
End Sub

Sub PopulateEnvironmentVariables()
    Dim strEnviron As String
    Dim arrEnviron As Variant
    Dim i As Long
    Dim strVariableName As String
    Dim strVariableValue As String
    
    '' Empty the table first
    CurrentProject.Connection.Execute "delete * from EnvironmentVariables"
    
    For i = 1 To 255
        '' Full variable name
        strEnviron = Environ$(i)
        
        '' When the variable exists, the variable name and variable value are separated
        '' by an equal sign (e.g. ALLUSERSPROFILE=C:\ProgramData).
        If Len(strEnviron) > 0 Then
            '' Use split function to turn it into an array of two elements.
            arrEnviron = Split(strEnviron, "=")
            
            strVariableName = arrEnviron(0)
            strVariableValue = arrEnviron(1)
            
            '' Add to an Access table
            CurrentProject.Connection.Execute 
		"insert into EnvironmentVariables(ID, Variable_Name, Variable_Value) " & _
                "values(" & i & ",'" & strVariableName & "','" & strVariableValue & "')"
        End If
    Next
End Sub

Below is a sample of the list of environment variables stored in the Access table. Total 41 records for my computer.

Full list of variable names for Environ function.

Environ$("ALLUSERSPROFILE")
Environ$("APPDATA")
Environ$("CommonProgramFiles")
Environ$("CommonProgramFiles(x86)")
Environ$("CommonProgramW6432")
Environ$("COMPUTERNAME")
Environ$("ComSpec")
Environ$("DriverData")
Environ$("EFC_2660")
Environ$("FPS_BROWSER_APP_PROFILE_STRING")
Environ$("FPS_BROWSER_USER_PROFILE_STRING")
Environ$("HOMEDRIVE")
Environ$("HOMEPATH")
Environ$("LOCALAPPDATA")
Environ$("LOGONSERVER")
Environ$("NUMBER_OF_PROCESSORS")
Environ$("OneDrive")
Environ$("OneDriveConsumer")
Environ$("OS")
Environ$("Path")
Environ$("PATHEXT")
Environ$("PROCESSOR_ARCHITECTURE")
Environ$("PROCESSOR_IDENTIFIER")
Environ$("PROCESSOR_LEVEL")
Environ$("PROCESSOR_REVISION")
Environ$("ProgramData")
Environ$("ProgramFiles")
Environ$("ProgramFiles(x86)")
Environ$("ProgramW6432")
Environ$("PSModulePath")
Environ$("PUBLIC")
Environ$("SESSIONNAME")
Environ$("SystemDrive")
Environ$("SystemRoot")
Environ$("TEMP")
Environ$("TMP")
Environ$("USERDOMAIN")
Environ$("USERDOMAIN_ROAMINGPROFILE")
Environ$("USERNAME")
Environ$("USERPROFILE")
Environ$("windir")



Copyright© GeeksEngine.com



Other Recent Articles from the MS Access category:

1.Examples of MS Access DateDiff function used in query and VBA code
2.MS Access DateDiff function
3.Examples of MS Access DatePart function
4.MS Access DatePart function
5.Examples of MS Access DateAdd function
6.MS Access DateAdd function
7.IIF function basics - the CASE statement of MS Access
8.MS Access Date Expression
9.Solved: MS Access error "The text is too long to be edited"
10.Create MS Access Combo Box essential properties by VBA code
11.Create MS Access Combo Box essential properties manually
12.How to do text search in MS Access programmatically
13.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
14.How to easily get a list of field names in MS Access
15.How to count distinct records in MS Access
16.How to do transaction based processing in MS Access
17.How to open a document (local/network file or web page) from MS Access
18.How to use ADOX to create unique composite index - the VBA approach
19.How to do cross-table update queries in MS Access - the right way
20.Three efficient ways to get the number of records by using VBA
21.How to create a composite unique index (not as a primary key) in MS Access
22.Use VBA to get the correct number of records in a Recordset object
23.Disable Access Prompt when a record is changed, table deleted, or action queries run
24.How to hide and unhide a MS Access object
25.How to return multiple values from a VBA function (Part 3)
26.How to return multiple values from a VBA function (Part 2)
27.How to return multiple values from a VBA function (Part 1)
28.Three ways to programmatically duplicate a table in MS Access by VBA
29.Create a DLL by CSharp or VB.Net for VBA
30.How to correctly reference and call a DLL
31.How to register a C# or VB.Net DLL
32.Email address validation by Regular Expressions using VBA
33.Fix MS Access error: Query must have at least one destination field
34.How to unselect radio buttons in MS Access after it has been selected
35.How to Change Query Timeout Value for MS Access SQL Queries
36.What is Northwind Traders database

Copyright © 2024 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy