Delete, BeforeDelConfirm, AfterDelConfirm Events — Event Procedures

Description

To create an event procedure that runs when a Delete, BeforeDelConfirm, or AfterDelConfirm event occurs, set the OnDelete, BeforeDelConfirm, or AfterDelConfirm property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_Delete(Cancel As Integer)

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)

Private Sub Form_AfterDelConfirm(Status As Integer)

The Delete event procedure has the following argument.

Argument

Description

Cancel

The setting determines if the Delete event occurs. Setting the Cancel argument to True (–1) cancels the Delete event.


The BeforeDelConfirm event procedure has the following arguments.

Argument

Description

Cancel

The setting determines if the BeforeDelConfirm event occurs. Setting the Cancel argument to True 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, the Response argument is ignored.

If Cancel is set to False (0), which it is by default, the value in the Response argument is used by Microsoft Access to determine the type of response to the delete event.

Response

The setting determines whether Microsoft Access displays the Delete Confirm dialog box asking if the record should be deleted. The Response argument can be set to one of the following intrinsic constants:


(continued)

Constant

Description

acDataErrContinue

Continues without displaying the Delete Confirm dialog box. Setting the Cancel argument to False and the Response argument to acDataErrContinue enables Microsoft Access to delete records without prompting the user.

acDataErrDisplay

(Default) Displays the Delete Confirm dialog box.


The AfterDelConfirm event procedure has the following argument.

Argument

Description

Status

The setting indicates whether a record has been deleted. The Status argument can be any of the following intrinsic 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.


Remarks

The AfterDelConfirm event can't 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 set of temporary variables. 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 clear the temporary variables, or save the employee information in another table.

See Also

Delete, BeforeDelConfirm, AfterDelConfirm events — macros.

Example

The following example shows how you can prevent a user from deleting records from a table.

To try this example, add the following event procedure to a form that is based on a table. Switch to form Datasheet view and try to delete a record.

Private Sub Form_Delete(Cancel As Integer)
    Cancel = True
    MsgBox "This record can't be deleted."
End Sub
The following example shows how you can use the BeforeDelConfirm event procedure to suppress the Delete Confirm 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 the example, add the following event procedure to 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 If
End Sub

Private 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 Select
End Sub