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

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

Guest

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
required)
Sub Test1()
Dim frm As Form, ctl As Control
Set frm = Forms![suchandsuch]
Set ctl = frm.Controls.Item(179).Name
End Sub
 
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!
 
Back
Top