Delete Record On Close

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

Guest

I am trying to create code to delete the current record in my form when the
user closes the form if a particular field is null. If the field is null, it
would indicate that the user didn't complete enough information for the
record to be "real." Following is the code in the On Close event:

Private Sub Form_Close()

DoCmd.GoToControl "CUSTOMER_NUMBER"
If IsNull([CUSTOMER_NUMBER]) Then

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70


End If

End Sub


Can anyone tell me why this code doesn't delete the record? Do I need to
use a different event?

Thanks in advance!

Keith
 
Keith said:
I am trying to create code to delete the current record in my form when the
user closes the form if a particular field is null. If the field is null, it
would indicate that the user didn't complete enough information for the
record to be "real." Following is the code in the On Close event:

If you just make the field Required in the table then Access will automatically
disallow any records where it is not filled in.

Otherwise Close might be too late. Try Unload.
 
Rick Brandt said:
If you just make the field Required in the table then Access will
automatically disallow any records where it is not filled in.

Otherwise Close might be too late. Try Unload.

I think Unload would still be too late. If the record shouldn't be
saved, then the place to check this is in the form's BeforeUpdate event.
Then you can cancel the event by setting the event procedure's Cancel
argument to True. I would also make the field Required in the design of
the table, as Rick suggests, but checking in the BeforeUpdate event
allows more graceful handling of the situation.
 
Back
Top