HandsOn Writing the Form Error Event Procedure

1. Create a new form based on the Customers table. Add all the fields from the Customers table and save the new form as Customers Data Entry.

2. In the Design view of the Customers Data Entry form, choose Edit | Select Form.

3. Choose View | Properties to activate the Form properties sheet.

4. Click the Data tab and set the form's DataEntry property to Yes.

5. Click the Event tab, set the On Error property to [Event Procedure], and press the Build button (...).

6. Access will create the event procedure stub. Enter the following Form_Error event procedure

Private Sub Form_Error(DataErr As Integer, Response As Integer) Dim strMsg As String Dim custId As String

Const conDuplicateKey = 3022 custId = Me.CustomerID

If DataErr = conDuplicateKey Then

' Don't show built-in error messages Response = acDataErrContinue strMsg = "Customer " & custId & " already exists." ' Show a custom error message MsgBox strMsg, vbCritical, "Duplicate Value" End If End Sub

7. Open the Customers Data Entry form in Form view mode.

8. Enter ALFKI in the Customerld field and Alfred Fiki in the Company Name field. When you try to save this record, the Form_Error event procedure code will cause a message box to appear, saying that the customer already exists. Click OK to the message. Press Esc to cancel the changes to this record.

9. Close the Customers Data Entry form.

Part IV

0 0

Post a comment