SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 163 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

In
the Class_Initialize event, add the following code:
Private Sub Class_Initialize()
m_lngID = RandomNumber(100000, 999999)
Set m_oAddress = New cAddress
Set m_oEquipment = New cEquipment
Set m_oAccess = New cAccess
SetObjectIDs
End Sub
We??™re setting our private ID variable, m_lngID, to a random six-digit value, and initializing
our private business object variables. We then call a private function that sets the ID values of
all four of our business objects to the same value, SetObjectIDs. Add the following code to the
cPerson class to generate the random number and synchronize the ID field:
Private Function RandomNumber(upper As Long, lower As Long) As Long
'generates a random number between upper & lower
Randomize
RandomNumber = Int((upper - lower + 1) * Rnd + lower)
End Function
Private Sub SetObjectIDs()
m_oAddress.ID = m_lngID
m_oEquipment.ID = m_lngID
m_oAccess.ID = m_lngID
End Sub
We??™ll also add a call to this procedure in our ID Property Let function. This way, if we
manually assign a value to the ID field, all the business objects will get the new value. The finished
ID Property Let will look like this:
Public Property Let ID(newID As Long)
m_lngID = newID
SetObjectIDs 'keep all objects in sync with the same ID
End Property
CHAPTER 4 n USERFORMS 162
The remainder of the cPerson class is very straightforward. Finish the cPerson class by
adding the following code:
Property Get FName() As String
FName = m_sFName
End Property
Property Let FName(newFName As String)
m_sFName = newFName
End Property
Property Get MidInit() As String
MidInit = m_sMidInit
End Property
Property Let MidInit(newMidInit As String)
m_sMidInit = newMidInit
End Property
Property Get LName() As String
LName = m_sLName
End Property
Property Let LName(newLName As String)
m_sLName = newLName
End Property
Property Get DOB() As Date
DOB = m_dtDOB
End Property
Property Let DOB(newDOB As Date)
m_dtDOB = newDOB
End Property
Property Get SSN() As String
SSN = m_sSSN
End Property
Property Let SSN(newSSN As String)
m_sSSN = newSSN
End Property
Property Get JobTitle() As String
JobTitle = m_sJobTitle
End Property
CHAPTER 4 n USERFORMS 163
Property Let JobTitle(newJobTitle As String)
m_sJobTitle = newJobTitle
End Property
Property Get Department() As String
Department = m_sDepartment
End Property
Property Let Department(newDepartment As String)
m_sDepartment = newDepartment
End Property
Property Get Email() As String
Email = m_sEmail
End Property
Property Let Email(newEmail As String)
m_sEmail = newEmail
End Property
Property Get Address() As cAddress
Set Address = m_oAddress
End Property
Property Set Address(newAddress As cAddress)
Set m_oAddress = newAddress
End Property
Property Get Equipment() As cEquipment
Set Equipment = m_oEquipment
End Property
Property Set Equipment(newEquipment As cEquipment)
Set m_oEquipment = newEquipment
End Property
Property Get Access() As cAccess
Set Access = m_oAccess
End Property
Property Set Access(newAccess As cAccess)
Set m_oAccess = newAccess
End Property
We??™ve added the remaining Person data elements to our class, as well as three object properties
using Property Get/Set statements.


Pages:
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175