Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources]

Alison Balter

نسخه متنی -صفحه : 544/ 221
نمايش فراداده

The Initialize and Terminate Events

The Initialize and Terminate events are the two built-in events that execute for a class object. The Initialize event executes as the class is instantiated, and the Terminate event executes as the class is destroyed.

Initialize

The Initialize event is generally used to perform tasks such as establishing a connection to a database and initializing variables. The following is an example of the use of the Initialize event:

Private Sub Class_Initialize() FirstName = "Alison" LastName = "Balter" End Sub

In this example, the code sets the default values of the FirstName and LastName properties of the class to Alison and Balter, respectively.

Terminate

You generally use the Terminate event to perform the class's cleanup tasks. An example is closing a recordset used by the class. The following is an example of the use of the Terminate event:

Private Sub Class_Terminate() rstCustomer.Close Set rstCustomer = Nothing End Sub

This code closes the recordset and destroys the recordset object variable. It provides an example of cleanup code that you would place in your own application.