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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
You da man! Thanks.
mx