In Microsoft Office Access 2007, when the user selects and deletes a record on a form, Access displays a dialog box that asks the user to confirm that she wants to delete the record. In some cases, you may want to suppress this dialogue box. There are two ways that you can prevent this dialogue box from appearing:
-
Cancel the BeforeDelConfirm event. This cancels the deletion. For more information regarding the BeforeDelConfirm event, see BeforeDelConfirm Event.
-
Set the Response argument of the BeforeDelConfirm event procedure to acDataErrContinue. This confirms the deletion.
You can also use the BeforeDelConfirm event procedure to display a custom dialogue box to handle users' responses. You can do this to create a more contextual dialog box for the user. This article illustrates how to use a custom dialog box to ask users whether they want to cancel or proceed with the record deletion.
This section demonstrates how to display a custom dialog box in response to a record deletion. The procedure has two steps.
To display a custom dialog box in response to a record deletion
-
Add a BeforeDelConfirm form event procedure to a form.
-
Add the record deletion confirmation code to the event procedure.
1. Add a BeforeDelConfirm Form Event Procedure
First, load the form and display it in Design View. To do this, right click the form name in the Navigation Pane and select the Design View menu item from the context menu. After the form is displayed in Design View, select the Design tab on the Office Fluent Ribbon and in the Tools group select the Property Sheet button. The form's property sheet appears.
Next, on the form's property sheet, select Form in the Selection type dropdown list, and then select the Event tab to display the form's events. Click the BeforeDelConfirm event and select the small down arrow that is displayed. Click the [Event Procedure] item and then click the ellipsis next to the small down arrow. Microsoft Visual Basic will load and display the body for the Form_BeforeDelConfirm subroutine.
2. Add Code to the BeforeDelConfirm Event Procedure
Next, modify the Form_BeforeDelConfirm event procedure created above so that it uses the MsgBox function to prompt the user for confirmation of the record deletion. The following code checks the return value from the MsgBox call, and if the user selects Yes, the code tells Access to continue with the deletion by setting the Response argument passed to the event procedure equal to acDataErrContinue. If the user selects No, the code cancels the record deletion by setting the Cancel argument passed to the event procedure equal to True.
Private Sub Form_BeforeDelConfirm(Cancel As Integer, _
Response As Integer)
Dim strMessage As String
Dim intResponse As Integer
On Error GoTo ErrorHandler
' Display the custom dialog box.
strMessage = "Would you like to delete the _
current record?"
intResponse = MsgBox(strMessage, vbYesNo + _
vbQuestion, _
"Continue delete?")
' Check the response.
If intResponse = vbYes Then
Response = acDataErrContinue
Else
Cancel = True
End If
Exit Sub
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & _
vbCrLf & Err.Description
End Sub
In many cases, you might want to display a custom dialogue box when Access prompts to confirm a record deletion, rather than the native Yes/No dialogue box that is presented. You can use the BeforeDelConfirm event procedure to create this custom dialogue to handle users' responses. This article illustrates how to use a custom dialog box to ask users whether they want to cancel or proceed with the record deletion.
This article explores how to display a custom dialog box in response to a record deletion on an Access 2007 form.
To display a custom dialog box in response to a record deletion on an Access 2007 form
-
Add a BeforeDelConfirm form event procedure to a form. In this step you add the BeforeDelConfirm event and build the body for the Form_BeforeDelConfirm subroutine.
-
Add the record deletion confirmation code to the event procedure. As part of this final step, you add code to the custom MsgBox form to capture a custom response. The user's selection determines if the record is deleted or if the deletion is canceled.
|