Looping through controls in Tab Order

J

Jeremy Gollehon

After searching a through the newsgroup and not finding anything that did
exactly what I want, I've created the following functions to return a
collection of controls in tab order. As I'm fairly new to Access VBA, are
there any gotcha's here that I might be missing? If not, then hopefully
others will find this code helpful.

---------------------------------------------------------------------------­-
Function HasTabStop(ctl As Access.Control) As Boolean
' Returns True if provided control has a TabStop property.
On Error Resume Next
HasTabStop = ctl.TabStop
End Function

Function ControlsInTabOrder(FormSection As Access.Section) As Collection
' Returns an collection of controls in tab order.

Dim asControlNames() As String
Dim colControlsInOrder As New Collection
Dim ctl As Access.Control
Dim i As Long

ReDim asControlNames(FormSection.Controls.Count)

For Each ctl In FormSection.Controls
If HasTabStop(ctl) Then
If ctl.TabStop = True Then asControlNames(ctl.TabIndex) = ctl.Name
End If
Next ctl

For i = 0 To UBound(asControlNames)
If asControlNames(i) <> "" Then
colControlsInOrder.Add FormSection.Controls(asControlNames(i))
End If
Next i

Set ControlsInTabOrder = colControlsInOrder

Exit_Function:
Exit Function
Error_Handler:
MsgBox "Error " & Err.Number & vbNewLine & _
Err.Description & vbNewLine & vbNewLine & _
"Problem occurs in ControlsInTabOrder of UDF", vbCritical, "Code
Error"
GoTo Exit_Function
End Function
---------------------------------------------------------------------------­-

It can be used like this:
---------------------------------------------------------------------------­-
Sub ValidateForm()
For Each ctl In ControlsInTabOrder(Me.Detail)
If TypeName(ctl) <> "CommandButton" Then 'No need to validate buttons.
Debug.Print ctl.TabIndex
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