Environ, Environ$ Functions

Named Arguments

Yes

Syntax

Environ({envstring | number})


envstring

Key: Optional

Data Type: String

The name of the required environment variable.


number

Key: Optional

Data Type: Numeric expression

The ordinal number of the environment variable within the environment string table.

Return Value

Environ returns a string containing the text assigned to envstring.

Description

Returns the value assigned to an operating-system environment variable.

Rules at a Glance

  • A zero-length string ("") is returned if envstring doesn't exist in the operating system's environment-string table, or if there is no environment string in the position specified by number.

  • envstring and number are mutually exclusive; that is, you can specify one or the other, but not both.

Example

Private Type env
   strVarName As String
   strValue As String
End Type

Private Sub Form_Load()

Dim intCtr As Integer, intPos As Integer
Dim strRetVal As String
Dim udtEnv As env

intCtr = 1
Do
   strRetVal = Environ(intCtr)
   If strRetVal <> "" Then
      intPos = InStr(1, strRetVal, "=")
      udtEnv.strVarName = Left(strRetVal, intPos - 1)
      udtEnv.strValue = Mid(strRetVal, intPos + 1)
   Else
      Exit Do
   End If
   intCtr = intCtr + 1
Loop

End Sub

Programming Tips and Gotchas

  • If you use number to specify the environment variable, both the name and the value of the variable are returned. An equals sign (=) separates them. For example, the function call Environ(1) might return the string TEMP=C:WINDOWSTEMP.

  • If you retrieve environment variables and their values by ordinal position, the first variable is in position 1, not position 0.

  • Due to the flexibility offered, it's now accepted and recommended practice to use the registry for variables needed by your application, rather than the environment string table.

  • Environment variables can be defined in a variety of ways, including by the AUTOEXEC.BAT and MSDOS.SYS files, as well as by the HKEY_LOCAL_MACHINESystemCurrentControlSetControlSessionManagerEnvironment and HKEY_CURRENT_USEREnvironment keys in the registry. However, the Environ function doesn't recognize environment variables defined in the registry.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.167.22