tweaking code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use this code on several forms to allow changes to records to be undone
before they are saved. The button in only enabled when the form is dirty.
But, on each different form I use, I need to change line 3 of the code.

Sub cmdUndoChanges_Click()
DoCmd.RunCommand acCmdUndo
txtCustomerID.SetFocus
Me!cmdUndo.Enabled = False
End Sub

How do I change line 3 so that focus always moves to the control that has
the 1st tab order?
 
Sophie said:
I use this code on several forms to allow changes to records to be
undone before they are saved. The button in only enabled when the
form is dirty. But, on each different form I use, I need to change
line 3 of the code.

Sub cmdUndoChanges_Click()
DoCmd.RunCommand acCmdUndo
txtCustomerID.SetFocus
Me!cmdUndo.Enabled = False
End Sub

How do I change line 3 so that focus always moves to the control that
has the 1st tab order?

Here's a function you might use to determine which control on the
current form is first in the tab order. It's a little more complicated
than you may need, but you should be able to use it, as I'll show below.
Note that this code is only lightly tested.

'----- start of function code -----
Function FirstInTabOrder(StartCtl As Access.Control) As Control

' Returns a reference to the first active control in the tab order,
' in the same section or tab page as the argument control
<StartCtl>.
' Returns Nothing if there is no active control in the section or
page.

Dim obj As Object
Dim ctl As Access.Control
Dim ctlRet As Access.Control
Dim intTI As Integer

On Error Resume Next
'to ignore errors we expect to raise

Set obj = StartCtl.Parent

' Build a list of available controls indexed by TabIndex.
' We aren't interested in controls that are invisible,
' disabled, not a tab stop, or not in the same section
' as StartCtl.
For Each ctl In obj.Controls
With ctl
If .Section = StartCtl.Section Then
If .Visible = True _
And .Enabled = True _
And .TabStop = True Then
intTI = obj.Controls.Count
intTI = ctl.TabIndex
If ctlRet Is Nothing Then
Set ctlRet = ctl
Else
If intTI < ctlRet.TabIndex Then
Set ctlRet = ctl
End If
End If
End If
End If
End With
Next ctl

Set obj = Nothing

Set FirstInTabOrder = ctlRet

End Function
'----- end of function code -----

The function returns an object reference to the control that is first in
the tab order, and is enabled, visible, and a tab stop. Note that it
uses the tab order of the section or page that contains the control you
pass as an argument. You might replace your line of code:
txtCustomerID.SetFocus

with this:

FirstInTabOrder(Me.ActiveControl).SetFocus

If your Undo button is not in the same form section or tab page, you'd
have to pass something else as the <StartCtl> argument, or maybe need a
different version of the function.
 
Dirk - nice solution!
--
Thanks
Sophie


Dirk Goldgar said:
Here's a function you might use to determine which control on the
current form is first in the tab order. It's a little more complicated
than you may need, but you should be able to use it, as I'll show below.
Note that this code is only lightly tested.

'----- start of function code -----
Function FirstInTabOrder(StartCtl As Access.Control) As Control

' Returns a reference to the first active control in the tab order,
' in the same section or tab page as the argument control
<StartCtl>.
' Returns Nothing if there is no active control in the section or
page.

Dim obj As Object
Dim ctl As Access.Control
Dim ctlRet As Access.Control
Dim intTI As Integer

On Error Resume Next
'to ignore errors we expect to raise

Set obj = StartCtl.Parent

' Build a list of available controls indexed by TabIndex.
' We aren't interested in controls that are invisible,
' disabled, not a tab stop, or not in the same section
' as StartCtl.
For Each ctl In obj.Controls
With ctl
If .Section = StartCtl.Section Then
If .Visible = True _
And .Enabled = True _
And .TabStop = True Then
intTI = obj.Controls.Count
intTI = ctl.TabIndex
If ctlRet Is Nothing Then
Set ctlRet = ctl
Else
If intTI < ctlRet.TabIndex Then
Set ctlRet = ctl
End If
End If
End If
End If
End With
Next ctl

Set obj = Nothing

Set FirstInTabOrder = ctlRet

End Function
'----- end of function code -----

The function returns an object reference to the control that is first in
the tab order, and is enabled, visible, and a tab stop. Note that it
uses the tab order of the section or page that contains the control you
pass as an argument. You might replace your line of code:


with this:

FirstInTabOrder(Me.ActiveControl).SetFocus

If your Undo button is not in the same form section or tab page, you'd
have to pass something else as the <StartCtl> argument, or maybe need a
different version of the function.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top