Setting Focus on next tab control

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
 
K

Ken Snell [MVP]

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
 
J

Jim Shaw

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
 
J

Jim Shaw

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
 
K

Ken Snell [MVP]

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>
 

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