If not already done so, create a new module in the Modules section.
Paste this statement into the General Declarations section of the module:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Paste this code:
Function fWin2KUserName() As String
Dim lngLength As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLength = 255
lngX = apiGetUserName(strUserName, lngLength)
If lngX <> 0 Then
fWin2KUserName = Left$(strUserName, lngLength - 1)
Else
fWin2KUserName = "unknown"
End If
End Function
You can run this function (fWin2KUserName) from anywhere within your application. The best way is to set some variable to be equal to the function: Dim strUser as String
strUser = fWin2KUserName
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.