Using For...Next with Controls

S

Suggy1982

I have got 3 label controls on a form; each label control has it own name
(list below)
- lbl_div_nbr_1
- lbl_div_name_1
- lbl_div_charge_1

These set of 3 labels are then repeats 3 times on the same form, with the
name being the only thing that changes e.g.
- lbl_div_nbr_2
- lbl_div_name_2
- lbl_div_charge_2
- lbl_div_nbr_3
- lbl_div_name_3
- lbl_div_charge_3

I would like to use a For….next statement to look through and set the values
on these labels and make them visible, rather than having to write out 9
separate statements to do this, but my code below isn’t working. It is
returning an error saying it cannot find “Forms!menu!lbl_div_nbr_x.Captionâ€

For x = 1 to 3
Forms!menu!lbl_div_nbr_x.Caption = “testâ€
Forms!menu!lbl_div_nbr_x.Visible = True

Forms!menu!lbl_div_name_x.Caption = “testâ€
Forms!menu!lbl_div_name_x.Visible = True

Forms!menu!lbl_div_charge_x.Caption = “testâ€
Forms!menu!lbl_div_charge_x.Visible = True
Next
 
D

Dirk Goldgar

Suggy1982 said:
I have got 3 label controls on a form; each label control has it own name
(list below)
- lbl_div_nbr_1
- lbl_div_name_1
- lbl_div_charge_1

These set of 3 labels are then repeats 3 times on the same form, with the
name being the only thing that changes e.g.
- lbl_div_nbr_2
- lbl_div_name_2
- lbl_div_charge_2
- lbl_div_nbr_3
- lbl_div_name_3
- lbl_div_charge_3

I would like to use a For….next statement to look through and set the
values
on these labels and make them visible, rather than having to write out 9
separate statements to do this, but my code below isn’t working. It is
returning an error saying it cannot find
“Forms!menu!lbl_div_nbr_x.Captionâ€

For x = 1 to 3
Forms!menu!lbl_div_nbr_x.Caption = “testâ€
Forms!menu!lbl_div_nbr_x.Visible = True

Forms!menu!lbl_div_name_x.Caption = “testâ€
Forms!menu!lbl_div_name_x.Visible = True

Forms!menu!lbl_div_charge_x.Caption = “testâ€
Forms!menu!lbl_div_charge_x.Visible = True
Next


Do it like this:

With Forms!menu
For x = 1 to 3

.Controls("lbl_div_nbr_" & x).Caption = “testâ€
.Controls("lbl_div_nbr_" & x).Visible = True

.Controls("lbl_div_name_" & x).Caption = “testâ€
.Controls("lbl_div_name_" & x).Visible = True

.Controls("lbl_div_charge_" & x).Caption = “testâ€
.Controls("lbl_div_charge_" & x).Visible = True

Next
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top