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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use varilable to reference control name

Status
Not open for further replies.

JordanCN2

IS-IT--Management
Feb 7, 2011
39
US
I am trying to loop through the controls on a subform and take all the values from the text boxes and set the same named text boxes on the main form to the same value. I would like to do it by using a variable

The code below gives me an error referencing Me.Parent!strParentControlName because it is referenceing the control as "strParentControlName" instead of as the variable strParentControlName

How do I get Me..Parent!strParentControlName to use strParentControlName as a variable.

Private Sub Form_Click()

Dim ctlCurrent As Control
Dim strParentControlName As String

For Each ctlCurrent In Me.Controls
If ctlCurrent.ControlType = acTextBox Then
strParentControlName = ctlCurrent.Name
Me.Parent!strParentControlName = ctlCurrent.value
End If
Next ctlCurrent

End Sub
 
to refer to a control by variable you need to use the form's control collection.

me.parent.controls(someVariable)

 
I'd replace this:
Me.Parent!strParentControlName = ctlCurrent.value
with this:
Me.Parent.Form.Controls(strParentControlName) = ctlCurrent.Value

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top