It probably is possible to force the focus into the subform in
Form_AfterInsert, and then use the Exit event of the subform control to
DLookup() the subform's table for a related record if the main form is not
at a new record.
There are several disadvantages of that approach, e.g.:
- Forcing focus into the subform regardless of how the record is saved in
the main form, and then not letting the user back out until they enter a
subform record would be quite confusing, I think.
- Can't use the subform's record count (it may be filtered), so it means
hitting the table with the DLookup() every time focus moves out of the
subform control.
- Without setting additional flags, can't know when exiting the subform
control whether the entry was as the result of a new main form record.
- If the user wants to kill the main form entry, they can't get out of the
subform to do so without dealing with the message about no related records.
It might be justified in some cases, but on balance I suspect the cure is
probably worse than the problem.
Additionally, there are problems in Access with connecting the subform
events and the main form's AfterUpdate event. I have a documented case where
the code in Form_AfterUpdate works perfectly unless the record is saved by
moving into the subform. However, if the user tabs into the subform (which
triggers the implicit save of the main form's record) then:
- Access moves the form to a different record after Form_AfterUpdate
completes, and
- the Enter event of the subform control fails to fire.
The particular case provided a number field for the user to reorder the
questions in a survey. In Form_AfterUpdate, if this field changed, we
requeried the form in AfterUpdate (to reflect the new sort order), and moved
back to the record. This all worked fine unless the user tabbed into the
subform, in which case Access moved the main form to a different record
after Form_AfterUpdate completed, and the Enter event of the subform never
fired.
While that's not directly related to this issue, I find it really scary when
the events are unreliable, and particularly where Access is moving the
record pointer after the events compete!
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:Xn********************************@216.196.97 .142...
"Allen Browne" <Al*********@SeeSig.Invalid> wrote in
news:42**********************@per-qv1-newsreader-01.iinet.net.au:
Kind of a chicken'n'egg problem.
You have to have the main record first, before you can create the
subform record. So you cannot make having a subform record a
condition for accepting the main form record.
The subject of the thread suggests a different problem to me, one
that should be able to be handled in the subform's OnExit event. In
order to go to a New record, you have to depart the existing record,
and that will force the OnExit of the subform, and in that, you
should be able to cancel that event if there's no child record,
which will in turn prohibit moving to a new record.
Now, the problem is forcing the OnExit event. That ought to be
doable by setting focus to the subform in the form's AfterUpdate
event.