Getting the User ID from a PC

Sometimes it is useful to keep track of who is entering data or running functions in your database – if you want to automate an auditing feature, or log who has created a record, you will need to pull the User ID from the users operating system. You can do this by inserting the following function as a module, and then calling it as needed.

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

Function fOSUserName() As String
' Returns the network login name
 Dim lngLen As Long, lngX As Long
 Dim strUserName As String
 strUserName = String$(254, 0)
 lngLen = 255
 lngX = GetUserName(strUserName, lngLen)
 If lngX <> 0 Then
 fOSUserName = Left$(strUserName, lngLen - 1)
 Else
 fOSUserName = ""
 End If
End Function

You can then call the function in your vba code and assign it to a variable, or you can add it to the design of a form or table:

Inserting User ID into form

Leave a Reply

Your email address will not be published. Required fields are marked *