Wednesday, 4 January 2012

MS-Access: Chain of events while deleting a record on a form

When you delete a record, the following events occur for the form:

    1 - Form's Delete event occurs before its BeforeDelConfirm event. Both can be cancelled if desired.
 
    2 - At Delete event stage, the record under deletion is still the current one and correct values for various controls are returned. By the time you encounter BeforeDelConfirm event, the record next to that meant for deletion becomes the current record and the values returned can be misleading.
 
    3 - If Delete event is cancelled, subsequent events (i.e. BeforeDelConfirm and AfterDelConfirm) don't fire.
 
    4 - AfterDelConfirm event is the last one to fire, provided delete event itself has not been cancelled. It returns Status values as follows:
    (a) 0 for actual implementation of deletion
    (b) 1 for programmatic cancellation at BeforeDelConfirm stage
    (c) 2 for cancellation by user in response to prompt at BeforeDelConfirm stage
 
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.
 
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

No comments: