HandsOn Writing the Form AfterUpdate Event Procedure

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

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

2. Other procedures that were prepared in Hands-On 24-2 and 24-3 will be listed in the Code window. Enter the following Form_AfterUpdate event procedure below the last procedure code.

3. For this code to work you must set the reference to Microsoft Scripting Runtime in the References dialog box. Choose Tools | References in the Visual Basic Editor window.

Private Sub Form_AfterUpdate() Dim fso As FileSystemObject Dim myFile As Object On Error Resume Next

Set fso = New FileSystemObject

Set myFile = fso.GetFile("C:\MyCust.txt")

If Err.Number = 0 Then ' open text file

Set myFile = fso.OpenTextFile("C:\MyCust.txt", 8)


' create a text file

Set myFile = fso.CreateTextFile("C:\MyCust.txt") End If myFile.writeLine UCase(Me.CustomerID) & _ " Created on: " & Date & " " & Time myFile.Close Set fso = Nothing

End Sub

Event Programming in Forms and Reports

The procedure first checks whether the text file named C:\MyCust.txt exists on your computer. If the file is found, then the Err.Number statement returns zero. At this point you want to open the file. The "8" represents the open mode for appending. You would use "2" if you wanted to replace the contents of a file with the new data.

4. To test the above event procedure, switch to the New Customers form and open it in Form view. Type Time Organizers in the Company Name box. Click the record selector to move to the next record. The BeforeUpdate event procedure code you prepared in Hands-On 24-4 will run at this point and you should see a message box that displays the custom-generated Customer ID. Click OK to the message. The next message box notifies you about the location of the audit trail (the result of the AfterUpdate event procedure prepared in this hands-on). Click OK to the message. Another message will appear with the number of total records. This message box is generated by the Afterlnsert event procedure that was prepared in Hands-On 24-3.

As you enter more customer records using the New Customers form, events are executed in the following order:

Beforelnsert (Hands-On 24-2)

BeforeUpdate (Hands-On 24-4)

AfterUpdate (Hands-On 24-5)

Afterlnsert (Hands-On 24-3)

5. Close the New Customers form.

0 0

Post a comment