Property Get ID() As Long
ID = m_lngID
End Property
Property Let ID(newID As Long)
m_lngID = newID
End Property
Another advantage class modules give us is the ability to initialize the values of the
module-level variables when an object is instantiated from the class.
5. Choose Class from the Object box in the code pane, as shown in Figure 1-29.
Figure 1-29. Selecting Class from the VBA code window Object box
CHAPTER 1 n THE MACRO RECORDER AND CODE MODULES 32
6. The VBE inserts the Class_Initialize method for you. Add code to set the default values
for the Employee class, as shown in Listing 1-4.
Listing 1-4. Class Initialization Code??”Here It??™s Set to Nonsense Values Useful in
Determining What Properties Have or Have Not Been Set.
Private Sub Class_Initialize()
m_lngID = 0
m_sName = "NOG"
m_sTitle = "NOG"
m_sPhoneNumber = "0000000000"
End Sub
There are two methods included with each class module, the Class_Initialize and the
Class_Terminate methods. It??™s always a good idea to initialize your values so that any clients
of your class have a value to work with.
The Initialize method is a great place to set default values, to open any data sources
or files your class may need, or to perform any other setup that your object may need to do
its job.
The Terminate method, although not always used, is important because it gives you a
place to clean up any data connections or recordsets (or any other objects your class may use)
and close any files you??™ve opened.
Pages:
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59