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

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "VBOFDataWrapper"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' (c) Copyright 1995 Ken Fitzpatrick
  11. '     All Rights Reserved
  12. '     Cannot be distributed or sold without permission
  13. '
  14. ' VBOFDataWrapper is a supplemental GUI
  15. '   Control Wrapper for Microsoft Visual Basic 4.0.
  16. '   It is valid only in conjunction with the
  17. '   following Classes Modules:
  18. '       VBOFCollection
  19. '       VBOFObjectLink
  20. '       VBOFObjectManager
  21.  
  22. ' VBOFDataWrapper is a wrapper class for
  23. '   providing automatic interfacing between a
  24. '   RecordSet VB control and an underlying
  25. '   VBOFCollection
  26.  
  27. Private pvtVBOFObjectManager As VBOFObjectManager
  28. Private pvtCollection As VBOFCollection
  29. Private pvtDataControl As Variant
  30. Private pvtSupportedTypeNames As String
  31. Private pvtDataControlSupportedTypeNames As String
  32. Private pvtCollectionSupportedTypeNames As String
  33. Private pvtPreviousDataControlActionCode As Long
  34.  
  35. Public ObjectID As Long
  36.  
  37. Public Property Get AbsolutePositionObject() As Variant
  38. ' Returns the object at the AbsolutionPosition (+ 1)
  39. '   of the underlying RecordSet
  40.     
  41.     Dim tempLong As Long
  42.     
  43.     On Local Error Resume Next
  44.  
  45.     tempLong = AbsolutePosition
  46.  
  47.     If tempLong >= 0 Then
  48.         Set AbsolutePositionObject = _
  49.             pvtCollection.Item _
  50.                 (tempLong + 1)
  51.     Else
  52.         Set AbsolutePositionObject = _
  53.             Nothing
  54.     End If
  55. End Property
  56.  
  57.  
  58. Public Property Set AbsolutePositionObject(Object As Variant)
  59. ' Sets the AbsolutionPosition (+ 1) of the
  60. '   underlying RecordSet to correspond to the
  61. '   object
  62.  
  63.     Dim tempLong As Long
  64.     
  65.     On Local Error Resume Next
  66.     
  67.     tempLong = _
  68.         pvtCollection.CollectionIndex _
  69.             (Item:=Object)
  70.  
  71.     If tempLong > 0 Then
  72.         AbsolutePosition = tempLong - 1
  73.     End If
  74. End Property
  75.  
  76.  
  77.  
  78. Public Function CloseRecordSet() As Long
  79. ' Closes the underlying RecordSet
  80.  
  81. ' bullet-proofing
  82.     If Not pvtIsFullyInitialized _
  83.         (Verbose:=True) _
  84.     Then
  85.         CloseRecordSet = -1
  86.         Exit Function
  87.     End If
  88.     
  89.     CloseRecordSet = _
  90.         pvtCollection. _
  91.             pvtCloseRecordSet()
  92. End Function
  93. Public Function Clone() As RecordSet
  94. ' Returns a cloned RecordSet of the underlying
  95. '   RecordSet object
  96.  
  97. ' bullet-proofing
  98.     If Not pvtIsFullyInitialized _
  99.         (Verbose:=True) _
  100.     Then
  101.         Set Clone = Nothing
  102.         Exit Function
  103.     End If
  104.     
  105.     Set Clone = _
  106.         pvtCollection.pvtCloneRecordSet()
  107. End Function
  108.  
  109.  
  110. Public Property Get Collection() As Variant
  111. ' Returns my VBOFCollection object
  112.  
  113.     Set Collection = pvtCollection
  114. End Property
  115. Public Property Set Collection(Collection As Variant)
  116.     
  117.     If Collection Is Nothing Then
  118.         Set pvtCollection = Nothing
  119.         Exit Property
  120.     End If
  121.     
  122.     pvtVerifyCollection _
  123.         Collection:=Collection, _
  124.         Verbose:=True
  125.  
  126. '    Set pvtCollection = Collection
  127. End Property
  128.  
  129. Public Property Get DataControl() As Variant
  130.     Set DataControl = pvtDataControl
  131. End Property
  132.  
  133. Public Property Set DataControl(DataControl As Variant)
  134.     pvtVerifyDataControl _
  135.         DataControl:=DataControl, _
  136.         Verbose:=True
  137. End Property
  138.  
  139. Public Property Get ObjectManager() As VBOFObjectManager
  140. ' Return my reference to the VBOFObjectManager
  141.     
  142.     Set ObjectManager = pvtVBOFObjectManager
  143. End Property
  144. Public Property Set ObjectManager(anObjectManager As VBOFObjectManager)
  145. ' Set my reference to the VBOFObjectManager
  146.     
  147.     Set pvtVBOFObjectManager = anObjectManager
  148. End Property
  149.  
  150. Public Function MoveToItem(Optional Item As Variant) As Variant
  151. ' Positions the underlying RecordSet to the
  152. '   specifed Item and returns the Item
  153.  
  154. ' bullet-proofing
  155.     If Not pvtIsFullyInitialized _
  156.         (Verbose:=True) _
  157.     Then
  158.         Set MoveToItem = Nothing
  159.         Exit Function
  160.     End If
  161.  
  162.     Set MoveToItem = _
  163.         pvtCollection. _
  164.             pvtRecordSetPositionToItem _
  165.                     (Item:=Item)
  166. End Function
  167.  
  168. Public Function MoveToObject(Optional Object As Variant) As Variant
  169. ' Positions the underlying RecordSet to the
  170. '   specifed Object and returns the Object
  171.  
  172. ' bullet-proofing
  173.     If Not pvtIsFullyInitialized _
  174.         (Verbose:=True) _
  175.     Then
  176.         Set MoveToObject = Nothing
  177.         Exit Function
  178.     End If
  179.  
  180.     Set MoveToObject = _
  181.         pvtCollection. _
  182.             pvtRecordSetPositionToItem _
  183.                     (Item:=Object)
  184. End Function
  185.  
  186. Private Sub pvtRefreshDataControl()
  187. ' Set the DataControl.RecordSet to the
  188. '   Collection.RecordSet
  189.  
  190.     Dim tempLong As Long
  191.  
  192.     On Local Error Resume Next
  193.  
  194.     If Not pvtIsFullyInitialized() _
  195.     Then
  196.         Exit Sub
  197.     End If
  198.     
  199.     tempLong = pvtDataControl.RecordSet.AbsolutePosition
  200.     pvtCollection.RecordSet.AbsolutePosition = tempLong
  201.     
  202.     Set pvtDataControl.RecordSet = _
  203.         pvtCollection.RecordSet
  204.  
  205. End Sub
  206.  
  207. Private Function pvtVerifyDataControl(Optional DataControl As Variant, Optional Verbose As Variant) As Boolean
  208.     pvtVerifyDataControl = _
  209.         ObjectManager. _
  210.             pvtWrapperVerifyControl( _
  211.                 Control:=DataControl, _
  212.                 pvtControl:=pvtDataControl, _
  213.                 Verbose:=Verbose)
  214. End Function
  215.  
  216. Private Function pvtVerifyCollection(Optional Collection As Variant, Optional Verbose As Variant) As Boolean
  217.     pvtVerifyCollection = _
  218.         ObjectManager. _
  219.             pvtWrapperVerifyCollection( _
  220.                 Collection:=Collection, _
  221.                 pvtCollection:=pvtCollection, _
  222.                 Verbose:=Verbose, _
  223.                 WrapperName:="Data")
  224. End Function
  225.  
  226.  
  227. Private Function pvtErrorMessage(Optional ErrorMessage As Variant) As Long
  228.     pvtErrorMessage = _
  229.         pvtVBOFObjectManager.DisplayErrorMessage _
  230.             (ErrorMessage)
  231. End Function
  232. Private Function pvtIsFullyInitialized(Optional Collection As Variant, Optional DataControl As Variant, Optional Verbose As Variant) As Boolean
  233.     
  234.     If Not pvtVerifyCollection( _
  235.         Collection:=Collection, _
  236.         Verbose:=Verbose) _
  237.     Then
  238.         pvtIsFullyInitialized = False
  239.         Exit Function
  240.     End If
  241.     
  242.     If Not pvtVerifyDataControl( _
  243.         DataControl:=DataControl, _
  244.         Verbose:=Verbose) _
  245.     Then
  246.         pvtIsFullyInitialized = False
  247.         Exit Function
  248.     End If
  249.  
  250.     pvtIsFullyInitialized = True
  251. End Function
  252.  
  253.  
  254. Private Function pvtUseDataControl(Optional DataControlParm As Variant, Optional Verbose As Variant) As Variant
  255.     Set pvtUseDataControl = _
  256.         ObjectManager. _
  257.             pvtWrapperUseControl( _
  258.                 ControlParm:=DataControlParm, _
  259.                 pvtControl:=pvtDataControl, _
  260.                 SupportedNames:=pvtDataControlSupportedTypeNames, _
  261.                 Verbose:=Verbose, _
  262.                 WrapperName:="Data")
  263. End Function
  264.  
  265. Private Function pvtUseCollection(Optional CollectionParm As Variant, Optional Verbose As Variant) As Variant
  266.     Set pvtUseCollection = _
  267.         ObjectManager. _
  268.             pvtWrapperUseCollection( _
  269.                 CollectionParm:=CollectionParm, _
  270.                 pvtCollection:=pvtCollection, _
  271.                 Verbose:=Verbose, _
  272.                 WrapperName:="Data")
  273. End Function
  274.  
  275.  
  276. Public Function Rebind(Optional Collection As Variant, Optional DataControl As Variant) As Variant
  277. ' Rebinds the Wrapper to a Collection or DataControl
  278. '   after having changed the assignment of either.
  279. '   For example, in the following scenario, the
  280. '   VBOFDataWrapper must be rebound because
  281. '   the VBOFCollection has been significantly altered:
  282. '
  283. '   Dim pvtAddresses as VBOFCollection
  284. '   Dim pvtPerson as Person
  285. '   Dim MyDataWrapper as VBOFDataWrapper
  286. '   Set MyDataWrapper = _
  287. '       ObjectManager.NewVBOFDataWrapper ( _
  288. '           Collection:=pvtAddresses, _
  289. '           DataControl:=MyDataControl)
  290. '
  291. ' the following line alters the state of the data
  292. ' in-effect at the time of the above binding
  293. '   Set pvtAddresses = pvtPerson.Addresses
  294. ' rebind the Wrapper
  295. '   MyDataWrapper.Rebind _
  296. '           Collection:=pvtAddresses
  297.     
  298. ' bullet-proofing
  299.     If Not IsMissing(Collection) Then
  300.         If TypeName(Collection) <> "VBOFCollection" Then
  301.             pvtErrorMessage TypeName(Me) & " cannot process the '.Rebind' method because the 'Collection:=' parameter is not a VBOFCollection."
  302.             Rebind = False
  303.             Exit Function
  304.         End If
  305.     End If
  306.     If Not IsMissing(DataControl) Then
  307.         If InStr(pvtDataControlSupportedTypeNames, TypeName(pvtDataControl)) = 0 Then
  308.             pvtErrorMessage TypeName(Me) & " cannot process the '.Rebind' method because the 'DataControl:=' parameter is not a Visual Basic DataControl control.  Please use a VBOF Wrapper for the " & TypeName(DataControl) & " control (or request the development of one.)"
  309.             Rebind = False
  310.             Exit Function
  311.         End If
  312.     End If
  313.     If Not pvtIsFullyInitialized( _
  314.         Collection:=Collection, _
  315.         DataControl:=DataControl, _
  316.         Verbose:=False) _
  317.     Then
  318.         Rebind = False
  319.         Exit Function
  320.     End If
  321.  
  322.     pvtRefreshDataControl
  323.     
  324.     Rebind = True
  325. End Function
  326.  
  327. Public Function RecordSet() As RecordSet
  328. ' Returns a DataControl-ready RecordSet object
  329. '   which pertains to the collection of objects
  330. '   instantiated and contained within this
  331. '   VBOFCollection
  332.  
  333. ' bullet-proofing
  334.     If Not pvtIsFullyInitialized _
  335.         (Verbose:=True) _
  336.     Then
  337.         Set RecordSet = Nothing
  338.         Exit Function
  339.     End If
  340.     
  341.     Set RecordSet = _
  342.         pvtCollection.RecordSet
  343. End Function
  344.  
  345. Public Property Get AbsolutePosition() As Long
  346. ' Pass-thru to pvtRecordSetMoveToRecordNumber
  347.     
  348.     On Local Error Resume Next
  349.  
  350. ' bullet-proofing
  351.     If Not pvtIsFullyInitialized _
  352.         (Verbose:=True) _
  353.     Then
  354.         AbsolutePosition = -1
  355.         Exit Property
  356.     End If
  357.  
  358.     AbsolutePosition = _
  359.         pvtDataControl. _
  360.             RecordSet.AbsolutePosition
  361. End Property
  362.  
  363. Public Property Let AbsolutePosition(RecordNumber As Long)
  364. ' Pass-thru to pvtRecordSetMoveToRecordNumber
  365.     
  366.     On Local Error Resume Next
  367.  
  368. ' bullet-proofing
  369.     If Not pvtIsFullyInitialized _
  370.         (Verbose:=True) _
  371.     Then
  372.         Exit Property
  373.     End If
  374.  
  375.     pvtDataControl. _
  376.         RecordSet.AbsolutePosition = _
  377.             RecordNumber
  378. End Property
  379.  
  380. Public Function EOF() As Boolean
  381. ' Returns a boolean, based on whether or not the
  382. ' underlying RecordSet is positioned at EOF
  383.  
  384. ' bullet-proofing
  385.     If Not pvtIsFullyInitialized _
  386.         (Verbose:=True) _
  387.     Then
  388.         EOF = False
  389.         Exit Function
  390.     End If
  391.     
  392.     EOF = _
  393.         pvtCollection. _
  394.             pvtRecordSetEOF
  395. End Function
  396. Public Function FindFirst(Optional SearchCriteria As Variant) As Variant
  397. ' Searches the underlying RecordSet for the first
  398. '   record meeting the specified criteria
  399. '   and returns the object for that row
  400.  
  401. ' bullet-proofing
  402.     If Not pvtIsFullyInitialized _
  403.         (Verbose:=True) _
  404.     Then
  405.         Set FindFirst = Nothing
  406.         Exit Function
  407.     End If
  408.  
  409.     Set FindFirst = _
  410.         pvtCollection. _
  411.             pvtRecordSetFindFirst _
  412.                 (SearchCriteria:=SearchCriteria)
  413. End Function
  414.  
  415. Public Function FindLast(Optional SearchCriteria As Variant) As Variant
  416. ' Searches the underlying RecordSet for the last
  417. '   record meeting the specified criteria
  418. '   and returns the object for that row
  419.  
  420. ' bullet-proofing
  421.     If Not pvtIsFullyInitialized _
  422.         (Verbose:=True) _
  423.     Then
  424.         Set FindLast = Nothing
  425.         Exit Function
  426.     End If
  427.  
  428.     Set FindLast = _
  429.         pvtCollection. _
  430.             pvtRecordSetFindLast _
  431.                 (SearchCriteria:=SearchCriteria)
  432. End Function
  433.  
  434. Public Function FindPrevious(Optional SearchCriteria As Variant) As Variant
  435. ' Searches the underlying RecordSet for the previous
  436. '   record meeting the specified criteria
  437. '   and returns the object for that row
  438.  
  439. ' bullet-proofing
  440.     If Not pvtIsFullyInitialized _
  441.         (Verbose:=True) _
  442.     Then
  443.         Set FindPrevious = Nothing
  444.         Exit Function
  445.     End If
  446.  
  447.     Set FindPrevious = _
  448.         pvtCollection. _
  449.             pvtRecordSetFindPrevious _
  450.                 (SearchCriteria:=SearchCriteria)
  451. End Function
  452.  
  453. Public Function FindNext(Optional SearchCriteria As Variant) As Variant
  454. ' Searches the underlying RecordSet for the next
  455. '   record meeting the specified criteria
  456. '   and returns the object for that row
  457.  
  458. ' bullet-proofing
  459.     If Not pvtIsFullyInitialized _
  460.         (Verbose:=True) _
  461.     Then
  462.         Set FindNext = Nothing
  463.         Exit Function
  464.     End If
  465.  
  466.     Set FindNext = _
  467.         pvtCollection. _
  468.             pvtRecordSetFindNext _
  469.                 (SearchCriteria:=SearchCriteria)
  470. End Function
  471.  
  472. Public Function MoveFirst() As Variant
  473. ' Moves the underlying RecordSet to the first record
  474. '   and returns the object for that row
  475.  
  476. ' bullet-proofing
  477.     If Not pvtIsFullyInitialized _
  478.         (Verbose:=True) _
  479.     Then
  480.         Set MoveFirst = Nothing
  481.         Exit Function
  482.     End If
  483.  
  484.     Set MoveFirst = _
  485.         pvtCollection. _
  486.             pvtRecordSetMoveFirst
  487. End Function
  488.  
  489. Public Function MoveLast() As Variant
  490. ' Moves the underlying RecordSet to the Last record
  491. '   and returns the object for that row
  492.  
  493. ' bullet-proofing
  494.     If Not pvtIsFullyInitialized _
  495.         (Verbose:=True) _
  496.     Then
  497.         Set MoveLast = Nothing
  498.         Exit Function
  499.     End If
  500.  
  501.     Set MoveLast = _
  502.         pvtCollection. _
  503.             pvtRecordSetMoveLast
  504. End Function
  505. Public Function MoveToRecordNumber(Optional RecordNumber As Variant) As Variant
  506. ' Moves the underlying RecordSet to the specified
  507. '   record (by number) and returns the object for
  508. '   that row
  509.  
  510. ' bullet-proofing
  511.     If Not pvtIsFullyInitialized _
  512.         (Verbose:=True) _
  513.     Then
  514.         Set MoveToRecordNumber = Nothing
  515.         Exit Function
  516.     End If
  517.  
  518.     Set MoveToRecordNumber = _
  519.         pvtCollection. _
  520.             pvtRecordSetMoveToRecordNumber _
  521.                 (RecordNumber:=RecordNumber)
  522. End Function
  523.  
  524. Public Function RecordCount() As Long
  525. ' Returns the RecordCount property of the
  526. ' underlying RecordSet
  527.  
  528. ' bullet-proofing
  529.     If Not pvtIsFullyInitialized _
  530.         (Verbose:=True) _
  531.     Then
  532.         RecordCount = -1
  533.         Exit Function
  534.     End If
  535.     
  536.     RecordCount = _
  537.         pvtCollection. _
  538.             pvtRecordSetRecordCount()
  539. End Function
  540.  
  541. Public Function Refresh(Optional DisplayOnly As Variant) As RecordSet
  542. ' Refresh the DataControl
  543.  
  544. ' Pass thru to pvtRefreshRecordSet()
  545.  
  546. ' bullet-proofing
  547.     If Not pvtIsFullyInitialized _
  548.         (Verbose:=True) _
  549.     Then
  550.         Set Refresh = Nothing
  551.         Exit Function
  552.     End If
  553.  
  554.     pvtRefreshDataControl
  555.     
  556.     If Not IsMissing(DisplayOnly) Then
  557.         If DisplayOnly Then
  558.             Set Refresh = _
  559.                 pvtCollection.RecordSet
  560.         Else
  561.             Set Refresh = _
  562.                 pvtCollection.pvtRecordSetRefresh
  563.         End If
  564.     Else
  565.         Set Refresh = _
  566.             pvtCollection.pvtRecordSetRefresh
  567.     End If
  568. End Function
  569.  
  570. Public Function Unbind() As Boolean
  571.  
  572.     Set pvtCollection = Nothing
  573.     Set pvtDataControl = Nothing
  574.     Set pvtVBOFObjectManager = Nothing
  575.  
  576. End Function
  577.  
  578.  
  579. Public Function BOF() As Boolean
  580. ' Returns a boolean, based on whether or not the
  581. ' underlying RecordSet is positioned at BOF
  582.  
  583. ' bullet-proofing
  584.     If Not pvtIsFullyInitialized _
  585.         (Verbose:=True) _
  586.     Then
  587.         BOF = False
  588.         Exit Function
  589.     End If
  590.     
  591.     BOF = _
  592.         pvtCollection. _
  593.             pvtRecordSetBOF
  594. End Function
  595.  
  596.  
  597.  
  598. Public Function Validate(Action As Integer, Save As Integer, Optional Sample As Variant, Optional Parent As Variant) As Variant
  599. ' Manages the Data1_Validate event procedure for
  600. '   the bound Data control
  601. '
  602. ' Programming example:
  603. '   Private Sub Data1_Validate(Action As Integer, Save As Integer)
  604. '       pvtPersonsDataWrapper. _
  605. '            Validate _
  606. '                Action:=Action, _
  607. '                Save:=Save
  608.  
  609. ' NOT YET SUPPORTED
  610. Exit Function
  611.     Dim tempActionCode As Long
  612.  
  613.     On Local Error Resume Next
  614.     
  615. ' make sure the Action is one that is handled
  616.     If Action = vbDataActionUpdate Then
  617.         If pvtPreviousDataControlActionCode = vbDataActionAddNew Then
  618.             tempActionCode = vbDataActionAddNew
  619.         Else
  620.             tempActionCode = vbDataActionUpdate
  621.         End If
  622.     ElseIf Action = vbDataActionDelete Then
  623.         Action = vbDataActionDelete
  624.     Else
  625.         pvtPreviousDataControlActionCode = Action
  626.         Set Validate = Nothing
  627.         Exit Function
  628.     End If
  629.  
  630. ' bullet-proofing
  631.     If Not pvtIsFullyInitialized _
  632.         (Verbose:=True) _
  633.     Then
  634.         Set FindPrevious = Nothing
  635.         Exit Function
  636.     End If
  637.     If IsMissing(Action) Then
  638.         pvtErrorMessage TypeName(Me) & " cannot process the '.Validate' method because the 'Action:=' parameter is missing."
  639.         Set Validate = Nothing
  640.         Exit Function
  641.     End If
  642.     If IsMissing(Save) Then
  643.         pvtErrorMessage TypeName(Me) & " cannot process the '.Validate' method because the 'Save:=' parameter is missing."
  644.         Set Validate = Nothing
  645.         Exit Function
  646.     End If
  647.     
  648.     Set Validate = _
  649.         pvtCollection. _
  650.             pvtDataValidate( _
  651.                 DataControl:=pvtDataControl, _
  652.                 Action:=tempActionCode, _
  653.                 Save:=Save, _
  654.                 Sample:=Sample, _
  655.                 Parent:=Parent)
  656.     
  657.     pvtCollection.Refresh
  658.     pvtPreviousDataControlActionCode = Action
  659. End Function
  660.  
  661. Private Sub Class_Initialize()
  662.     
  663.     pvtSupportedTypeNames = _
  664.         "RecordSet DynaSet SnapShot"
  665.     pvtDataControlSupportedTypeNames = _
  666.         "Data"
  667.     pvtCollectionSupportedTypeNames = _
  668.         "VBOFCollection"
  669.         
  670.     Set pvtDataControl = Nothing
  671. End Sub
  672.  
  673.  
  674. Private Sub Class_Terminate()
  675.     If Not ObjectManager Is Nothing Then
  676.         ObjectManager.TerminateObject _
  677.             Object:=Me
  678.     End If
  679. End Sub
  680.  
  681.