Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use variable to refer to a control?

Status
Not open for further replies.

megmoo75

Programmer
Jun 14, 2003
40
0
0
US
I would like to know if there's a way to use a variable to refer to a control. For example, if I have a text box named txtFirstName and I want to set the text field to be "John", I would normally do:

Let txtFirstName.Text = "John"

What I would like to do is this, where xxx is some function that forces VB to know that it should treat the variable string as a reference to a control:

Dim strControlName as string
Let strControlName = "txtFirstName.Text"
Let xxx(strControlName) = "John"


Does anyone know if this is possible?

Thanks in advance.
 
After a bit more searching, I found the answer to my own question in thread222-633372 courtesy of jonathanmb:

Controls("txtField" & i).Text = "Blah"
 
You could write a function to return the control by name. I would also pass in the form as a parameter so it works for any form.

Function fGetControlByName(frmSource As Form, _
Byval strName As String) As Control

Dim objCtl As Control

For Each objCtl In frmSource.Controls
If objCtl.Name = strName Then
Set fGetControlByName = objCtl
Exit For
End If
Next

Set objCtl = Nothing

End Function

The you could set the textbox text with

fGetControlByName(Me, "txtFirstName").Text = "John Smith"

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top