Get User Name Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trixxnixon
    New Member
    • Sep 2008
    • 98

    #1

    Get User Name Function

    so im using the function below in a module. This works, when i press the run button the msg box pops up with my correct user name.

    how could i get a field in a form to populate with the output from this function.

    Code:
     ' Declare for call to mpr.dll.
       Declare Function WNetGetUser Lib "mpr.dll" _
          Alias "WNetGetUserA" (ByVal lpName As String, _
          ByVal lpUserName As String, lpnLength As Long) As Long
    
       Const NoError = 0       'The Function call was successful
    
       Sub GetUserName()
    
          ' Buffer size for the return string.
          Const lpnLength As Integer = 255
    
          ' Get return buffer space.
          Dim status As Integer
    
          ' For getting user information.
          Dim lpName, lpUserName As String
    
          ' Assign the buffer size constant to lpUserName.
          lpUserName = Space$(lpnLength + 1)
    
          ' Get the log-on name of the person using product.
          status = WNetGetUser(lpName, lpUserName, lpnLength)
    
          ' See whether error occurred.
          If status = NoError Then
             ' This line removes the null character. Strings in C are null-
             ' terminated. Strings in Visual Basic are not null-terminated.
             ' The null character must be removed from the C strings to be used
             ' cleanly in Visual Basic.
             lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
          Else
    
             ' An error occurred.
             MsgBox "Unable to get the name."
             End
          End If
    
          ' Display the name of the person logged on to the machine.
          MsgBox "The person logged on this machine is: " & lpUserName
    
       End Sub
  • DonRayner
    Recognized Expert Contributor
    • Sep 2008
    • 489

    #2
    Change
    Code:
    Sub GetUserName()
    .....
    End Sub
    to this

    Code:
    Function GetUserName() as string
    .....
    End Function
    After Line 31 add

    Code:
    GetUserName = lpUserName
    Change Line 35 to

    Code:
    GetUserName = "Unable to get the name"
    Remove Lines 36,39-41

    Your completed function will look like this

    Code:
       Function GetUserName()
     
          ' Buffer size for the return string.
          Const lpnLength As Integer = 255
     
          ' Get return buffer space.
          Dim status As Integer
     
          ' For getting user information.
          Dim lpName, lpUserName As String
     
          ' Assign the buffer size constant to lpUserName.
          lpUserName = Space$(lpnLength + 1)
     
          ' Get the log-on name of the person using product.
          status = WNetGetUser(lpName, lpUserName, lpnLength)
     
          ' See whether error occurred.
          If status = NoError Then
             ' This line removes the null character. Strings in C are null-
             ' terminated. Strings in Visual Basic are not null-terminated.
             ' The null character must be removed from the C strings to be used
             ' cleanly in Visual Basic.
             lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
            GetUserName = lpUserName
    
          Else
     
             ' An error occurred.
             GetUserName = "Unable to get the name."
     
          End If
    
       End Function
    After this you can populate a textbox with the following, Replacing TextBoxName with the actual name of the text box.

    Code:
    Me.TextBoxName = GetUserName()

    Comment

    • trixxnixon
      New Member
      • Sep 2008
      • 98

      #3
      sweet, thanks for looking at that.
      it works like a charm!

      thanks again!!

      Comment

      • DonRayner
        Recognized Expert Contributor
        • Sep 2008
        • 489

        #4
        You're quite welcome.

        Don

        Comment

        Working...