coding to cancel or go (tab) back to previous text box in form

  • Thread starter Thread starter Guest
  • Start date Start date
You could use the SendKeys method as shown just below this statement, but me
personally, I don't like to use it cause it can very easily cause too many
other issues, so I included a VBA code sample that would work too.

SendKeys "+{TAB}"

Here's a sample code you could put in your VBA form class module.

Private Sub GotoPrvCtrl()
Dim ctl as Control, lngCurIdx As Long, lngPreIdx As Long, lngCtlIdx As
Long
Dim ctlPrvCtl as Control
On Error Goto EH
lngCurIdx = ActiveControl.TabIndex
lngPrvIdx = lngCurIdx
For each ctl in Me.Controls
lngCtlIdx = ctl.TabIndex
If ctl.Enable = -1 Then
If lngPrvIdx = lngCurIdx Then
lngPrvIdx = lngCtlIdx
Set ctlPrvCtl = ctl
ElseIflngPrvIdx > lngCurIdx Then
If lngCtlIdx > lngPrvIdx Or lngCtlIdx<lngCurIdx Then
lngPrvIdx = lngCtlIdx
Set ctlPrvCtl = ctl
End If
Elseif lngCtlIdx > lngPrvIdx And lngCtlIdx < lngCurIdx
lngPrvIdx = lngCtlIdx
Set ctlPrvCtl = ctl
End If
End If
NextCtrl:
Next
If ctlPrvCtl.Name <> ActiveControl.Name Then
ctlPrvCtl.SetFocus
End If
ExitSub:
Exit Sub
EH:
Select Case Err.Number
Case 461 'Method or data member not found.
'More precisely, TabIndex is not a member of the ctl object on
it's current loop.
'Goto the end of the loop to go to the next ctl.
Resume NextCtrl
Case Else
'Some other error took place, we are exiting out of the
subprocedure.
Resume ExitSub
End Select
End Sub

--
Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000

jtd135 said:
Does anyone know the code I would use to tab back to the previous text box
in a form?
 
jtd135 said:
Does anyone know the code I would use to tab back to the previous
text box in a form?

I don't recommend using SendKeys, because (a) there are bugs associated
with it, and (b) you never know for sure that some other window isn't
going to seize the focus just as you send your keystrokes.
Unfortunately, I've never found any simpler method for identifying the
next or previous control in the tab order than looping through the
controls in the section and examining their TabIndex properties. In
response to a question some time ago, I wrote the following function to
return the *next* control in the tab order; presumably you can adapt it
to return the name of the *previous* control instead:

'---- start of code ----
Function NextInTabOrder(StartCtl As Access.Control) As String

' Returns the name of the next active control in the tab order,
' if possible. Returns a null string if there isn't one.

Dim astrControlNames() As String
Dim obj As Object
Dim ctl As Access.Control
Dim intI As Integer

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

Set obj = StartCtl.Parent

ReDim astrControlNames(obj.Controls.Count)

' Build a list of available controls indexed by TabIndex.
' We aren't interested in controls that are invisible,
' disabled, 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 Then
astrControlNames(.TabIndex) = .Name
End If
End If
End With
Next ctl

Set obj = Nothing

' Loop forward through the list
For intI = StartCtl.TabIndex + 1 To UBound(astrControlNames)
If Len(astrControlNames(intI)) > 0 Then
NextInTabOrder = astrControlNames(intI)
Exit Function
End If
Next intI

' If we didn't find one, start at the beginning of the list.
For intI = 0 To StartCtl.TabIndex - 1
If Len(astrControlNames(intI)) > 0 Then
NextInTabOrder = astrControlNames(intI)
Exit Function
End If
Next intI

' If we get here, there's no available control.
Exit Function

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

It does seem like a lot of overhead to go through just to tab to the
next or previous control. I wouldn't blame you if you went with
SendKeys despite the risks.
 
One little issue that you haven't taken into account with your code. If
StartCtl is on a Multi-Tab object (actually, a page object, which is part of
the Pages collection group on the Multi-Tab object), the following line in
your code will error out:

Set obj = StartCtl.Parent

What I have done to get around this issue, I have used the following code:

'---------StartCode----------
Public Function GetFrm(ByRef OBJ As Object) As Form
Dim frm As Form, Ctl as Control
On Error Resume Next
Set Ctl = OBJ
Set frm = Ctl.Parent
Do While err.Number <> 0
err.Clear
Set Ctl = Ctl.Parent
If err.Number <> 0 Then
GoTo EH
Else
Set frm = Ctl.Parent
End If
Loop
Set GetFrm = frm
ExitFnc:
Exit Function
EH:
Set GetFrm = Nothing
Resume ExitFnc
End Function
'---------EndCode---------

Which then the following code wouldn't error out.

Set obj = GetFrm(StartCtl)

The only thing you would need to do is test to see of obj is set to Nothing,
and if it's not then it is set to a form.
 
Ronald Dodge said:
One little issue that you haven't taken into account with your code.
If StartCtl is on a Multi-Tab object (actually, a page object, which
is part of the Pages collection group on the Multi-Tab object), the
following line in your code will error out:

Set obj = StartCtl.Parent

I don't follow you. That line is valid for a control on a tab page and
won't error out; it will return the Page object, which was my
intention. I think what we have here is a difference in intention --
your "GetFrm" function chases up the parent/child hierarchy to get to
the form object, while I am only concerned with the tab order of
controls in the same section or tab page. I decided to handle it that
way because the TabIndex is only meaningful with reference to the
controls with the same parent. If you have a form header section, a
form footer section, a detail section, and a tab control with two pages,
you could potentially have 5 controls all with the same TabIndex.

It would be possible to write the function so that, if the user requests
the previous control from the first control in a section/page, or the
next control from the last control in a section/page, to leave that
section and look for the appropriate control in another section. I
chose not to do this, because (a) it's more complicated than I felt a
need for, and (b) it's hard to know what the caller's intention is:
does he want to change sections or stay in the current section?
 
Okay, I see your point. Guess what I dealt with before, I had a couple of
command buttons on the form level and everything else on pages with regards
to one of my forms, of which it tabbed through everything on the page, then
it went to the form level controls after that (assuming we went from tab
index of 0 on the page). One thing I didn't realize, each page/form section
has it's own sequence of tab index numbers. I will therefore have to modify
my code slightly.

The other thing that had me think about this is the error that I ran into
with Leban's Month Calendar tool as that was my first encounter of the error
that I got, thus what led me to the creation of the function.
 
Ronald Dodge said:
Okay, I see your point. Guess what I dealt with before, I had a
couple of command buttons on the form level and everything else on
pages with regards to one of my forms, of which it tabbed through
everything on the page, then it went to the form level controls after
that (assuming we went from tab index of 0 on the page).

Which makes perfect sense, of course, if that's what you want it to do,
though as you say, you may want to look into the problem of duplicate
tab indexes. Depending on how you search the controls, you may get
exactly the behavior your want.
The other thing that had me think about this is the error that I ran
into with Leban's Month Calendar tool as that was my first encounter
of the error that I got, thus what led me to the creation of the
function.

I'm not familiar with that control. You had an error arising from the
control-parent question?
 
See inline below

--
Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000

Dirk Goldgar said:
Which makes perfect sense, of course, if that's what you want it to do,
though as you say, you may want to look into the problem of duplicate
tab indexes. Depending on how you search the controls, you may get
exactly the behavior your want.


I'm not familiar with that control. You had an error arising from the
control-parent question?

My mistake, wasn't in the month calendar control, but rather in the command
color tool that he has:

Set objFormReport = Ctl.Parent

Which then when it came to the line:

If objFormReport.CurrentView <> 0 Then

It would error out as a page doesn't have the CurrentView Property.
 
Ronald Dodge said:
My mistake, wasn't in the month calendar control, but rather in the
command color tool that he has:

Set objFormReport = Ctl.Parent

Which then when it came to the line:

If objFormReport.CurrentView <> 0 Then

It would error out as a page doesn't have the CurrentView Property.

Ah, I see.
 
Back
Top