I have used a function from Dev Ashish's website ( in my access project to get the user's network login name. The code is as follows:
Private Declare Function apiGetUserName 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 = apiGetUserName(strUsername, lngLen)
If (lngX > 0) Then
fosUserName = Left$(strUsername, lngLen - 1)
fosUserName = LCase(fosUserName)
Else
fosUserName = vbNullString
End If
End Function
Basically, I use this for user tracking, and keep a record of whic user had added records to the database. This all works 100%.
In order to do this, I reference the fosUserName function in various bits of code and in many queries.
My question is this - would I be better off declaring a global variable and assigning fosUserName to this variable right at the start of the application opening. This means that I would not have to keep running the fosUserName function every time I needed to get the username. I would assume that this is a more efficient way of doing things, but not sure if its worth all the trouble.
Your comments/thoughts are appreciated.
Private Declare Function apiGetUserName 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 = apiGetUserName(strUsername, lngLen)
If (lngX > 0) Then
fosUserName = Left$(strUsername, lngLen - 1)
fosUserName = LCase(fosUserName)
Else
fosUserName = vbNullString
End If
End Function
Basically, I use this for user tracking, and keep a record of whic user had added records to the database. This all works 100%.
In order to do this, I reference the fosUserName function in various bits of code and in many queries.
My question is this - would I be better off declaring a global variable and assigning fosUserName to this variable right at the start of the application opening. This means that I would not have to keep running the fosUserName function every time I needed to get the username. I would assume that this is a more efficient way of doing things, but not sure if its worth all the trouble.
Your comments/thoughts are appreciated.