How to call a module from a user form?

I have this code located in Module5 of my macro. The code is :

 

     ' 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

This code works by itself in Module5

 

I need to call Module5 from my user form code (UserForm1). What is the syntax to call a module from a userform?

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

You don't need to refer to Module5. You can call GetUserName in the code behind the userform, e.g. in the On Click event procedure of a command button on the userform:

 

Private Sub cmdTest_Click()
    Call GetUserName
End Sub

---
Best wishes, HansV
https://www.eileenslounge.com

2 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi

Hans

The code will return nothing as it is not a function ...

Option Explicit
     ' 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

   Public Function fnGetUserName() As String
      ' 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.
      fnGetUserName = lpUserName
End Function


sub test
        fnGetUserName
end sub

If this response answers your question then please mark as Answer.
GMT+1 - Europe

4 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi

from my code

   msgbox "Hello "  &  fnGetUserName

Regards
JY
If this response answers your question then please mark as Answer.
GMT+1 - Europe

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi

Please do make the difference between a sub and a function

Option Explicit

Sub test1()
    MsgBox Date
    userform.Name = getMydate
End Sub

Function fnMydate() As Date
Dim temp As Date
temp = Date
getMydate = temp
End Function

Public Sub GiveMydate()
Dim temp As Date
temp = Date
MsgBox temp
End Sub

HTH
Regards
JY

HTH

If this response answers your question then please mark as Answer.
GMT+1 - Europe

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks for your quick reply. The code runs now, but the value 'lpUserName' is not being returned to the UserForm1 code. I need lpUserName so I can display it on a worksheet.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks for your quick reply. The code runs now, but the value 'lpUserName' is not being returned to the UserForm1 code. I need lpUserName so I can display it on a worksheet.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks for your quick reply. The code runs now, but the value 'lpUserName' is not being returned to the UserForm1 code. I need lpUserName so I can display it on a worksheet.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi

Try to use the fi-unction name , i.e. fnGetUserName , and not lpUserName
this is demonstrated int the function I provided.

Sub Userform_Activate()
me.caption = fnGetUserName( )
End sub

HTH
Regards
JY



If this response answers your question then please mark as Answer.
GMT+1 - Europe

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I tried it but it still isn't passing the variable. In the form I am currently using this statement:

 

.Cells(lastRow, 56).Value = .Cells(lastRow, 56) & Application.UserName

 

Application.UserName returns the Office username, which isn't always accurate. We need to return the Windows login username, which the function does ('lpUserName'). so how to I get lpUserName into the form? The desired reseult would be something like this:

 

.Cells(lastRow, 56).Value = .Cells(lastRow, 56) & lpUserName.

 

I tried using lpUserName and that doesn't work either.

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Jean-Yves Tfelt has shown you:

 

.Cells(lastRow, 56).Value = .Cells(lastRow, 56) & fnGetUserName

 

where fnGetUserName is as shown in his first reply in this thread.

---
Best wishes, HansV
https://www.eileenslounge.com

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

 
 

Question Info


Last updated October 23, 2024 Views 13,570 Applies to: