UserForm Query

D

Derek Dowle

UserForm Query

I have a User Form which is used to collect monthly cost information for a
five year period for a database. The start month and year of the five year
period will vary from record to record.

Intially the user enters the start month and start year into separate Text
Boxes. From this information I have created an Array, strMonthLabels(60),
which contains each month and year for the five year period; i.e. "Jan 2009",
"Feb 2009", etc.

Each of the Text Boxes to capture the month cost information has an adjacent
Label to signify the month and year so that the user can enter the data
correctly. These Labels are named lblMonth1 through to lblMonth60.

I want to be able to use a For-Next Loop to change the Captions on each of
the Labels. The following indicates my intententions but does not work.

For i = 1 To 60
lblMonth(i).Caption = strMonthLabels(i)
Next

An alternative I have tried unsuccessfully is to create a Label variable

Dim objLabelName As Label

and then a value for this variable

objLabelName = "lblMonth" + Trim(Str(i))
objLabelName.Caption = strMonthLabels(i)

The answer is probably staring me in the face!

Please can you advise how to use a For-Next Loop to move from Label to Lable
to change each caption?

Many thanks
 
P

p45cal

For i = 1 To 60
UserForm1.Controls("lblMonth" & i).Caption = strMonthLabels(i)
Next i

-userfom1-(or your userform's name) may/may not be necessary dependin
on where the code is placed
 
J

Jon Peltier

Note that objLabelName is a string, so you need this:

controls(objLabelName).caption = strMonthLabels(i)

But p45cal's code doesn't need an intermediate string.

- Jon
 

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

Top