Solved

How do I assign series of controls to variable in an MS Access report?

Posted on 2013-02-05
4
240 Views
Last Modified: 2013-02-06
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!
0
Comment
Question by:CRMEHC
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
You can't refer to controls like that and you only use Set with objects, "lbl_stu" & i is a string.

Try this.
Me.Controls("lbl_stu" & i).Caption = DLookup("[stu_name]", "t_students", "[stu_auto_id] =" & i)

Open in new window

0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
You must use...

  Set ctrlStuLbl  = Me.Controls("lbl_stu" & i)
0
 

Author Closing Comment

by:CRMEHC
Comment Utility
Exactly what I need! Works perfectly! Thank you! And it's much cleaner without having to assign a variable first.
0
 

Author Comment

by:CRMEHC
Comment Utility
peter57r - Sorry, but your solution didn't work. I still get an error message.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Outlook Free & Paid Tools
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now