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?
Rewrite for Excel:
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.