>> Tab to next control

G

Guest

Hi, to set a control's enabled property to false, focus must first go to
another control.

How can I use the control's .tabindex to move focus to the next control in
the tab order?

Any ideas or suggestions appreciated :)

Many thanks, Jonathan.
 
K

Ken Snell \(MVP\)

Here's a subroutine that I have used to do this:

' **********************************
' * Sub MoveFocusToNextControl *
' **********************************

Public Sub MoveFocusToNextControl(xfrmFormName As Form, xctlCurrentControl
As Control, _
xblnOnlyTabStopYes As Boolean)
' *** THIS SUBROUTINE MOVES THE FOCUS TO THE NEXT CONTROL IN THE TAB ORDER.
' *** IF xblnOnlyTabStopYes IS TRUE, THEN THE NEXT CONTROL MUST HAVE A TAB
' *** STOP VALUE OF "TRUE" AND MUST BE VISIBLE AND MUST BE ENABLED, ELSE
' *** THE SUBROUTINE GOES TO THE NEXT CONTROL.
' *** NOTE: THIS SUBROUTINE WILL NOT "CYCLE BACK" TO TAB INDEX OF 0, SO
' *** DO NOT USE THIS SUBROUTINE IF THERE IS NO CONTROL CAPABLE OF RECEIVING
THE
' *** FOCUS IN THE TAB ORDER SEQUENCE AFTER THE CURRENT CONTROL!!!!!
' Ken Snell - May 26, 2005

Dim xctl As Control
Dim lngTab As Long, lngNewTab As Long

On Error Resume Next

' Move focus to the next control in the tab order (if that control has a Tab
Stop
' property of True)
lngTab = xctlCurrentControl.TabIndex + 1

MyLoopLabel:
For Each xctl In xfrmFormName.Controls
lngNewTab = xctl.TabIndex
' An error will occur if the control does not have a TabIndex property;
' skip over those controls.
If Err.Number = 0 Then
If lngNewTab = lngTab Then
If (xctl.TabStop = True Or xblnOnlyTabStopYes = False) And _
xctl.Visible = True And xctl.Enabled = True Then
xctl.SetFocus
Exit For
Else
lngTab = lngTab + 1
GoTo MyLoopLabel
End If
End If
Else
Err.Clear
End If
Next xctl
Set xctl = Nothing
Err.Clear
End Sub
 
G

Guest

Thanks Ken, simply marvellous. I must admit I had not thought that a custom
routine was required to achieve this. I was expecting a built-in method!

Many thanks, Jonathan
 

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