Reputation: 7172
How do I pass a form's TextBox
object to the a method?
The following code is issuing an exception.
Private Sub DoSmthWithTextBox(ByRef txtBox as TextBox)
txtBox.BackColor = vbRed
End Sub
Private Sub TextBox1_Change()
DoSmthWithTextBox Me.TextBox1
End Sub
The problem appears when DoSmthWithTextBox Me.TextBox1
passes the String
from TextBox1
instead of the object reference.
How do I pass the TextBox
object to the DoSmthWithTextBox
method?
Upvotes: 5
Views: 13348
Reputation: 91376
Rewrite for Excel:
Private Sub DoSmthWithTextBox(txtBox As MSForms.TextBox)
txtBox.BackColor = vbRed
End Sub
As far as I know, this is because Excel has an object textbox that is a shape, whereas userforms use the ActiveX control textbox, so you need an explicit reference to the MSForms library.
Upvotes: 13