Reputation: 3680
Is there a way of passing Windows log in information to a string in Access? I need a way of automatically detecting a user on a multi-user database held in a central location. Thanks in advance.
Upvotes: 0
Views: 133
Reputation: 12210
Here is what I use on a domain network with Windows XP and Windows 7 machines. I'm not sure how it would work on a non-domain network or if you were on no network at all.
Public Function GetComputerUserName()
Dim objnet As Object
Set objnet = CreateObject("WScript.Network")
GetComputerUserName = objnet.UserName
Set objnet = Nothing
End Function
Upvotes: 2
Reputation: 24227
The most reliable way is to use a Windows API call:
'This line must go at top of module
Private Declare Function WNetGetUser& Lib "Mpr" Alias _
"WNetGetUserA" (lpName As Any, ByVal lpUserName$, lpnLength&)
Function GetUserName() As String
Const MaxNameLength As Long = 256
Dim ret As Long, UserName As String
UserName = Space(256)
ret = WNetGetUser(ByVal 0&, UserName, MaxNameLength)
If ret = 0 Then
'Success - strip off the null.
UserName = Left(UserName, InStr(UserName, Chr(0)) - 1)
Else
UserName = ""
End If
GetUserName = LCase$(UserName)
End Function
Upvotes: 1