HandsOn Writing the Form Delete Event Procedure

This hands-on uses the Product Categories form created in Hands-On 24-6.

1. In the Visual Basic Editor window, double-click Form_Product Categories, which was created in Hands-On 24-6.

2. Enter the following Form_Delete event procedure below the Form_Dirty event procedure prepared in Hands-On 24-6.

Private Sub Form_Delete(Cancel As Integer) If CategoryID <= 8 Then

MsgBox "You can't delete the original categories." Cancel = True


Event Programming in Forms and Reports

If MsgBox("Do you really want to delete " & _ "this record?", vbOKCancel, _ "Delete Verification") = vbCancel Then Cancel = True

End If End If End Sub

3. To test the above event procedure, open the Product Categories form in Form view.

4. Click on the record selector to the left of the first record and press the Delete key. You can also click the Delete Record button (|j§ ) on the toolbar. At this point Access will execute the code of the Form_Delete event procedure. You should see the message that you cannot delete original product categories.

5. Add a new category and try to delete it. If there is no code in the Form_ BeforeDelConfirm event procedure (see Hands-On 24-8), you will be prompted twice to confirm the deletion.

6. Close the Product Categories form.

0 0

Post a comment