Author: Calvin Smith
http://www.CalvinSmithSoftware.com/codedisk/sneakpeek.htm

The following code will allow a developer to programmatically determine the current user name.

'We need the following API declaration first
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function ReturnTheUserName() As String
' ---------------------------------------------------------------------------------
' Author: Calvin Smith
' Environment(s): VBA / Visual Basic (32-bit)
' ---------------------------------------------------------------------------------
'
'   *****************************************
'   * Courtesy code from my CodeDisk© product
'   *****************************************
'

On Error GoTo ErrorHandling_Err

    ' -------------------------------------------------
    ' Purpose: Determine the username
    '
    ' Accepts: Nothing
    '
    ' Returns: The logged on username
    '
    ' Example usage: strRetVal = ReturnTheUserName()
    ' -------------------------------------------------

   
Dim strUserNameBuffer As String * 1024

'Get the user name from our API
GetUserName strUserNameBuffer, Len(strUserNameBuffer)

'Trim everything after the first null character.
ReturnTheUserName = Left(strUserNameBuffer, (InStr(1, strUserNameBuffer, vbNullChar)) - 1)

ErrorHandling_Err:
    If Err Then
        'Trap your error(s) here, if any!
    End If
End Function