Running code after record is deleted

C

cherman

This should be easy, but I cannot figure it out - Friday morning. :)

Anyway, I have a subform and I want to run some code when one of the records
is deleted, but only if it is deleted. I don't want to run on the subform's
Delete or BeforeDelConfirm or AfterDelConfirm. These don't seem to fire when
I want.

Delete fires, but then the user can cancel the delete. Before and After
Confirm fire even if the user cancels the delete. I simply want to run code
after the record is deleted.

I am using Access 2003

Thanks in advance,
Clint
 
A

Allen Browne

You do need a combination of events to get this to work.

During the form's Delete event, you can discover what record is being
deleted. If the user selected several records (in a continuous or datasheet
view form), and pressed Delete, the Delete event fires for each one, so you
would need to store the primary key of all the records until you know
whether the delete proceeded.

The form's AfterDelConfirm event gives you the Status argument, so at this
stage you do know whether the delete succeeded or not. If it did succeed,
you can now refer to the primary key value(s) for the record(s) deleted, and
take whatever action you need.

For an example of using those events, see:
Audit Trail - Log changes at the record level
at:
http://allenbrowne.com/AppAudit.html
The AuditDelBegin() code is fired by Form_Delete, and records the primary
key values proposed for deletion. The AuditDelEnd() code fires in
Form_AfterDelConfirm, and makes the appropriate response (in this case
logging the deletion.)

That all works fine, provided that confirmation of deletion is turned on,
and that this is an MDB. ADPs are different:
http://support.microsoft.com/kb/234866/en-us
 
C

cherman

Thank you for your detailed response! I've been able to adjust my code to get
it to work. I cannot believe I haven't noticed the Status variable in the
AfterDelConfirm event!!! :)

Clint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top