Using variables in name references.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with 20 text boxes and their associated labels. They are
labeled Field1, Field2, ... and lblField1, lblField2,...
At the top of the form I have a combo box with the numbers from 1-20. I
would like to use the visible property of the labels and text boxes in order
to make visible the range that the user picks. I.E. if 3 is selected, then
Field1, Field2 and Field3 would be visible along with their association
labels, while the other ones would not be visible. I entered the following
code in the after update event of the combo box:
Private Sub cmbNumber_AfterUpdate()
Dim Number As Integer
Number = cmbNumber

For x = 1 To Number
Me.lblFieldx.Visible = True
Me.Fieldx.Visible = True
Next x

For x = (Number + 1) To 20
Me.lblFieldx.Visible = False
Me.Fieldx.Visible = False
Next x

End Sub

However, when I try to run the code I get an "invalid method or data member"
error. The problem is obviously my attempt to create a reference to a
control using a variable. Any light that could be shed on this subject would
be appreciated.
 
Jason

I seem to recall that making a text box.Visible = False also hides that text
box's label. Are your labels bound to their respective text boxes?

What happens if you only set the text box visibility?

Have you set a breakpoint in your code and discovered which command is
"breaking"?
 
How about something like:

For X = 1 To 20
Me("Field" & X).Visible = (cmbNumber <= X)
'Next line may not be needed if the label is attached to the control
Me("lblField" & X).Visible = (cmbNumber <= X)
Next X
 
The labels are not bound to the text boxes. I had them formated a specific
way and so just copied and pasted so it would keep the formating. Then I
just renamed them.

I did however take out of the code the label references and just left the
textbox code. It still brought up the same error: Method or data member not
found. And then when I click on debug Fieldx is highlighted in the code.

As for breaking... I am unfamiliar with how to do that.

Thanks
 
Back
Top