home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / dbawarco / vb4oo.bas < prev    next >
Encoding:
BASIC Source File  |  1996-11-20  |  6.7 KB  |  251 lines

  1. Attribute VB_Name = "TestDBAwareCollection"
  2. Option Explicit
  3.  
  4. Public publicCompany As New Company
  5.  
  6. Public CompanyDatabase As Database
  7. Private pvtPerson As Person
  8. Private pvtPersons As New DBAwareCollection
  9. Private pvtAddress As Address
  10. Private pvtPhone As Phone
  11.  
  12. Public George As Person
  13. Public Abe As Person
  14. Public BillClinton As Person
  15. Public HillaryClinton As Person
  16. Public ChelseaClinton As Person
  17.  
  18. Sub CreateTestData()
  19.     
  20. '    Set pvtPersons = Company.Persons.pvtDBSelect()
  21.     Dim tempNewPerson As New Person
  22.  
  23.     On Local Error Resume Next
  24.         
  25.     CompanyDatabase.Execute "DELETE FROM Persons "
  26.     CompanyDatabase.Execute "DELETE FROM Addresses "
  27.     CompanyDatabase.Execute "DELETE FROM Phones "
  28.     CompanyDatabase.Execute "DELETE FROM DBAwareObjectLinks "
  29.  
  30. ' an example if instantiating and populating
  31. '   a Person object.
  32. ' Note: DBAwareCollection automatically creates
  33. '   persistent objects by synchronizing object
  34. '   creations, updates and deletions with
  35. '   corresponding SQL directed at the appropriate
  36. '   table
  37.     Set George = New Person
  38.     With George
  39.         .CustomerNumber = "1363267"
  40.         .FirstName = "George"
  41.         .LastName = "Washington"
  42.         .SSN = 123456789
  43.         .DateOfBirth = #8/29/53#
  44.     End With
  45.  
  46.  ' example of adding the Person to the
  47.  '  Company.Persons DBAwareCollection object
  48.     publicCompany.AddPerson _
  49.         Item:=George
  50.  '  (the Company.AddPerson is a wrapper for the
  51.  '  DBAwareCollection.Add method, which
  52.  '  automatically takes care of the Parent:=
  53.  '  named parameter)
  54.     
  55.     Set Abe = New Person
  56.     With Abe
  57.         .CustomerNumber = "205289"
  58.         .FirstName = "Abe"
  59.         .LastName = "Lincoln"
  60.         .SSN = 234567890
  61.         .DateOfBirth = #11/3/58#
  62.     End With
  63.     publicCompany.AddPerson _
  64.         Item:=Abe
  65.  
  66.     Set pvtAddress = New Address
  67.     With pvtAddress
  68.         .Line1 = "123 Elm"
  69.         .City = "Phoenix"
  70.         .StateCode = "AZ"
  71.         .ZipCode = 80808
  72.         .ZipSupplement = 5591
  73.         .ZipExtension = 22
  74.         .Status = "Current"
  75.         .Usage = "Residence"
  76.     End With
  77.  
  78.     George.AddAddress _
  79.         Item:=pvtAddress
  80.  
  81.     Set pvtAddress = New Address
  82.     With pvtAddress
  83.         .Line1 = "122 Pine"
  84.         .City = "Springfield"
  85.         .StateCode = "IL"
  86.         .ZipCode = 60609
  87.         .Status = "Former"
  88.         .Usage = "Residence"
  89.     End With
  90.     George.AddAddress _
  91.         Item:=pvtAddress
  92.  
  93.     Set pvtPhone = New Phone
  94.     With pvtPhone
  95.         .PhoneNumber = "3035551400"
  96.         .Usage = "Residence"
  97.     End With
  98.     George.AddPhone _
  99.         Item:=pvtPhone
  100.  
  101.     Set pvtPhone = New Phone
  102.     With pvtPhone
  103.         .PhoneNumber = "8185551212"
  104.         .Usage = "Business"
  105.     End With
  106.     George.AddPhone _
  107.         Item:=pvtPhone
  108.         
  109. ' example of containing one of George's
  110. '   instantiated Address objects within the
  111. '   Abe.Addresses DBAwareCollection object.
  112. ' Note: there is no new object instantiated due
  113. '   to this operation -- both DBAwareCollections
  114. '   contain references to the same object
  115.     Abe.AddAddress _
  116.         Item:=George.Addresses(1)
  117.  
  118. ' example of containing one of George's
  119. '   instantiated Phone objects within the
  120. '   Abe.Phones DBAwareCollection object.
  121. ' Note: there is no new object instantiated due
  122. '   to this operation -- both DBAwareCollections
  123. '   contain references to the same object
  124.     Abe.AddPhone _
  125.         Item:=George.Phones(1)
  126.         
  127.     Set pvtAddress = New Address
  128.     With pvtAddress
  129.         .Line1 = "401 Maple"
  130.         .City = "Boston"
  131.         .StateCode = "MA"
  132.         .ZipCode = 6709
  133.         .Status = "Former"
  134.         .Usage = "Residence"
  135.     End With
  136.     Abe.AddAddress _
  137.         Item:=pvtAddress
  138.  
  139.     Set pvtPhone = New Phone
  140.     With pvtPhone
  141.         .PhoneNumber = "2024441578"
  142.         .Usage = "Business"
  143.     End With
  144.     Abe.AddPhone _
  145.         Item:=pvtPhone
  146.  
  147.     Set pvtPhone = New Phone
  148.     With pvtPhone
  149.         .PhoneNumber = "9132242668"
  150.         .Usage = "Fax"
  151.     End With
  152.     Abe.AddPhone _
  153.         Item:=pvtPhone
  154.  
  155. ' add filler data
  156.     Set BillClinton = New Person
  157.     With BillClinton
  158.         .CustomerNumber = "1"
  159.         .FirstName = "Bill"
  160.         .LastName = "Clinton"
  161.         .SSN = 123456789
  162.         .DateOfBirth = #1/3/48#
  163.     End With
  164.     publicCompany.AddPerson _
  165.         Item:=BillClinton
  166.  
  167.     Set HillaryClinton = New Person
  168.     With HillaryClinton
  169.         .CustomerNumber = "2"
  170.         .FirstName = "Hillary"
  171.         .LastName = "Clinton"
  172.         .SSN = 234567890
  173.         .DateOfBirth = #6/22/49#
  174.     End With
  175.     publicCompany.AddPerson _
  176.         Item:=HillaryClinton
  177.  
  178.     Set ChelseaClinton = New Person
  179.     With ChelseaClinton
  180.         .CustomerNumber = "3"
  181.         .FirstName = "Chelsea"
  182.         .LastName = "Clinton"
  183.         .SSN = 345678901
  184.         .DateOfBirth = #8/29/80#
  185.     End With
  186.     publicCompany.AddPerson _
  187.         Item:=ChelseaClinton
  188.     
  189.     Set pvtAddress = New Address
  190.     With pvtAddress
  191.         .Line1 = "123 Pennsylvannia Ave."
  192.         .City = "Washington"
  193.         .StateCode = "DC"
  194.         .ZipCode = 12345
  195.         .ZipSupplement = 1001
  196.         .Status = "Current"
  197.         .Usage = "Primary"
  198.     End With
  199.     BillClinton.AddAddress _
  200.         Item:=pvtAddress
  201.  
  202.     HillaryClinton.AddAddress _
  203.         Item:=BillClinton.Addresses(1)
  204.  
  205.     ChelseaClinton.AddAddress _
  206.         Item:=BillClinton.Addresses(1)
  207.     
  208. ' example of an on-demand creation of a
  209. '   DBAwareCollection of instantiated objects,
  210. '   based on the contents of the database.
  211. ' this is an example of how DBAwareCollection
  212. '   provides object persistence by re-instantiating
  213. '   objects from the database at some time after
  214. '   their initial instantiation.
  215. ' Note: this uses the
  216. '   DBAwareCollection.InstantiateFromDatabase
  217. '   method
  218. ' gather all of the above individuals and
  219. '   place them in pvtPersons
  220. '    publicCompany.EmptyPersons
  221.     Set pvtPersons = _
  222.         pvtPersons.InstantiateFromDatabase( _
  223.             Database:=CompanyDatabase, _
  224.             Parent:=publicCompany, _
  225.             SampleObject:=tempNewPerson)
  226.  
  227.     Abe.AddAddress _
  228.         Item:=BillClinton.Addresses(1)
  229.         
  230. ' example of DBAwareCollection.Replace to
  231. '   replace an object in a DBAwareCollection
  232.     Abe.Addresses.Replace _
  233.         Item:=Abe.Addresses(3), _
  234.         ReplaceWith:=George.Addresses(2)
  235. End Sub
  236.  
  237. Sub Main()
  238.  
  239.     Set CompanyDatabase = DBEngine.Workspaces(0). _
  240.         OpenDatabase(App.Path & "\VB4OO.MDB")
  241.     
  242.     Set publicCompany = New Company
  243.         
  244.     pvtPersons.SetDatabaseParameters _
  245.         Database:=CompanyDatabase, _
  246.         OrderByClause:="LastName ASC, FirstName ASC"
  247.  
  248.     Intro.Show
  249.  
  250. End Sub
  251.