Referencing form controls with a loop

R

robotman

I have a form that has 12 identical lines (same label, button, field
etc.).

In normal VB, I can create each control as an array, ex.
frmMyForm.lblMyLabel(1).
So this works great for changing something for all the controls with a
loop:

For Myloop = 1 to 12
frmMyForm.btnMyButton(MyLoop).Enabled = False
Next MyLoop

In VBA, I have to independently name each control and reference them
each one by one:

frmMyForm.btnMybutton1.Enabled = False
frmMyForm.btnMybutton2.Enabled = False
...
frmMyForm.btnMybutton12.Enabled = False

Does anyone have a shortcut to reference multiple identical controls
with a loop in VBA?!

Thanks.

John
 
D

Dave

Try referring to the control on the form as frmWhatEver.control(x) where x is
and integer reference to the controls... you will have to figure a way to
find the buttons by referring to the name of control eg.
left(frmWhatEver.control(x).name , 3) = "btn" or something like that - don't
know if there is a way to see what the control typeis ..

Best
 
J

JLGWhiz

I'm not sure what you mean, but each type of control on a UserForm has an
index number that can be used in an array. Any collection can be used in an
array in VBA and controls are collections.
 

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