I have the following event procedures set for the Delete, BeforeDelConfirm and AfterDelConfirm events in a form.
Private Sub Form_AfterDelConfirm(Status As Integer)
MsgBox “Deletion completed”
End Sub
Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
Response = acDataErrContinue
End Sub
Private Sub Form_Delete(Cancel As Integer)
If MsgBox(“Do you want to delete?”, vbYesNo) = vbNo Then Cancel = True
End Sub
When I delete a record from the form, I encounter the following sequence of events (breakpoints are set on each of the three lines of actual code, to help track progress).
– When Delete is clicked, the message box appears, and No is clicked.
– After completing the Form_Delete procedure, there is no attempt to proceed to the Form_BeforeDelConfirm procedure (this is behaving as I would expect it to.)
– If I again click Delete, the Form_BeforeDelConfirm procedure is immediately initiated, and proceeds to the Form_AfterDelConfirm procedure, before THEN running the new Form_Delete procedure.
– No records are actually deleted during this process.
– This occurs even if I have moved to a new record, but not if I have closed and re-opened the form.
As I understand the Help file, if Cancel is set to True in the Delete procedure, the BeforeDelConfirm and AfterDelConfirm procedures will not run. That is what I want; however, the way it is working now will create a somewhat misleading sequence of messages.
I encountered the problem in a live database, but the code given above is a simplified version set up in an otherwise empty database that has a single table with an autonumber field, a text field and an integer field, with an auto-generated form providing the interface. There are no other objects or code, but the problem remains the same. The same problem occurs if I remove the breakpoints and let it run freely.
I know I can get around the problem by using a Private variable that flags whether to run the Before and After procedures, but would really like to know if I am doing something wrong, or if this is a bug in Access 2003 VBA.
Thanks.