Getting Index the Controls Property in VBA

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

Guest

Hi,

I am trying to get the index of the current control that has focus.
I tried:
Index = Me.Controls(Me.SNDetailSubform.Form.ActiveControl.Name).TabControl

Index = Me.Controls(Me.SNDetailSubform.Form.ActiveControl.Name).Index

Index = Me.Controls(Me.SNDetailSubform.Form.ActiveControl.Name).TabIndex

I was able to get tabindex, but tabindex is not what Controls uses as their
index. Access uses tabindex. Controls even gives an index to its labels.

How can I get the index property value the Controls is using?

Thanks
 
First of all, the syntax you're using looks counterproductive, unless you
are actually trying to work with a control on your main form which has the
same name as the active control on the subform.

Second, I'm not sure why you need the index in the Controls collection, when
you can always refer to a control by its name instead of its index -
Me.Controls("MyControl") instead of Me.Controls(i).

However, if you really need to return that index, you could do it with a
function like this:
(WARNING: AIR CODE)

Public Function ControlIndex(ctl as Control) as long
Dim i as Integer
For i=0 to Me.Controls.Count-1
if me.Controls(i) is ctl then
ControlIndex=i
exit for
end if
next
End Function

HTH
 
Back
Top