HandsOn Writing the Form BeforeUpdate Event Procedure

This hands-on uses the New Customers form created in Hands-On 24-2.

1. In the Visual Basic Editor window, double-click Form_New Customers.

2. In the Code window, other procedures prepared in Hands-On 24-2 and 24-3 will be listed. Enter the following Form_BeforeUpdate event procedure below the last procedure code.

Private Sub Form_BeforeUpdate(Cancel As Integer) If Not IsNull(Me.CompanyName) Then

Me.CustomerID = Left(CompanyName, 3) & _

Right(CompanyName, 2) MsgBox "You just added Customer ID: " & _ Me.CustomerID

Else

MsgBox "Please enter Company Name.", _

vbOKOnly, "Missing Data" Me.CompanyName.SetFocus Cancel = True End If End Sub

3. To test the above event procedure, open the New Customers form and switch to Form view. Type Event Enterprises in the Company Name box. Click the record selector to move to the next record. The BeforeUpdate event procedure code will run at this point and you will see a message box that displays the custom-generated Customer ID. Click OK to the message. Another message will appear with the number of total records. This message box is generated by the AfterInsert event procedure that was prepared in Hands-On 24-3.

4. Close the New Customers form.

Part IV

0 0

Post a comment