|
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
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 |
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 |