In VBA how do I get the index number for a control on a form



I have a control on a form that I want to access with its index number (the
controls are various). Without enumerating how do I get that index number
(the 179 in the code below)? and
why does this code throw an error when the variable ctl is set (says object
Sub Test1()
Dim frm As Form, ctl As Control
Set frm = Forms![suchandsuch]
Set ctl = frm.Controls.Item(179).Name
End Sub

Douglas J. Steele

The reason you're getting the error is because you're including a property
when you're instantiating the control in your Set statement:

Set ctl = frm.Controls.Item(179)
MsgBox "Control 179 is named " & ctl.Name

I don't understand, though, why you need to know the control number. Do you
not know the name of the control? If you do, you can refer to it by name:

Set ctl = frm.Controls.Item("NameOfControl")

If you don't know the name of the control, then I don't understand how
enumerating through the Controls collection would help!

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