Referring to Controls Using Variable

S

Sprinks

I have a series of lables whose captions I'd like to set in the On Open event
depending on variables. They are named lbl1, lbl2, lbl3, etc., and rather
than setting each explicitly, I would like to use a For...Next loop that can
refer to them using the counter.

Can anyone tell me how to do this?

Thank you.
 
A

Allen Browne

You can refer to lbl1 as:
Me("lbl1")
or if you prefer:
Me.Controls("lbl1")

So:
For i = 1 to 3
Me("lbl" & i).Caption = "This is lbl" & i
Next
 
D

Douglas J Steele

Dim intLoop As Integer

For intLoop = 1 to 5
Me.Controls("lbl" & intLoop).Caption = strCaption(intLoop)
Next intLoop

strCaption(intLoop) assumes you've got an array of captions to use: you can
change that to any other approach you like (a function, looking the captions
up in a table, etc.)
 

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