help with environ("username") in access database

James Speckmann 5 Reputation points
2026-09-26T23:15:19.0966667+00:00

I can't get this to work hopefully there are answers already to this ?

Microsoft 365 and Office | Access | Development

3 answers

Sort by: Most helpful
  1. Duane Hookom 27,035 Reputation points Volunteer Moderator
    2026-09-29T03:44:42.86+00:00

    “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.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. AI answer

    2026-09-26T23:15:30.18+00:00

    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.
    • If the variable is not found, it returns an empty string ("").
    • It is available in VBA, and is not available on Macintosh.
    • In Access, if the Microsoft Jet Expression Service is running in sandbox mode, this function is disabled.

    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.


    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    1 person found this answer helpful.
  3. Nacif Boudlal 0 Reputation points
    2026-12-01T09:52:40.6666667+00:00

    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.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.