Object References via Coding

  • Thread starter Thread starter Roger Lord
  • Start date Start date
R

Roger Lord

I have a form with 20 labels on it among other things. The
data that goes into these labels comes from an array whose
number of elements far exceeds 20. Given that, I am using a
"Next Page" & "Previous Page" scheme so that I can look at
20 items at a time.

The names of the labels are: lblName01 ... lblName20

I want to be able to construct in code the name of each
label and then fill each one via its Caption property in a
"For I = 1 to 20" loop. I tried this by creating the name
of each label as a string variable called txtName (easy to
do), but my program bombs when I try to set the caption as
follows: txtName.Caption = MyArray(I). The error I get is
"Invalid qualifier" pointing to txtName.

I'm clearly doing something wrong. Can this be done? If
so, how?

Thanks,
Roger
 
I have a form with 20 labels on it among other things. The
data that goes into these labels comes from an array whose
number of elements far exceeds 20. Given that, I am using a
"Next Page" & "Previous Page" scheme so that I can look at
20 items at a time.

The names of the labels are: lblName01 ... lblName20

I want to be able to construct in code the name of each
label and then fill each one via its Caption property in a
"For I = 1 to 20" loop. I tried this by creating the name
of each label as a string variable called txtName (easy to
do), but my program bombs when I try to set the caption as
follows: txtName.Caption = MyArray(I). The error I get is
"Invalid qualifier" pointing to txtName.

use Me.Controls(txtname).caption

BTW: if you name your labels without the leading Zero (lblName1) you
cab use:
For I = 1 to 20
Me.Controls("lblName" &I).caption=MyArray(I)
Next I
 
Andi Mayer said:
use Me.Controls(txtname).caption

BTW: if you name your labels without the leading Zero (lblName1) you
cab use:
For I = 1 to 20
Me.Controls("lblName" &I).caption=MyArray(I)
Next I

With the leading zeroes, he can use:

For I = 1 to 20
Me.Controls("lblName" & Format(I, "00")).caption=MyArray(I)
Next I
 
Back
Top