The creation and customization of database applications using Microsoft Access
“I can't get this to work” is too vague. What do you see that we don’t.
I usually recommend the code at the Access Web.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I can't get this to work hopefully there are answers already to this ?
The creation and customization of database applications using Microsoft Access
“I can't get this to work” is too vague. What do you see that we don’t.
I usually recommend the code at the Access Web.
AI answer
Environ("USERNAME") is the VBA function used to return the value of the Windows environment variable for the signed-in user.
Key points:
Environ(envstring) returns the string associated with an operating system environment variable."").Example in VBA:
MsgBox Environ("USERNAME")
If the goal is the Access database user instead of the Windows sign-in name, use CurrentUser instead:
MsgBox CurrentUser
CurrentUser returns the current database user account name. If workgroup security is not established, it returns the default user account, Admin.
If the goal is specifically the network or Windows logon name, Access documentation also provides an approach using a user-defined function that calls the GetUserNameA Windows API.
Where you put it matters, because Environ behaves differently in VBA and in expressions. The usual cases:
1. In a table field's Default Value (table design view): that doesn't work. Table-level defaults only accept a few built-in functions, not Environ or your own functions. Set the value in the form instead (see 3).
2. In a query, a text box Control Source or a form control's Default Value (=Environ("USERNAME")): Environ is one of the functions Access blocks in expressions when it runs in sandbox mode. If the database isn't trusted, you get Undefined function 'Environ' in expression or #Name?. So first put the file in a Trusted Location (File > Options > Trust Center > Trust Center Settings > Trusted Locations). Then use a small wrapper in a standard module (not a form module). It works from queries, control sources and code alike, and you can change how you get the name later in one place:
Public Function WinUser() As String
WinUser = Environ$("USERNAME")
End Function
Then use =WinUser() as the control's Default Value or Control Source, or WinUser() in a query (SELECT ... WHERE CreatedBy = WinUser()).
3. Stamping the user on new records (the most common goal): do it in the form:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me!CreatedBy = WinUser()
End Sub
To check the basics, type ? Environ$("USERNAME") in the Immediate window (Ctrl+G). If that prints your login, the function itself works and the problem is one of the cases above.
As Daniel said, the USERNAME variable can be changed by the user before starting Access. Fine for display or logging, not for permissions: there, use the Windows API (GetUserNameW) or CreateObject("WScript.Network").UserName.