I am looking for some information or code samples for the Environ
Function in VBA to grab the username on the current system.
Asked
Active
Viewed 1.1e+01k times
20

JimmyPena
- 8,694
- 6
- 43
- 64

gary A.K.A. G4
- 1,013
- 8
- 23
- 36
-
4@the_drow: I ask that every day I press alt+f11. I blame Spolsky. – Eric Jun 01 '09 at 14:58
-
Gentlemen, I am looking to do 2 things; identify what my user name is on my PC (using the Environ Function) and once i understand that, I would like to write a statement that closes my database if the user is not me (in other words, if opened on another PC) Can you help me get started? – Marchese Il Chihuahua Sep 20 '14 at 06:51
3 Answers
44
Environ()
gets you the value of any environment variable. These can be found by doing the following command in the Command Prompt:
set
If you wanted to get the username, you would do:
Environ("username")
If you wanted to get the fully qualified name, you would do:
Environ("userdomain") & "\" & Environ("username")
References
- Microsoft | Office VBA Reference | Language Reference VBA | Environ Function
- Microsoft | Office Support | Environ Function

StackzOfZtuff
- 2,534
- 1
- 28
- 25

Eric
- 92,005
- 12
- 114
- 115
-
that was exactly what I was looking for, just a little help getting started, thank you. – gary A.K.A. G4 Jun 01 '09 at 14:58
-
2Don't forget that Environ is one of the functions blocked in Sandbox mode: http://office.microsoft.com/en-us/access/HP010447361033.aspx – Fionnuala Jun 01 '09 at 16:01
-
3Also, don't forget that Environ is a cached copy of what the environment variable was when the parent process started. Found this out the hard way. – ErinsMatthew Feb 13 '14 at 09:51
-
Gentlemen, I am looking to do 2 things; identify what my user name is on my PC (using the Environ Function) and once i understand that, I would like to write a statement that closes my database if the user is not me (in other words, if opened on another PC) Can you help me get started? – Marchese Il Chihuahua 4 secs ago edit – Marchese Il Chihuahua Sep 20 '14 at 06:52
23
As alluded to by Eric, you can use environ with ComputerName argument like so:
MsgBox Environ("USERNAME")
Some additional information that might be helpful for you to know:
- The arguments are not case sensitive.
- There is a slightly faster performing string version of the Environ function. To invoke it, use a dollar sign. (Ex: Environ$("username")) This will net you a small performance gain.
- You can retrieve all System Environment Variables using this function. (Not just username.) A common use is to get the "ComputerName" value to see which computer the user is logging onto from.
- I don't recommend it for most situations, but it can be occasionally useful to know that you can also access the variables with an index. If you use this syntax the the name of argument and the value are returned. In this way you can enumerate all available variables. Valid values are 1 - 255.
Sub EnumSEVars()
Dim strVar As String
Dim i As Long
For i = 1 To 255
strVar = Environ$(i)
If LenB(strVar) = 0& Then Exit For
Debug.Print strVar
Next
End Sub