Discussion Locked

Determining NT Username in Access

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to be able to determine the NT username of whomever uses a form in my database. I know it can be done, but I can't remember how. This has to be the NT username and not the user that is using the system.mdw. Any additional information, likehow to take advantage of any Active Directory information would also be useful.

Thanks!

4 replies

by christian.cloutier

Use this code in a code module in Access:

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

Public Function GetUser() As String

Dim lRetVal As Long
Dim nSize As Long
Dim lpBuffer As String * 256

lpBuffer = String(256, 0)
nSize = Len(lpBuffer) - 1
lRetVal = GetUserName(lpBuffer, nSize)

If lRetVal <> 0 Then
GetUser = Left$(lpBuffer, nSize - 1)
End If

End Function

You could also add code to check GetLastError() if lRetVal is 0.

by sixfourtykilo

Beautiful!! Exactly what I needed! Enjoy the points! Do you have any idea if Active Directory info can be pulled (like Full name, etc)? Email me!

Thanks!

This discussion is closed to new replies.