Forum Discussion

Ian_Ryan's avatar
Ian_Ryan
Copper Contributor
Jan 07, 2020
Solved

Print to a Text Box on a User Form from VBA Module

Hello All, I am in need of some expert guidance to an issue I have.

 

Basically, I have created a User Form with some buttons on the form that trigger different Modules of VBA. This all works fine but as some of the VBA Modules take a long time to process the various tasks that they do, I need to show the user what is actually happening in the back-round via some text in a text box. I added a text box to the User form, I added a new button to the User form and I was able to write text to the Text Box using the following code:-

 

Public Sub CommandButton5_Click()
Dim strText As String
Dim blnClear As Boolean
blnClear = True

strText = "Operation Completed"
Call WriteToLogText(strText, 1)
End Sub
Public Sub WriteToLogText(strText As String, Optional blnClear As Boolean = False)

textlog.SetFocus
If blnClear = True Then
textlog = strText
Else
textlog = textlog.Text & Chr(13) & Chr(10) & strText
End If
End Sub

 

When I tried to use the above code in other modules it does not work as the code fails on the statement "textlog.SetFocus". I am assuming that this is because the code is running external to the user form.

My question is can I pass text into the named text box on the User Form from other modules.

 

Any assistance would be greatly appreciated.

 

Regards

Ian

 

  • This means you need to refer to the userform instance that is displaying to access that routine. So if you have an object variable named "MyUF" which points to the currently running userform you can call the sub like so:
    MyUF.WriteToLogText "LogText", False

4 Replies

    • JKPieterse's avatar
      JKPieterse
      Silver Contributor
      This means you need to refer to the userform instance that is displaying to access that routine. So if you have an object variable named "MyUF" which points to the currently running userform you can call the sub like so:
      MyUF.WriteToLogText "LogText", False

Resources