To create an event procedure that is run when a Delete, BeforeDelConfirm, or AfterDelConfirm event occurs, set the OnDelete, BeforeDelConfirm, or AfterDelConfirm property to [Event Procedure], and click the Build button.
Private Sub Form_Delete (Cancel As Integer)
Private Sub Form_BeforeDelConfirm (Cancel As Integer, Response As)
Private Sub Form_AfterDelConfirm (Status As Integer)
The Delete event procedure uses the following argument.
Argument | Description |
Cancel | The setting determines if the Delete event occurs. Setting Cancel to True (-1) cancels the Delete event. |
The BeforeDelConfirm event procedure uses the following arguments.
Argument | Description |
Cancel | The setting determines if the BeforeDelConfirm event occurs. Setting Cancel to True (-1) cancels the BeforeDelConfirm event and prevents the delete confirm dialog box from being displayed. If the event is canceled, the original records are restored, but the AfterDelConfirm event still occurs. If Cancel is set to True (-1), the Response argument is ignored. |
Setting Cancel is set to False (0), does not cancel the BeforeDelConfirm event and the value in the Response argument is used by Microsoft Access to determine the type of response to the delete event. | |
Response | An intrinsic constant that determines whether Microsoft Access displays a dialog box asking if the record should be deleted. The Response argument can have the following constants. |
Constant | Description |
acDataErrContinue | Enables Microsoft Access to delete records without prompting the user. |
acDataErrDisplay | Displays the default message |
The AfterDelConfirm event procedure uses the following argument.
Argument | Description |
Status | An intrinsic constant that determines whether a record has been deleted. The Status argument can be any the following constants: |
Constant | Description |
acDeleteOK | Indicates the deletion was successful. |
acDeleteCancel | Indicates the deletion was canceled in Visual Basic. |
acDeleteUserCancel | Indicates the deletion was canceled by the user. |
The AfterDelConfirm event cant be canceled.
You can use these events to customize how records are deleted. For example, when a user deletes a record, such as an employee record, the Delete event occurs. You can place code in the Delete event procedure to save the data from the Employee ID and Name fields in a temporary buffer. When the BeforeDelConfirm event occurs, you can prevent the delete confirm dialog box from being displayed by setting the Response argument to acDataErrContinue. This enables you to display a custom dialog box. When the AfterDelConfirm event occurs and the Status argument is set to acDeleteOK, you can do the actual delete in the table and clear the record from the temporary buffer.
Delete, BeforeDelConfirm, AfterDelConfirm Events Macros.
This example shows how you can prevent a user from deleting records from a table.
To try this example, add the following code to the Declarations section of a form that is based on an Employees table. Switch to the forms Datasheet view and try to delete a record.
Private Sub Form_Delete (Cancel As Integer) Cancel = True MsgBox "This record can't be deleted."Sub
The following example shows how you can use the BeforeDelConfirm event procedure to suppress the default confirmation dialog box and display a custom dialog box when a record is deleted. It also shows how you can use the AfterDelConfirm event procedure to display a message indicating whether the deletion progressed in the usual way or whether it was canceled in Visual Basic or by the user.
To try this example, add the following code to the Declarations section of a form that is based on a table or query.
Private Sub Form_BeforeDelConfirm (Cancel As Integer, _ Response As Integer) ' Suppress default delete confirm dialog box. Response = acDataErrContinue ' Display custom dialog box. If MsgBox ("Delete this record?", vbOKCancel) = vbCancel Then Cancel = True End IfSub Sub Form_AfterDelConfirm (Status As Integer) Select Case Status Case acDeleteOK MsgBox "Deletion occurred normally." Case acDeleteCancel MsgBox "Programmer canceled the deletion." Case acDeleteUserCancel MsgBox "User canceled the deletion." End SelectSub