1

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?

0m3r
  • 12,286
  • 15
  • 35
  • 71
  • 1
    Perhaps you are aware of this, but it is unclear from your question. Environ("username") should return the user name of the logged in user, not the person who's machine it is.... – OpiesDad Feb 10 '16 at 20:17
  • I don't understand what's going on. If you open a command prompt window on that machine and ask for `echo %username%`, does that give you a different name than you get from the VBA `environ("username")` ? – HansUp Feb 10 '16 at 21:07
  • 2
    In the Access Immediate window, what name does this show you? `Debug.Print CreateObject("WScript.Network").Username` – HansUp Feb 10 '16 at 22:38

1 Answers1

0

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

0m3r
  • 12,286
  • 15
  • 35
  • 71