Link to home
Start Free TrialLog in
Avatar of kentgorrell
kentgorrell

asked on

Get the name of a control's label in vba

At design time I loop through my controls in VBA to apply naming conventions.

I want to rename my labels as "lbl" & [Control source of the control the label is attached to].
To do this I either need to find the name of the control the label is attached to or, visa versa, the name of the label attached to each text or combo box.

Is there a property of the label or the txt/cbo box that will do this? I can't see one. Or is there another method?
ASKER CERTIFIED SOLUTION
Avatar of Graham Mandeno
Graham Mandeno
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kentgorrell
kentgorrell

ASKER

.Controls(0) was what I needed. Thank you.

Can there be more than one control (anything other then a label) in a text (or combo) box's controls collection?
I'm not sure - I've never seen more than one for a text or combo box, and I can't see how there could be more.

Of course, some other objects such as a tab control or a form section can have multiple members in this collection.

Graham
Thanks Graham, I can't think of anything other than a label.

Tab controls' Intellisense doesn't list the Controls collection but it's there.
You can also use the following syntax to tet the name (or any other property) of the control associated with any label (or any other control for that matter)

ctl.parent.name

If the control is a label, and it is associated with another control, you will get the controlname.  If it is a label that is not associated with another control, you will get the form name (as you would with any of the textbox, lists, combo, checkbox, ... other controls).
One small adjustment to prevent controls in the footer replacing the controls with the same tab index as the ones in the detail section.
Line 2 should be -

For Each ctl in frm.Detail.Controls