Setting Focus on next tab control

  • Thread starter Thread starter Jim Shaw
  • Start date Start date
J

Jim Shaw

BlankAt the end of a piece of event logic for a control. I want to move the
focus to the next control in the tab list. I want to be able, in the
control's property settings, to change the tab order without having a need
to revise the VBA code. I there a VBA command or something to do this?

Thanks
Jim
 
No command, but you could use a VBA subroutine/code to do this:



' ******************************************
' ** Subroutine MoveFocusToNextControl **
' ******************************************

Public Sub MoveFocusToNextControl(xfrmFormName As Form, _
xctlCurrentControl As Control)
' *** THIS SUBROUTINE MOVES THE FOCUS TO THE
' NEXT CONTROL IN THE TAB ORDER.
' First argument is the form object on which the action is to occur.
' Second argument is the control object that is the control whose tab
' order number is just before the control that is to get the focus (in
' most cases, this will be the control that currently has the focus).

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
lngTab = xctlCurrentControl.TabIndex + 1
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
xctl.SetFocus
Exit For
End If
Else
Err.Clear
End If
Next xctl
Set xctl = Nothing
Err.Clear
End Sub
 
HI;
And when I change the tab order in the control's properties, I'd have to
revise this VBA code! I don't want to have to do that.
Thanks
Jim

JL said:
Hi,
Try setfocus. The syntax is similar to
Me![Field].setfocus


Jim Shaw said:
BlankAt the end of a piece of event logic for a control. I want to move the
focus to the next control in the tab list. I want to be able, in the
control's property settings, to change the tab order without having a need
to revise the VBA code. I there a VBA command or something to do this?

Thanks
Jim
 
Ken;
This is exactly what I need. Thanks!
I've never passed these types of args before...
Is the calling syntax:?
call MoveFocusToNextControl(frmMyForm as Form, tboxMyControl as Control)
Jim
 
Not quite.

Let's assume that you're calling this subroutine from within the form where
you want to change the focus. I'd write it this way:

Call MoveFocusToNextControl(Me, Me.ControlName)

Me is literal...don't change it.

ControlName is the name of the control that currently has the focus.

--

Ken Snell
<MS ACCESS MVP>
 
Back
Top