Home | What's New | Articles | Code Downloads | Tool Box | Links | Code Snippets  

Network

Finding a Computer's Name

To find the computer's name place this into the General Declarations section of a form

'Declarations section:

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Use the following code to find the computer's name

    Dim strComputerName As String * 255
    Dim lngComputerNameLength As Long
    
    lngComputerNameLength = Len(strComputerName)
    If GetComputerName(strComputerName, lngComputerNameLength) <> 0 Then
        MsgBox Left$(strComputerName, lngComputerNameLength)
    Else
        MsgBox Err.LastDllError
    End If

Getting the Username of the Logged in user

The code to get the username of the logged in user is almost identical to the code used to find the computer's name.

'Place in General Declarations section:

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Use the following code to find the username.

    Dim strUserName As String * 255
    Dim lngUserNameLength As Long
    
    lngUserNameLength = Len(strUserName)
    If GetUserName(strUserName, lngUserNameLength) <> 0 Then
        MsgBox Left$(strUserName, lngUserNameLength)
    Else
        MsgBox Err.LastDllError
    End If