Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determine control index

Status
Not open for further replies.

bronc

Instructor
Aug 28, 2003
145
GB
We can determine the name of a control by using its index:

MsgBox Controls(3).Name

but how can I determine the index of a control using code?

Thanks
 
Can you tell us why the index is important or significant? Typically I think of the index like an Autonumber. It shouldn't make any difference what the value is, only that it is unique.

Duane
Hook'D on Access
MS Access MVP
 
I agree this sounds like a bad idea. But my guess would be
Code:
Public Function getIndex(frm As Access.Form, ctlName As String) As Variant
  Dim ctl As Access.Control
  Dim i As Integer
  Dim blnFound As Boolean
  For i = 0 To frm.Controls.Count - 1
    If frm.Controls(i).Name = ctlName Then
      blnFound = True
      getIndex = i
      Exit For
    End If
  Next i
  If Not blnFound Then MsgBox "Check control name. Control not found"
End Function
 
I am not sure if this is even correct, because I do not know if controls renumber as you add and delete them. I believe it is the case so that if there are ten controls on a form then there is always indices 0-9. But if that is the case, you would never want to write code that references it index vice name. So you could try it.
 
How are bronc . . .

To Add another fly in the soup ... any [blue]attached label(s)[/blue] are also included in the [blue]Controls Collection![/blue] Removing any attached label(s) (for whatever purpose) will surely mixup the soup! . . .

I see no point to this.[surprised]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Actually I tried a couple of tests. The provided code is correct, but has limited utility. The index number may change every time you add or delete a control.
 
well thanks for that - i'm begining to realize that the index is not specific to the control but just the index number in the collection? Presumably if i delete/add any controls the collection will be renumbered anyway. I should try that. thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top