How to Not Save a New Record

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

Guest

I have a form with a subform, the main form generates a unique identifyer for
the record on form open.

If nothing is entered in the subform, I don't want the record on the main
form saved.

How can I keep it from saving?

Sorry - I know this is probably easy but I have "Friday brain". Thanks.
 
Anne said:
I have a form with a subform, the main form generates a unique
identifyer for the record on form open.

If nothing is entered in the subform, I don't want the record on the
main form saved.

How can I keep it from saving?

Sorry - I know this is probably easy but I have "Friday brain".
Thanks.

It's not easy at all. The main record MUST be saved before you can create a
subform record due to referential constraints (if you've set them up properly).
Because of this requirement the main record is saved as soon as focus moves to
the subform. You can do little at that point to force the user to actually
create a sub-record.

Thinking out loud I suppose you could use the subforms [Enter] event to check
for existing subrecords and in the absence of any it could "pre-dirty" the
subform by setting the value of some fields. Then the user would be forced to
cancel the creation of the subrecord rather than just neglecting to do so and
you might be able to trap for that and delete the parent record. This of course
requires that deleting the parent satisfies your requirements as opposed to
preventing its creation in the first place.
 
Anne said:
I have a form with a subform, the main form generates a unique
identifyer for the record on form open.

If nothing is entered in the subform, I don't want the record on the
main form saved.

How can I keep it from saving?

Sorry - I know this is probably easy but I have "Friday brain".
Thanks.

It's not as easy as all that, so maybe your brain is okay. If the focus
ever goes to the subform, the main form will be saved, and there's
nothing you can do to stop it. You *can* do these things:

1. Instead of setting the value of the main form's key in the form's
Open event, you can set that control's DefaultValue property instead.
That way the form won't be "dirty", so if the focus *doesn't* go to the
subform, and the user just closes the main form, the record won't be
saved.

2. If necessary, you might use the Exit event of the subform control (on
the main form) to check whether any subform record was added for this
main-form record. If not, you can delete the main-form record. That's
not really safe unless there's no main-form record is ever supposed to
exist without a subform record.

3. Similarly, and as an alternative, you could use the main form's Close
event to run a delete query that deletes all main-form records that
don't at least one matching subform record. Again, you can only do this
if there's a logical rule that says no main-form record should ever
exist without a matching subform record.
 
Back
Top