Access Environment variables using VBA

List of environment variables.

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. Some time we need to recognize the user by Environ variable. For that we can use the Environ function. We can used this function to find the home path and to find the location of the My Documents folder as well as some other useful pieces of information regarding server name, operating system ,number of processors and the all the information that stored the environment variables. Environ function return the string associated with an operating system environment variable.

This article is about to create function that will return the all the items that contain the environment variable. We can use these items for different purpose. This article is about to create a routine which will find the path of database and system variables.

After creating routine we have to test this function. For that on navigation window we have to call the function. It will return the list of items that contain the environ variables as shown in fig 1.1.

How to use environment variables in VBA. Fig-1.1


This function also return the path of my document folder as shown in fig 1.2. There are other environmental variables also, like as:

Environ("COMPUTERNAME") return the name of your computer.

Environ("USERDOMAIN") return the domain you're logged on to.

Environ("USERPROFILE") return the path to where your files are stored.

How to use environment variables in VBA. Fig-1.2


VBA code

Option Compare Database
Sub main()
MsgBox Environ$("USERPROFILE") & "\My Documents"
End Sub
Sub showEnvironment()
Dim Bcheck As Boolean
Dim Intno As Integer
Dim Intpath As Integer
Dim msg As String
Intno = 1
Dim strEnviron As String
strEnviron = Environ(Intno)
While Not Bcheck And (strEnviron <> "")
If (Mid(strEnviron, 1, 5) = "Path=") Then
Debug.Print Intno & "=" & Environ(Intno)
Bcheck = True
Debug.Print Intno & "=" & Environ(Intno)
Intno = Intno + 1
strEnviron = Environ(Intno)
End If
End Sub


It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. Access Guru will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.