Creating the Event Sub Procedure or Function

You must write code that executes when an event is raised. In Chapter 3, you had to create event procedures to specify what should happen when an event occurs. You now use the same process for custom objects.

From the General Declarations section of the object where you wish to handle the event, you declare the object variable for the class that causes the event using the WithEvents keyword.

Dim WithEvents objCar As clsCar

After the object has been declared using WithEvents, the Visual Basic Editor displays the object in the list of objects and displays the event in the procedure drop-down list, just as it would for any other object. You can select the object and event from the list, and the code template is automatically specified. You then add the code that you want to run when the event occurs.

Private Sub objCar_ActionSuccess(strMessage As String) 'code to execute when event occurs goes here End Sub

You will actually create the event in the sample database momentarily, but first I want to cover a few other topics necessary to write the sample code to handle the event.

0 0

Post a comment