HandsOn Writing the Form AfterInsert 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, you will see the event procedure prepared in Hands-On 24-2. Below this procedure code, enter the Form_AfterInsert event procedure as shown below.

Private Sub Form_AfterInsert()

Dim conn As New ADODB.Connection Dim rst As New ADODB.Recordset Set conn = CurrentProject.Connection rst.CursorType = adOpenKeyset rst.Open "Customers", conn MsgBox "Added " & rst.RecordCount & _ "th customer."

rst.Close Set rst = Nothing conn.Close Set conn = Nothing End Sub

3. To test the above event procedure, open the New Customers form in Form view. Type TRYIT in the Customer ID text box. Type Test Events in the Company Name text box. Use the record selector to move to the next

Event Programming in Forms and Reports record. Access executes the code in the Form_AfterInsert event procedure and displays the total number of records.

0 0

Post a comment