totn Access Functions

MS Access: Environ Function

This MSAccess tutorial explains how to use the Access Environ function with syntax and examples.

Description

The Microsoft Access Environ function will return the value of an operating system environment variable.

Syntax

The syntax for the Environ function in MS Access is:

Environ ( numeric_position )

OR

Environ ( variable_name )

Parameters or Arguments

numeric_position
An integer value indicating the numeric position of the environment variable in the table.
variable_name
A string value representing the name of the environment variable.

Returns

The Environ function returns a string value.

Applies To

The Environ function can be used in the following versions of Microsoft Access:

  • Access 2019, Access 2016, Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the Environ function in MS Access.

Numeric Position

You can use the Environ function to retrieve the value of an environment variable by providing the numeric position of the variable within the environment variable table.

For example:

Environ(1)
Result: "ALLUSERSPROFILE=C:\ProgramData"

Environ(2)
Result: "APPDATA=C:\Users\totn\AppData\Roaming"

Environ(3)
Result: "CLIENTNAME=totn"

When you supply the numeric position, it will return both the name of the environment variable as well as its value.

Variable Name

You can also use the Environ function to retrieve the value of an environment variable by passing in the name of the environment variable.

For example:

Environ("ALLUSERSPROFILE")
Result: "C\ProgramData"

Environ("APPDATA")
Result: "C:\Users\totn\AppData\Roaming"

Environ("CLIENTNAME")
Result: "totn"

When you supply the environment variable name, it will only return the value for the variable. It does not include in the name in the result.

Example in VBA Code

The Environ function can be used in VBA code in Microsoft Access.

For example:

Dim LPosition As Integer

For LPosition = 1 To 8
   MsgBox Environ(LPosition)
Next LPosition

In this example, a message box would appear that displays each variable name and value combination for the first 8 environment variables.