Retrieving Active Directory User Name into Access 2003 table

  • Thread starter Thread starter shawkins84
  • Start date Start date
S

shawkins84

It seems like there would be an easy VBA call to do this, but I have not
found it yet...

I am trying to grab the Active Directory User Name (the user name that is
used to log into WinXP professional by the user) in order to populate the
user name in a transactional database. Is there some simple (or not simple)
VBA code that is out there to accomplish this? Perhaps something internal to
Access 2003?
 
hi,

I am trying to grab the Active Directory User Name (the user name that is
used to log into WinXP professional by the user) in order to populate the
user name in a transactional database. Is there some simple (or not simple)
VBA code that is out there to accomplish this? Perhaps something internal to
Access 2003?

Option Compare Database
Option Explicit

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

Public Function GetUserName()

Const BUFFER_SIZE As Long = 100

Dim buffer As String
Dim result As Long
Dim size As Long

buffer = String(BUFFER_SIZE + 1, 0)
size = BUFFER_SIZE + 1
result = GetUserNameA(buffer, size)
If result <> 0 Then
GetUserName = Left$(buffer, size - 1)
End If

End Function


mfG
--> stefan <--
 
Found this listing:

I always cringe when people suggest using an environment variable for this
purpose, given how easy it is to reset an environment variable.

Far safer, in my opinion, is to use the GetUserName API call. See
http://www.mvps.org/access/api/api0008.htm at "The Access Web" for a
complete sample.

This works in Access, Word and Excel, as you would expect.
 
Word of advice, I would use a global variable and encapsulate any code that
queries AD for any user information. I found out the hard way that there can
be a noticable performance hit while Access is grabbing the information from
AD. By placing the value in a global variable and encapsulating the code,
you can eliminate constantly hitting AD.

Function getWindowsUserId()

'Set the value if we don't have it; avoids having to set it when the
application loads and
'resets it in the event that the value is lost for any reason.
If Len(gWindowsUserId) = 0 Then setWindowsUserId

getWindowsUserId = gWindowsUserId

End Function

Sub setWindowsUserId()

gWindowsUserId = Left(fOSUserName, Len(fOSUserName()))

End Sub

setWindowsUserId has other code in it that warrants it being its own SUB
which has been cut out for this post.

David
 
hi David,

Word of advice, I would use a global variable and encapsulate any code that
queries AD for any user information. I found out the hard way that there can
be a noticable performance hit while Access is grabbing the information from
AD. By placing the value in a global variable and encapsulating the code,
you can eliminate constantly hitting AD.
While this is true, it does not apply in this case, as the function
mentioned by Jack and myself does not query the AD.

They use the Win API function GetUserNameA which only queries the local
computer for this information.



mfG
--> stefan <--
 
I used the same function from mvps.org and noticed a remarkably slow-down
once the App was running on the network versus my standalone machine.
 
I got this to work in Excel, but I'm not sure how/where to put it in access. I am trying to hide it on a form. Any suggestions?
 
Back
Top