tags:

views:

2434

answers:

1

How to pass Form TextBox reference to method?

Following code is issuing exception.

Private Sub DoSmthWithTextBox(ByRef txtBox as TextBox)
    txtBox.BackColor = vbRed
End Sub

Private Sub TextBox1_Change()
    DoSmthWithTextBox Me.TextBox1
End Sub

Problem appears when'DoSmthWithTextBox Me.TextBox1' passes string from TextBox1 insead of object reference. How to pass TextBox object to method DoSmthWithTextBox?

Thanx

+5  A: 

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.

Remou
+1 for knowing the difference. ;-) Can you write a short explanation?
Tomalak
YES!!!!Upvote from me ;-)This works for me...
Jox