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:
You should
Dim myForm As MyUserForminstead ofDim myForm As UserForm.Your
updatesub does not have an argument but you passedpublicVarin.update publicVar, you will need to declare the argument in yourupdatesub like this:
Public Sub update(argNewText As String)
MyMessageBox.Text = argNewText
End Sub
Call .txtBoxVal2(testString)won't work for 2 reason:a)
txtBoxVal2is aProperty, not aSubso you do not useCall. You assign value totxtBoxVal2property bymyForm.txtBoxVal2 = "new value"b)
txtBoxVal2is aGetproperty so you can only retrievetxtBoxVal2value, in order to allowtxtBoxVal2be assigned a value, you have to change toLet:
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
