Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the name of the user logged into the PC ?

How to

How do I get the name of the user logged into the PC ?

by  chris153  Posted    (Edited  )
The following code supplies the login name of the user that's logged onto the PC. Copy it into a module and call the GetUserName_TSB function.

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

Public Function GetUserName_TSB() As String
On Error GoTo error_handler
Dim lngLen As Long
Dim strBuf As String
Const MaxUserName = 255
strBuf = Space(MaxUserName)
lngLen = MaxUserName
If CBool(TSB_API_GetUserName(strBuf, lngLen)) Then
GetUserName_TSB = Left$(strBuf, lngLen - 1)
Else
GetUserName_TSB = ""
End If
Exit Function
error_handler:
Error_Recorder
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top