Link to home
Start Free TrialLog in
Avatar of bkosterman
bkosterman

asked on

How do I get the user's Windows log on ID in VBA?

I am trying to build a module to look up the current user's log in name so that I can record it on records that are changed.

I tried the CurrentUser() function, but it just returns "Admin" because I am admin on my computer, but that's not my login name.

I also found this code on the Web, but it also returns Admin
Dim doc As Document
  Dim strUserName As String
  Dim dbMCL1 As Database
 
  'Get current user name
  Dim varUserID As String
  Set dbMCL1 = DBEngine(0)(0)
  Set doc = dbMCL1.Containers("Tables").Documents(0)  'CurrentDB doesn't work
  varUserID = doc.UserName
  Set dbMCL1 = Nothing

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ENVIRON("ComputerName") returns the DeviceID ... fyi.

mx
Try This:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function UserName() As String

    Dim strUserName As String
   
    strUserName = String(255, Chr(0))
    GetUserName strUserName, 255
   
    UserName = Left(strUserName, InStr(strUserName, Chr(0)) - 1)
   
    MsgBox UserName
End Function
Avatar of bkosterman
bkosterman

ASKER

You da man!  Thanks.