# Visual Basic > Visual Basic FAQs >  Classic VB - How can I pass a control (textbox/listbox/..) to a sub or function?

## si_the_geek

Rather than write the same code for different controls, it is better to use a Sub or Function to do the work, and simply pass the control as a parameter.  This not only makes your code shorter (and so easier to write), but it also means that any changes you make will be changed for all of the controls at the same time (saving the time of repeating the change).


Let's pretend that you have code like the following for several textboxes _(it highlights all of the text when the textbox gets the focus)_:


```
Private Sub Text1_GotFocus()
  Text1.SelStart = 0
  Text1.SelLength = Len(Text1.Text)
End Sub
```

..not a long example, but it will prove the idea!

You can easily convert this to a sub, which works only on one control (Text1):


```
Public Sub HighlightText
  Text1.SelStart = 0
  Text1.SelLength = Len(Text1.Text)
End Sub

Private Sub Text1_GotFocus()
  HighlightText
  'or:
  'Call HighlightText
End Sub
```

..so how can you make this work for any textbox?   

You need to set up a parameter to the sub/function with the relevant data type (in this case, TextBox), and replace the specific control name (_Text1_) inside the sub with the name of the parameter, like this:


```
Public Sub HighlightText(TheTextBox As TextBox)
  TheTextBox.SelStart = 0
  TheTextBox.SelLength = Len(TheTextBox.Text)
End Sub
```

When you have this, you need to modify the code that calls it to also specify the textbox to work with:


```
Private Sub Text1_GotFocus()
  HighlightText Text1 
  'or:
  'Call HighlightText (Text1)
End Sub

Private Sub Text2_GotFocus()
  HighlightText Text2
End Sub
```

Note that what we are passing to the sub is the _control_ (or _object variable_) Text1, rather than the _string_ "Text1" or the text that the textbox contains.



*What if the sub/function is in a different form/module to the control?*
Well it doesn't actually matter.. you still use exactly the same code as above.  This is because you are passing an object (a reference to the control itself), rather than the name of the control.

The control knows which form it is on, so you do not need to specify the form (just like when you use it within its own form).  If you try to pass the form aswell, you may end up with code like this:


```
Public Sub HighlightText(TheForm as Form, TheTextBox As TextBox)
  TheForm.TheTextBox.SelStart = 0  'error!!
```

Why doesn't this work?  Well the code is looking for a control which is called "TheTextBox" on the form you specified, not a control with the same name as the control you passed.  There is no need whatsoever for the form to be passed, simply use the method which was shown above.


*What if the control is in a different form to the code that calls the sub/function?*
This is the only time that something needs to be changed - now you need to specify the form, but only on the line that calls the sub/function, as that is what specifies the object to use.

Assuming that the control is on Form2, the code would be like this:


```
  HighlightText Form2.Text1 
  'or:
  'Call HighlightText (Form2.Text1)
```

----------

