How do you find out the Windows User Name from VBA

We log onto our network with a three letter user name e.g. ADG. Can I return the user name of the person opening my Access Database with VBA

I want to customise the options available to the user without using the security features built into Access.



Answer this question

How do you find out the Windows User Name from VBA

  • BLOX

    Many thanks Carbo,

    Environ$("Username") returned "adg" as expected. Thanks also Peter, but Application.CurrentUser returned "Admin", which was not the user I was seeking this time.

    While searching on Microsoft.com I also found the below code which works, but I prefer the simple approach above.

    ' Declare for call to mpr.dll.
    Declare Function WNetGetUser Lib "mpr.dll" _
    Alias "WNetGetUserA" (ByVal lpName As String, _
    ByVal lpUserName As String, lpnLength As Long) As Long

    Const NoError = 0 'The Function call was successful

    Sub GetUserName()

    ' Buffer size for the return string.
    Const lpnLength As Integer = 255

    ' Get return buffer space.
    Dim status As Integer

    ' For getting user information.
    Dim lpName, lpUserName As String

    ' Assign the buffer size constant to lpUserName.
    lpUserName = Space$(lpnLength + 1)

    ' Get the log-on name of the person using product.
    status = WNetGetUser(lpName, lpUserName, lpnLength)

    ' See whether error occurred.
    If status = NoError Then
    ' This line removes the null character. Strings in C are null-
    ' terminated. Strings in Visual Basic are not null-terminated.
    ' The null character must be removed from the C strings to be used
    ' cleanly in Visual Basic.
    lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
    Else

    ' An error occurred.
    MsgBox "Unable to get the name."
    End
    End If

    ' Display the name of the person logged on to the machine.
    MsgBox "The person logged on this machine is: " & lpUserName

    End Sub


  • PKH

    This will provide you with the information you desire. I use it often in the default value of a field.

    (Environ$("Username"))


  • GregVance

    Hi

    Access isn't my strong suite, but isn't there something like ...

    Application.CurrentUser

    that gives you the information you need

    Peter Mo


  • How do you find out the Windows User Name from VBA