Tab in and out of a subform

  • Thread starter Thread starter SB
  • Start date Start date
S

SB

I have a main form and a sub form. I want to fill in
data on the main form and when I get to the subform it
tabs in fine but keeps cycling through the subform
instead of tabbing out when I'm done entering records.
How do I make it tab out? Microsoft has done this in the
Northwinds Sample Database but I cannot figure out how
the hell they did it.

Thanks in advance.
Scott
 
You can use Ctrl-tab to tab out of a subform and on to the next control in
the main form.
 
I'm looking for a solution that does not use the Ctrl-Tab. As I mentioned
the Northwinds database does it on the Orders form, how might I duplicate
this?
 
I'm looking for a solution that does not use the Ctrl-Tab. As I mentioned
the Northwinds database does it on the Orders form, how might I duplicate
this?

Set your subform to datasheet view.
 
Scott said:
I'm looking for a solution that does not use the Ctrl-Tab. As I
mentioned the Northwinds database does it on the Orders form, how
might I duplicate this?

You can do it by putting an infinitesimal text box -- height 0, width 0,
but with its Visible and Enabled properties set to True -- last in the
subform's tab order. Then use the GotFocus event of this control to set
the focus back to some control on the main form.
 
Hmmmm.....Thanks! That does that work now I guess I'll have to decide if I
want it in DS view.
 
That's brilliant! You're the man!




Dirk Goldgar said:
You can do it by putting an infinitesimal text box -- height 0, width 0,
but with its Visible and Enabled properties set to True -- last in the
subform's tab order. Then use the GotFocus event of this control to set
the focus back to some control on the main form.

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

(please reply to the newsgroup)
 
Hmmmm.....Thanks! That does that work now I guess I'll have to decide if I
want it in DS view.

If you don't want it in datasheet view and want the same functionality as the
Northwind example, you can include code in your subform to check to see if the
current record is a .NewRecord and, if so, set focus to the desired control on
the main form *if* the Tab or Enter key is pressed (I usually use the "KeyDown"
event procedure for the last control in the tab order on the subform). If you
opt to use the KeyDown event, don't forget to set "KeyCode" to 0 to cancel the
relevant keypress:

'***Example KeyDown Event Procedure Code
'If a blank record and Tab or Enter key is pressed
If Me.NewRecord And (KeyCode = Asc(vbTab) _
Or KeyCode = Asc(vbEnter)) Then
'Clear keypress
KeyCode = 0
'Set focus to desired control on parent form
Me.Parent.MyControlName.SetFocus
End If
'***
 
Dirk,
I tried [Names].Form![Amount].SetFocus
in the GotFocus property, no luck, am I using the wrong formula?

SB

[Names] = My parent form
[Amount] = the field on Names that I want the focus to go to.
 
Scott said:
Dirk,
I tried [Names].Form![Amount].SetFocus
in the GotFocus property, no luck, am I using the wrong formula?

Yep. Use either...

Forms![Names]!Amount.SetFocus

or

Me.Parent!Amount.SetFocus
 
Although you can use the OnGotFocus event of a hidden control as Dirk
suggests, you should consider that it is common to enter more than one
record in a subform - that could get quite annoying if the user needs to
enter more than one record.
 
Joan Wild said:
Although you can use the OnGotFocus event of a hidden control as Dirk
suggests, you should consider that it is common to enter more than one
record in a subform - that could get quite annoying if the user needs
to enter more than one record.

Agreed. When I've used this technique, either I was using the subform
for subclassing -- so it was really a one-to-one relationship -- or I
had logic in the "transfer" control to decide whether to leave the
subform or not.
 
Greetings!

And one more thing... I remember dumping this trick because if your users
get used to just tabbing forward through to the main form, they also might
try tabbing *backwards* to get to the main form... You can't do the teeny
control trick in that situation, (well, at least I don't *think* you can
<g>), and trying somehow to trap for the shift-tab keystrokes, seemed much
more trouble than teaching the users to use Control-Tab and
Shift-Control-Tab...

Cheers!
Fred Boer
 
Fred Boer said:
Greetings!

And one more thing... I remember dumping this trick because if your
users get used to just tabbing forward through to the main form, they
also might try tabbing *backwards* to get to the main form... You
can't do the teeny control trick in that situation, (well, at least I
don't *think* you can <g>) [...]

Sure you can, but there's a bit of finagling involved. If you want to
go back to a different control on the parent form, you have to use
another "teeny" control. I *think* -- it's been a while -- that you
just model this second control on the first one, using its GotFocus
event to send the focus to the desired parent-form control, and put it
*after* the other "transfer" control in the tab order. Back-tabbing
from the first control in the tab order causes the focus to go to this
new control because it's last in the tab order, and voilà! However, you
have to ensure that when control enters the subform, neither of these
"transfer" controls has the focus. Otherwise you'll never be able to
get back into the subform. You probably have to have the GotFocus
events of each of the transfer controls first set the focus on the
*subform* to its first control, and *then* set the focus back to the
desired control on the parent form.
 
Hi Dirk!
can't do the teeny control trick in that situation, (well, at least I
don't *think* you can <g>) [...]

Sure you can, but there's a bit of finagling involved. If you want to

Of course it's possible... what was I thinking to even suggest that it
wasn't! <g>

Dirk, this is very clever, but I'm not able to follow *all* of this; I'm
stumped here:
However, you
have to ensure that when control enters the subform, neither of these
"transfer" controls has the focus. Otherwise you'll never be able to
get back into the subform.

Umm.. how could these "transfer" controls get the focus, since they are, to
put it technically, "really teeny"? Are you thinking that someone might
accidentally just happen to hit them when they click into the subform?

You probably have to have the GotFocus
events of each of the transfer controls first set the focus on the
*subform* to its first control, and *then* set the focus back to the
desired control on the parent form.

Just plain lost here...

Thanks!
Fred
 
Fred Boer said:
[...] I'm not able to follow *all* of this; I'm stumped here:
However, you
have to ensure that when control enters the subform, neither of these
"transfer" controls has the focus. Otherwise you'll never be able to
get back into the subform.

Umm.. how could these "transfer" controls get the focus, since they
are, to put it technically, "really teeny"? Are you thinking that
someone might accidentally just happen to hit them when they click
into the subform?

No, not at all. But you must bear in mind that every form, if it has
any control capable of receiving the focus, has its own "active
control", as returned by the form's ActiveControl property. This is the
control that, *as far as the form is concerned*, has the focus. It may
not be the control that has the focus as far as the whole application is
concerned, but it has the focus *on this form*. This is true whether
the form is a main form or displayed in a subform. You'll probably have
noticed that if you click on a control (not the first in the tab order)
on a subform, then Ctrl+Tab out to the main form, move to a new record,
and then tab back into the subform, you end up back in the same control
you were in before. This demonstrates that the subform's "focus"
control is maintained separately from that of the main form.

So if you have one of these transfer controls on your subform, and on
its GotFocus event it transfers the focus dirrectly back to a control on
the main form, which control on the subform will have the focus if you
then shift the focus, by tabbing or clicking, onto the subform? You
guessed it: the transfer control! So what happens then? The focus is
kicked *back* out of the subform and back to the main form again.
Effectively, you can't get back into the subform. This is the problem I
was talking about.
You probably have to have the GotFocus

Just plain lost here...

You see, if you first change the subform's active control to some other
control, not one of the transfer controls, and only then send the focus
back to the main form, then the subform's active control won't be one of
the transfer controls. So when you next attempt to enter the subform
the focus won't go to a transfer control, and so you'll be able to get
in. Does that clarify it?
 
Hi Dirk:

Thanks for your wonderfully clear explanation!
You'll probably have
noticed that if you click on a control (not the first in the tab order)
on a subform, then Ctrl+Tab out to the main form, move to a new record,
and then tab back into the subform, you end up back in the same control
you were in before. This demonstrates that the subform's "focus"
control is maintained separately from that of the main form.

Actually, I have *never* noticed this behaviour; if I had I might better
have understood your original answer! Now, it makes perfect sense.
in. Does that clarify it?

Perfectly! :) I might even take another shot at trying this technique now..

Many thanks!
Fred
 
Fred Boer said:
I might even take another shot at trying this technique
now..

If you do, and you want to get around the problem pointed out by Joan --
needing to be able to enter more than one record on the subform, then
you could use code similar to this in the transfer control:

Private Sub txtTransfer_GotFocus()

If Me.NewRecord And Not Me.Dirty Then
Me.Parent!ID.SetFocus
Else
Me.ID.SetFocus
DoCmd.GoToRecord , , acNext
End If

End Sub

The code only leaves the subform if you're tabbing out of a blank new
record.

Note: "ID" in the above example happens to be the name of two different
controls: one on the parent form (bound to the key of the parent
record), and one on the subform (bound to the key of the child record).
That's just the way I happened to set up the example.
 
Dirk Goldgar said:
If you do, and you want to get around the problem pointed out by Joan
-- needing to be able to enter more than one record on the subform,
then you could use code similar to this in the transfer control:

Private Sub txtTransfer_GotFocus()

If Me.NewRecord And Not Me.Dirty Then
Me.Parent!ID.SetFocus
Else
Me.ID.SetFocus
DoCmd.GoToRecord , , acNext
End If

End Sub

Whoops, I should have included a line to take care of the "no re-entry"
problem I mentioned before. Change to this:

Private Sub txtTransfer_GotFocus()

If Me.NewRecord And Not Me.Dirty Then
Me.ID.SetFocus
Me.Parent!ID.SetFocus
Else
Me.ID.SetFocus
DoCmd.GoToRecord , , acNext
End If

End Sub

That ought to take care of it.
 
Back
Top