Grabbing windows user name

Johnny Drama

In need of beer...
Local time
Today, 14:56
Joined
Dec 12, 2008
Messages
211
Hi all,

I've got some tables that certain individuals will have access to so they can update the data. The DB back end is SQL server using windows authentication, and I was wondering if anyone knows if it is possible to grab the users network name and then drop that info into a field when they make a change to the data so that we can audit who made the changes. If it's possible the next question is how?

Thanks in advance.
 
Howzit

I have used this in the past. There are other ways, but I do not have these at hand.

A way to call it is

Code:
dim strSendWho as string

strSendWho = GetUserName_TSB

Put this in a module
Code:
Declare Function TSB_API_GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function GetUserName_TSB() As String
' Comments  : Retrieves the name of the user logged into Windows
' Parameters: none
' Returns   : string user name
'
    Dim lngLen As Long
    Dim strBuf As String

    Const MaxUserName = 255

    strBuf = Space(MaxUserName)

    lngLen = MaxUserName

    If CBool(TSB_API_GetUserName(strBuf, lngLen)) Then
        GetUserName_TSB = Left$(strBuf, lngLen - 1)
    Else
        GetUserName_TSB = ""
    End If

End Function
 
Just because I don't know, am too lazy to test it and you might know the answer off the top of your head.

How does Environ("username") respond if you've run as when you start access? Is it reporting the windows username or the account that is currently running Access?
 
no idea - a few posters have previously pointed out that environ can be "tricked" to give the wrong answer. depends how critical it is.

offhand i think another environ setting gives the machine name, so you could store that as well, if it mattered
 
Environ('username") is not a robust solution because environment variables are very easily changed. The API function is so simple I don't see the why any developer would ever choose environ rather than get the actual username.
 

Users who are viewing this thread

Back
Top Bottom