home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / dbawarco / person.cls < prev    next >
Encoding:
Text File  |  1996-11-20  |  5.7 KB  |  208 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Person"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. Public CustomerNumber As Long
  11. Public FirstName As String
  12. Public LastName As String
  13. Public SSN As Long
  14. Public Sex As String
  15. Public DateOfBirth As Variant
  16. Public MaritalStatus As String
  17. Public ObjectID As Long
  18.  
  19. Private pvtAddressesCollection As New DBAwareCollection
  20. Private pvtPhonesCollection As New DBAwareCollection
  21.  
  22. Public Function AddPhone(Optional ByVal Item As Variant, Optional ByVal Parent As Variant) As DBAwareCollection
  23. ' Return a Phone, added to my pvtPhonesCollection
  24. '   (just a wrapper method for Phones.Add)
  25.  
  26.     Set AddPhone = Me.Phones.Add( _
  27.         Item:=Item, _
  28.         Parent:=Me)
  29. End Function
  30.  
  31. Public Function InitializeFromRecordSet(Optional ByVal RecordSet As Variant) As Person
  32. ' Populate my variables from the RecordSet
  33. '   (in support of DBAwareCollection)
  34.  
  35.     On Local Error Resume Next
  36.     
  37.     CustomerNumber = RecordSet("CustomerNumber")
  38.     FirstName = RecordSet("FirstName")
  39.     LastName = RecordSet("LastName")
  40.     SSN = RecordSet("SSN")
  41.     Sex = RecordSet("Sex")
  42.     DateOfBirth = RecordSet("DateOfBirth")
  43.     MaritalStatus = RecordSet("MaritalStatus")
  44.     ObjectID = RecordSet("ObjectID")
  45.  
  46.     Set InitializeFromRecordSet = Me
  47. End Function
  48.  
  49.  
  50. Public Function InitializeRecordSet(Optional ByVal RecordSet As Variant) As Long
  51. ' Populate the RecordSet with my variables.
  52. '   Do not initialize the ObjectID column.
  53. '   Return any error code encountered.
  54. '   (in support of DBAwareCollection)
  55.  
  56.     On Local Error GoTo InitializeRecordSet_SetError
  57.     Err = 0
  58.     
  59.     RecordSet("CustomerNumber") = CustomerNumber
  60.     RecordSet("FirstName") = FirstName
  61.     RecordSet("LastName") = LastName
  62.     RecordSet("SSN") = SSN
  63.     RecordSet("Sex") = Sex
  64.     RecordSet("DateOfBirth") = DateOfBirth
  65.     RecordSet("MaritalStatus") = MaritalStatus
  66.     
  67.     GoTo InitializeRecordSet_SetError
  68.  
  69. InitializeRecordSet_SetError:
  70.     InitializeRecordSet = Err
  71.     Exit Function
  72. End Function
  73.  
  74. Public Function TableName() As String
  75. ' Return the Table with which this Class is associated
  76. '   (in support of DBAwareCollection)
  77.     
  78.     TableName = "Persons"
  79. End Function
  80. Public Function NewInstanceOfMyClass() As Person
  81. ' Return a new instance of this class
  82. '   (in support of DBAwareCollection)
  83.  
  84.     Set NewInstanceOfMyClass = New Person
  85. End Function
  86.  
  87.  
  88. Public Function AddAddress(Optional ByVal Item As Variant, Optional ByVal Parent As Variant) As DBAwareCollection
  89. ' Return an Address, added to my pvtAddressesCollection
  90. '   (just a wrapper method for Addresses.Add)
  91.  
  92.     Set AddAddress = Me.Addresses.Add( _
  93.         Item:=Item, _
  94.         Parent:=Me)
  95. End Function
  96.  
  97.  
  98.  
  99. Public Function Addresses(Optional ByVal ObjectID As Variant) As Variant
  100. ' Returns a DBAwareCollection of Address objects which are
  101. '   contained by this Person object,
  102. ' or
  103. ' Returns an Address object whose ObjectID matches the ObjectID
  104. '   parameter.
  105.  
  106.     Dim tempNewAddress As New Address
  107.  
  108.     On Local Error Resume Next
  109.     
  110. ' check for need to populate the collection from the database
  111.     If Not pvtAddressesCollection.DatabaseHasBeenReferenced Then
  112.         pvtAddressesCollection.SetDatabaseParameters _
  113.             Database:=CompanyDatabase
  114.             
  115.         Set pvtAddressesCollection = _
  116.             pvtAddressesCollection.InstantiateFromDatabase( _
  117.                 Parent:=Me, _
  118.                 SampleObject:=tempNewAddress)
  119.     End If
  120.     
  121. ' check for a request for a specific Address
  122.     If Not IsMissing(ObjectID) Then
  123.         Set Addresses = pvtAddressesCollection. _
  124.                         Item(ObjectID)
  125.  
  126. ' else, return the entire collection
  127.     Else
  128.         Set Addresses = pvtAddressesCollection
  129.     End If
  130. End Function
  131.  
  132. Public Function Phones(Optional ByVal ObjectID As Variant) As Variant
  133. ' Returns a DBAwareCollection of Phone objects which are
  134. '   contained by this Person object,
  135. ' or
  136. ' Returns a Phone object whose ObjectID matches the ObjectID
  137. '   parameter.
  138.  
  139.     Dim tempNewPhone As New Phone
  140.  
  141.     On Local Error Resume Next
  142.     
  143. ' check for need to populate the collection from the database
  144.     If Not pvtPhonesCollection.DatabaseHasBeenReferenced Then
  145.         pvtPhonesCollection.SetDatabaseParameters _
  146.             Database:=CompanyDatabase
  147.             
  148.         Set pvtPhonesCollection = _
  149.             pvtPhonesCollection.InstantiateFromDatabase( _
  150.                 Parent:=Me, _
  151.                 SampleObject:=tempNewPhone)
  152.     End If
  153.     
  154. ' check for a request for a specific Phone
  155.     If Not IsMissing(ObjectID) Then
  156.         Set Phones = pvtPhonesCollection. _
  157.                         Item(ObjectID)
  158.  
  159. ' else, return the entire collection
  160.     Else
  161.         Set Phones = pvtPhonesCollection
  162.     End If
  163.  
  164. End Function
  165.  
  166. Public Function FormattedName() As String
  167.  
  168.     Dim ReturnString As String
  169.     
  170.     If UCase$(Left$(Sex, 1)) = "M" Then
  171.         ReturnString = "Mr. "
  172.     ElseIf UCase$(Left$(Sex, 1)) = "F" Then
  173.         ReturnString = "Ms. "
  174.     End If
  175.     
  176.     FormattedName = ReturnString & FirstName & " " & LastName
  177. End Function
  178.  
  179.  
  180. Private Sub Class_Initialize()
  181.  
  182.     CustomerNumber = -1
  183.     FirstName = ""
  184.     LastName = ""
  185.     SSN = -1
  186.     DateOfBirth = ""
  187.     ObjectID = -1
  188.     
  189.     Set pvtAddressesCollection = Nothing
  190. End Sub
  191.  
  192.  
  193.  
  194. Public Function Age() As Double
  195.     If DateOfBirth = "" Then
  196.         Age = 0
  197.     Else
  198.         Age = DateDiff("yyyy", DateOfBirth, Now)
  199.     End If
  200. End Function
  201.  
  202. Public Function ObjectType() As String
  203. ' Return the type of this Class
  204. '   (in support of DBAwareCollection)
  205.     
  206.     ObjectType = "Person"
  207. End Function
  208.