CRMEHC
asked on
How do I assign series of controls to variable in an MS Access report?
I have a series of labels in my MS Access report named in sequence as lbl_stu1, lbl_stu2, lbl_stu3, etc. When the report Activates, I would like to assign each label to a Control variable and loop through the number of records in a table and assign a text value in each control with a corresponding record number in the table.
I thought this would work:
Dim ctrlStuLbl As Control
ctrlStuLbl = Nothing
' loop through all student records
' -------------------------- ---------- ---------- ----
For i = 1 To 20
Set ctrlStuLbl = ("lbl_stu" & i)
ctrlStuLbl.Caption = DLookup("[stu_name]", "t_students", "[stu_auto_id] =" & i)
Next i
But I get an error on: Set ctrlStuLbl = ("lbl_stu" & i)
I'm sure the answer is simple, but Access help is as useless as usual, and an internet search keeps leading me down the wrong path.
Any help is greatly appreciated in advance!
Thanks!
I thought this would work:
Dim ctrlStuLbl As Control
ctrlStuLbl = Nothing
' loop through all student records
' --------------------------
For i = 1 To 20
Set ctrlStuLbl = ("lbl_stu" & i)
ctrlStuLbl.Caption = DLookup("[stu_name]", "t_students", "[stu_auto_id] =" & i)
Next i
I'm sure the answer is simple, but Access help is as useless as usual, and an internet search keeps leading me down the wrong path.
Any help is greatly appreciated in advance!
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Exactly what I need! Works perfectly! Thank you! And it's much cleaner without having to assign a variable first.
ASKER
peter57r - Sorry, but your solution didn't work. I still get an error message.
Set ctrlStuLbl = Me.Controls("lbl_stu" & i)