All pastes #1629963 Raw Edit

Access: Get Username

public text v1 · immutable
#1629963 ·published 2009-10-19 15:19 UTC
rendered paste body
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


Public Function GetCurrentUserName() As String
On Error GoTo Err_GetCurrentUserName
Dim lpBuff As String * 25
Dim ret As Long, Username As String
  ret = GetUserName(lpBuff, 25)
  Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
  GetCurrentUserName = Username & ""

Exit_GetCurrentUserName:
   Exit Function

Err_GetCurrentUserName:
       MsgBox Err.Description
       Resume Exit_GetCurrentUserName
End Function