Home > OS >  Passing a string to a Userform
Passing a string to a Userform

Time:11-05

I'm trying to build a simple Userform with a single text box with scroll bars for informational messages.

Unfortunately, I'm getting a Run-time Error 438, Object doesn't support this properly or method error.

My testing code is simple 'three different attempts

Userform including different attempts

 Dim inString As String
 Sub txtBoxVal(passedStr As String)
    inString = passedStr
    MyMessageBox = inString
 End Sub

 public Property get txtBoxVal2(passedStr As String) as string
    inString = passedStr
    MyMessageBox = inString
 End Sub

 Public Sub update()

    MyMessageBox.Text = publicStrVarDeclaredInFunction
 End Sub

 Private Sub UserForm_Initialize()

 End Sub

Sheet1 Code

Public publicVar As String

Private Sub userformTest()
Dim myForm As UserForm
Dim testString As String

testString = "This is a test string"

    Set myForm = New MyUserForm
    With myForm
        ' My 3 different way attempts
        Call .txtBoxVal(testString)
        .update publicVar
        Call .txtBoxVal2(testString)
        .Show

    End With
End Sub

Anyone have any insights where I'm making my mistake?

CodePudding user response:

Assuming that your Userform's name is MyUserForm and MyMessageBox is the TextBox control:

  1. You should Dim myForm As MyUserForm instead of Dim myForm As UserForm.

  2. Your update sub does not have an argument but you passed publicVar in .update publicVar, you will need to declare the argument in your update sub like this:

Public Sub update(argNewText As String)
   MyMessageBox.Text = argNewText
End Sub
  1. Call .txtBoxVal2(testString) won't work for 2 reason:

    a) txtBoxVal2 is a Property, not a Sub so you do not use Call. You assign value to txtBoxVal2 property by myForm.txtBoxVal2 = "new value"

    b) txtBoxVal2 is a Get property so you can only retrieve txtBoxVal2 value, in order to allow txtBoxVal2 be assigned a value, you have to change to Let:

Public Property Let txtBoxVal2(passedStr As String)
    inString = passedStr
    MyMessageBox = inString
End Property

=====================

I personally prefer to use Let property approach in this case so your code will look something like this:

Sheet1

Private Sub userformTest()
    Dim testString As String
    testString = "This is a test string"

    Dim myForm As MyUserForm
    Set myForm = New MyUserForm
    myForm.MessageText = testString
    
End Sub

MyUserForm

Private inString As String

Public Property Let MessageText(passedStr As String)
    inString = passedStr
    MyMessageBox.Text = inString
End Property

CodePudding user response:

Try this:
Make a simple UserForm called 'MyMsgBox' with a textbox called 'TextBox1' and a command button called 'CmdOK'.

Put this code into the UserForm:

Option Explicit

''' On form show:
Private Sub UserForm_Initialize()

''' Put text into textbox
    TextBox1 = pstMessage
    
'''BEEP (optional)
    Beep

End Sub

''' Close form on OK
Private Sub CmdOK_Click(): Unload Me
End Sub

Put this code in any Standard Module:

Option Explicit

    Public pstMessage$

Sub ShowMsg()

''' Put the message into pstMessage
    pstMessage = "This is a test string"

''' Run MyMsgBox
    MyMsgBox.Show

End Sub
  • Related