Environ("username") returning wrong user names

1.8k views Asked by At

When I execute my Access application across alternating sessions, the function Environ("username") returns the user name of a person in my department other than the actual user whose machine I am then currently using to execute the code.

No other type of user name manipulation occurs throughout the program.

Any idea how this is possible?

1

There are 1 answers

0
0m3r On

This uses Windows API functions to return the name of the user who is currently logged in.

'// API Declarations
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) _
  As Long


Function UserName() As String
    '// Returns the name of the logged-in user
    Dim Buffer As String * 100
    Dim BuffLen As Long
    BuffLen = 100
    GetUserName Buffer, BuffLen
    UserName = Left(Buffer, BuffLen - 1)
    'MsgBox UserName
End Function

Retrieving Logged-in User Name