Referring to Controls Using Variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
 
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.)
 
Back
Top