refering to a control by its tab index

G

Guest

Hi,

Simple question: is it possible to refer to a control by its tab index in
Visual Basic?

Thanks in anticipation...
 
D

Douglas J Steele

Don't think so, but you'd be better off asking in a newsgroup related to
Visual Basic.

This newsgroup is for questions about Access, the database product that's
part of Office Professional. The controls available for forms in Access are
considerably different than those available in VB.
 
B

Brendan Reynolds

I'm afraid the term 'Visual Basic' or 'VB' can mean so many different things
these days that it has become, for most practical purposes, meaningless. Do
you mean VBA, or 'classic' VB (e.g. VB6 or earlier) or VB.NET?

If you mean VBA, then the only method I know is to walk the collections. See
the example below. If you mean VB 'classic' or VB.NET then, as you have been
advised elsewhere in this thread, you may get a more definitive answer in a
more appropriate newsgroup.

Private Sub Command4_Click()

Dim ctl As Control
Dim prp As Property
Dim boolFound As Boolean

For Each ctl In Me.Controls
For Each prp In ctl.Properties
If prp.Name = "TabIndex" Then
If prp.Value = 1 Then
MsgBox "The control with TabIndex 1 is: " & ctl.Name
boolFound = True
Exit For
End If
End If
Next prp
If boolFound Then
Exit For
End If
Next ctl

End Sub
 
D

Douglas J Steele

Brendan: As I pointed out to the poster in another newsgroup where he posted
the same question, that may not work.

The Form Header, Detail and Form Footer sections all have their own
independent Tab orders. Assuming you've got controls that can take focus on
each of those three sections, you're going to have 3 separate controls that
show a value of 0 for the TabOrder property.

Take the

If boolFound Then
Exit For
End If

out of your code, and run it on a form that does have a header and footer
visible. Assuming you've got at least 2 controls on each of the 3 sections
(and they have their TabStop set to True), you should get 3 separate message
boxes.
 
B

Brendan Reynolds

Thanks Doug. I never would have discovered that, as I never use form headers
and footers. This may be redundant, as I don't know what has been said in
the other thread, but for whatever it may be worth ...

Private Sub Command4_Click()

Dim ctl As Control
Dim prp As Property
Dim boolFound As Boolean

For Each ctl In Me.Section(Val(Me.txtSection)).Controls
For Each prp In ctl.Properties
If prp.Name = "TabIndex" Then
If prp.Value = Val(Me.txtTabIndex) Then
MsgBox "The control in section " & Me.txtSection & _
" with TabIndex " & Me.txtTabIndex & " is: " &
ctl.Name
boolFound = True
Exit For
End If
End If
Next prp
If boolFound Then
Exit For
End If
Next ctl

End Sub
 

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