Delete, BeforeDelConfirm, AfterDelConfirm Events — Event Procedures 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