Getting Index the Controls Property in VBA

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
 
M

MacDermott

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
 

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