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

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "VBOFDBGridWrapper"
  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. ' VBOFDBGridWrapper is a supplemental GUI Control
  15. '   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. ' VBOFDBGridWrapper is a wrapper class for
  23. '   providing automatic interfacing between a
  24. '   DBGrid VB control and an underlying
  25. '   VBOFCollection
  26.  
  27. Private pvtVBOFObjectManager As VBOFObjectManager
  28. Private pvtCollection As VBOFCollection
  29. Private pvtSample As Variant
  30. Private pvtParent As Variant
  31. Private pvtDBGrid As Variant
  32. Private pvtSupportedTypeNames As String
  33.  
  34. Public ObjectID As Long
  35.  
  36. Public Property Get Collection() As Variant
  37. Attribute Collection.VB_Description = "Sets the underlying VBOFCollection"
  38. ' Returns my VBOFCollection object
  39.  
  40.     Set Collection = pvtCollection
  41. End Property
  42.  
  43. Private Function pvtIsFullyInitialized(Optional Collection As Variant, Optional DBGrid As Variant, Optional Verbose As Variant) As Boolean
  44.     
  45.     If Not pvtVerifyCollection( _
  46.         Collection:=Collection, _
  47.         Verbose:=Verbose) _
  48.     Then
  49.         pvtIsFullyInitialized = False
  50.         Exit Function
  51.     End If
  52.     
  53.     If Not pvtVerifyDBGrid( _
  54.         DBGrid:=DBGrid, _
  55.         Verbose:=Verbose) _
  56.     Then
  57.         pvtIsFullyInitialized = False
  58.         Exit Function
  59.     End If
  60.  
  61.     pvtIsFullyInitialized = True
  62. End Function
  63.  
  64. Private Function pvtSetParent(Optional Parent As Variant, Optional MethodName As Variant) As Boolean
  65.     
  66.     On Local Error Resume Next
  67.     
  68.     pvtSetParent = True
  69.     
  70.     If IsMissing(Parent) Then
  71.         If pvtParent Is Nothing Then
  72.             pvtErrorMessage TypeName(Me) & " cannot process the '." & MethodName & "' method for this object because the 'Parent:=' parameter is missing and no preceeding method has established a default object."
  73.             pvtSetParent = False
  74.         End If
  75.     Else
  76.         Set pvtParent = Parent
  77.     End If
  78.  
  79. End Function
  80.  
  81. Private Function pvtSetSample(Optional Sample As Variant, Optional MethodName As Variant) As Boolean
  82.     
  83.     On Local Error Resume Next
  84.     
  85.     pvtSetSample = True
  86.     
  87.     If IsMissing(Sample) Then
  88.         If pvtSample Is Nothing Then
  89.             pvtErrorMessage TypeName(Me) & " cannot process the '." & MethodName & "' method for this object because the 'Sample' parameter is missing and no preceeding method has established a default."
  90.             pvtSetSample = False
  91.         End If
  92.     End If
  93.  
  94. End Function
  95.  
  96.  
  97. Public Property Let Bookmark(Bookmark As Variant)
  98. Attribute Bookmark.VB_Description = "Sets the Bookmark property of the DBGrid"
  99. ' Sets the Bookmark value of the DBGrid
  100. ' Using this method:
  101. '       MyDBGridWrapper.Bookmark = _
  102. '           MyBookMark
  103.     
  104.     On Local Error Resume Next
  105.  
  106. ' bullet-proofing
  107.     If Not pvtIsFullyInitialized() _
  108.     Then
  109.         Exit Property
  110.     End If
  111.  
  112.     pvtCollection. _
  113.         pvtDBGridBookmark _
  114.             (DBGrid:=pvtDBGrid) = _
  115.                 Bookmark
  116.  
  117. End Property
  118.  
  119.  
  120. Public Property Get Bookmark() As Variant
  121. ' Returns the Bookmark value of the DBGrid
  122. ' Using this method:
  123. '     Set MyBookmark = _
  124. '         MyDBGridWrapper.Bookmark
  125.     
  126.     On Local Error Resume Next
  127.  
  128. ' bullet-proofing
  129.     If Not pvtIsFullyInitialized() _
  130.     Then
  131.         Exit Property
  132.     End If
  133.  
  134.     Bookmark = _
  135.         pvtCollection. _
  136.             pvtDBGridBookmark _
  137.                 (DBGrid:=pvtDBGrid)
  138.  
  139. End Property
  140.  
  141.  
  142. Public Property Set BookmarkObject(Object As Variant)
  143. Attribute BookmarkObject.VB_Description = "Set the Bookmark property of the DBGrid to equate to the specified object"
  144. ' Sets the Bookmark of the DBGrid to the position
  145. '   of Object
  146. ' Using this method:
  147. '     Set MyDBGridWrapper.BookmarkObject = _
  148. '         MyObject
  149.     
  150.     On Local Error Resume Next
  151.  
  152. ' bullet-proofing
  153.     If Not pvtIsFullyInitialized() _
  154.     Then
  155.         Exit Property
  156.     End If
  157.  
  158.     Set pvtCollection. _
  159.         pvtDBGridBookmarkObject _
  160.             (DBGrid:=pvtDBGrid) = _
  161.                 Object
  162. End Property
  163.  
  164.  
  165. Public Property Get BookmarkObject() As Variant
  166. ' Returns the Object at the Bookmark value of the
  167. '   DBGrid
  168. ' Using this method:
  169. '       MyObject = _
  170. '           MyDBGridWrapper. BookmarkObject
  171.     
  172.     On Local Error Resume Next
  173.  
  174. ' bullet-proofing
  175.     If Not pvtIsFullyInitialized() _
  176.     Then
  177.         Exit Property
  178.     End If
  179.  
  180.     Set BookmarkObject = _
  181.         pvtCollection. _
  182.             pvtDBGridBookmarkObject _
  183.                 (DBGrid:=pvtDBGrid)
  184.  
  185. End Property
  186.  
  187.  
  188. Public Property Set DBGrid(DBGrid As Variant)
  189. Attribute DBGrid.VB_Description = "Sets the underlying DBGrid"
  190.     pvtVerifyDBGrid _
  191.         DBGrid:=DBGrid
  192. End Property
  193.  
  194. Public Property Set Collection(Collection As Variant)
  195.     
  196.     If Collection Is Nothing Then
  197.         Set pvtCollection = Nothing
  198.         Exit Property
  199.     End If
  200.     
  201.     pvtVerifyCollection _
  202.         Collection:=Collection, _
  203.         Verbose:=True
  204. End Property
  205.  
  206. Public Property Get ObjectManager() As VBOFObjectManager
  207. ' Return my reference to the VBOFObjectManager
  208.     
  209.     Set ObjectManager = pvtVBOFObjectManager
  210. End Property
  211.  
  212. Public Property Set ObjectManager(anObjectManager As VBOFObjectManager)
  213. ' Set my reference to the VBOFObjectManager
  214.     
  215.     Set pvtVBOFObjectManager = anObjectManager
  216. End Property
  217.  
  218. Private Function pvtErrorMessage(Optional ErrorMessage As Variant) As Long
  219.     pvtErrorMessage = _
  220.         pvtVBOFObjectManager.DisplayErrorMessage _
  221.             (ErrorMessage)
  222. End Function
  223.  
  224. Private Function pvtUseCollection(Optional CollectionParm As Variant, Optional Verbose As Variant) As Variant
  225.     Set pvtUseCollection = _
  226.         ObjectManager. _
  227.             pvtWrapperUseCollection( _
  228.                 CollectionParm:=CollectionParm, _
  229.                 pvtCollection:=pvtCollection, _
  230.                 Verbose:=Verbose, _
  231.                 WrapperName:="DBGrid")
  232. End Function
  233.  
  234. Private Function pvtUseDBGrid(Optional DBGridParm As Variant, Optional Verbose As Variant) As Variant
  235.     Set pvtUseDBGrid = _
  236.         ObjectManager. _
  237.             pvtWrapperUseControl( _
  238.                 ControlParm:=DBGridParm, _
  239.                 pvtControl:=pvtDBGrid, _
  240.                 SupportedNames:=pvtSupportedTypeNames, _
  241.                 Verbose:=Verbose, _
  242.                 WrapperName:="DBGrid")
  243. End Function
  244.  
  245.  
  246. Private Function pvtVerifyCollection(Optional Collection As Variant, Optional Verbose As Variant) As Boolean
  247.     pvtVerifyCollection = _
  248.         ObjectManager. _
  249.             pvtWrapperVerifyCollection( _
  250.                 Collection:=Collection, _
  251.                 pvtCollection:=pvtCollection, _
  252.                 Verbose:=Verbose, _
  253.                 WrapperName:="DBGrid")
  254. End Function
  255.  
  256. Public Function Rebind(Optional Collection As Variant, Optional DBGrid As Variant) As Boolean
  257. Attribute Rebind.VB_Description = "Rebinds the VBOFDBGridWrapper"
  258. ' Rebinds the Wrapper to a Collection or DBGrid
  259. '   after having changed the assignment of either.
  260. '   For example, in the following scenario, the
  261. '   VBOFSDBGridWrapper must be rebound because the
  262. '   VBOFCollection has been significantly altered:
  263. '
  264. '   Dim pvtAddresses as VBOFCollection
  265. '   Dim pvtPerson as Person
  266. '   Dim MyDBGridWrapper as VBOFDBGridWrapper
  267. '   Set MyDBGridWrapper = _
  268. '       ObjectManager.NewVBOFDBGridWrapper ( _
  269. '           Collection:=pvtAddresses, _
  270. '           DBGrid:=MyDBGrid)
  271. '
  272. ' the following line alters the state of the data
  273. ' in-effect at the time of the above binding
  274. '   Set pvtAddresses = pvtPerson.Addresses
  275. ' rebind the Wrapper
  276. '   MyDBGridWrapper.Rebind _
  277. '           Collection:=pvtAddresses
  278.     
  279. ' bullet-proofing
  280.     If Not IsMissing(Collection) Then
  281.         If TypeName(Collection) <> "VBOFCollection" Then
  282.             pvtErrorMessage TypeName(Me) & " cannot process the '.Rebind' method because the 'Collection:=' parameter is not a VBOFCollection."
  283.             Rebind = False
  284.             Exit Function
  285.         End If
  286.     End If
  287.     If Not IsMissing(DBGrid) Then
  288.         If TypeName(DBGrid) <> "DBGrid" Then
  289.             pvtErrorMessage TypeName(Me) & " cannot process the '.Rebind' method because the 'DBGrid:=' parameter is not a Visual Basic DBGrid control.  Please use a VBOF Wrapper for the " & TypeName(DBGrid) & " control (or request the development of one.)"
  290.             Rebind = False
  291.             Exit Function
  292.         End If
  293.     End If
  294.     If Not pvtIsFullyInitialized( _
  295.         Collection:=Collection, _
  296.         DBGrid:=DBGrid, _
  297.         Verbose:=False) _
  298.     Then
  299.         Exit Function
  300.     End If
  301.     
  302.     pvtDBGrid.Refresh
  303.  
  304.     Rebind = True
  305. End Function
  306.  
  307. Public Function Refresh(Optional DisplayOnly As Variant) As Boolean
  308. Attribute Refresh.VB_Description = "Refreshes the VBOFDBGridWrapper, the VBOFCollection and the DBGrid"
  309. ' Refreshes the display of the DBGrid
  310. ' Using this method:
  311. '   MyDBGridWrapper.Refresh
  312.     
  313.     On Local Error Resume Next
  314.  
  315. ' bullet-proofing
  316.     If Not pvtIsFullyInitialized() _
  317.     Then
  318.         Exit Function
  319.     End If
  320.     
  321.     If Not IsMissing(DisplayOnly) Then
  322.         If DisplayOnly Then
  323.         Else
  324.             Set Collection = _
  325.                 pvtCollection.Refresh
  326.         End If
  327.     Else
  328.     End If
  329.  
  330.     pvtDBGrid.Refresh
  331.  
  332.     Refresh = True
  333. End Function
  334.  
  335.  
  336. Private Function pvtVerifyDBGrid(Optional DBGrid As Variant, Optional Verbose As Variant) As Boolean
  337.     pvtVerifyDBGrid = _
  338.         ObjectManager. _
  339.             pvtWrapperVerifyControl( _
  340.                 Control:=DBGrid, _
  341.                 pvtControl:=pvtDBGrid, _
  342.                 Verbose:=Verbose)
  343. End Function
  344.  
  345. Public Property Get DBGrid() As Variant
  346.     Set DBGrid = pvtDBGrid
  347. End Property
  348.  
  349.  
  350. Public Function SetNumberOfRows() As Boolean
  351. ' Informs the DBGrid of the number of rows that
  352. '   are to be added
  353. ' Note:  the referenced objects must contain the
  354. '   method 'ObjectDBGridValue', which must populate
  355. '   and return the RowBuffer object
  356. '   (for more information, find "RowBuffer" in the
  357. '   online VB Help.)
  358. '
  359. ' Note:  this method is optional and can be coded as
  360. '   follows:
  361. '   Private Sub Form_Load()
  362. '       MyDBGridWrapper.SetNumberOfRows
  363. '   End Sub
  364.  
  365.     On Local Error Resume Next
  366.     
  367.     If Not pvtIsFullyInitialized() _
  368.     Then
  369.         Exit Function
  370.     End If
  371.  
  372.     SetNumberOfRows = _
  373.         pvtCollection. _
  374.             pvtDBGridSetNumberOfRows _
  375.                 (DBGrid:=pvtDBGrid)
  376. End Function
  377.  
  378.  
  379. Public Function Unbind() As Boolean
  380.  
  381.     Set pvtCollection = Nothing
  382.     Set pvtDBGrid = Nothing
  383.     Set pvtVBOFObjectManager = Nothing
  384.  
  385. End Function
  386.  
  387. Public Function UnboundAddData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional NewRowBookmark As Variant, Optional Sample As Variant, Optional Parent As Variant) As Variant
  388. Attribute UnboundAddData.VB_Description = "Processes the DBGrid UnboundAddData event procedure"
  389. ' Processes the UnboundAddData event of the DBGrid.
  390. '   Automatically instantiates a new object,
  391. '   populates it, adds it to the VBOFCollection
  392. '   and returns the VBOFCollection to the
  393. '   application.
  394. '
  395. ' Parameters:
  396. '   RowBuf:= is the same RowBuf parameter found
  397. '       in the application's UnboundAddData event
  398. '       handler
  399. '   NewRowBookmark:= is the same NewRowBookmark
  400. '       parameter found in the application's
  401. '       UnboundAddData event handler
  402. '   Sample:= (Optional) identifies the class
  403. '       type to instantiate with the new data.
  404. '       If a previous VBOFDBGridWrapper method had
  405. '       already established a Sample:=, this
  406. '       parameter can be eliminated
  407. '   Parent:= (Optional) identifies the object
  408. '       which is the parent ("container") object of
  409. '       the objects in this collection.
  410. '       If a previous VBOFDBGridWrapper method had
  411. '       already established a Parent:=, this
  412. '       parameter can be eliminated
  413. '
  414. ' Note:  this method should be coded as follows:
  415. '   Private Sub DBGrid1_UnboundAddData(ByVal RowBuf As RowBuffer, NewRowBookmark As Variant)
  416. '       Dim tempSample as New MyClass
  417. '       MyDBGridWrapper.UnboundAddData _
  418. '           RowBuf:=RowBuf, _
  419. '           NewRowBookmark:=NewRowBookmark, _
  420. '           Sample:=tempSample
  421. '   End Sub
  422.     
  423.     Dim tempParent As VBOFCollection
  424.     
  425.     On Local Error Resume Next
  426.     
  427. ' bullet-proofing
  428.     If Not pvtIsFullyInitialized( _
  429.         DBGrid:=DBGrid) _
  430.     Then
  431.         Exit Function
  432.     End If
  433.     If IsMissing(RowBuf) Then
  434.         pvtErrorMessage TypeName(Me) & " cannot process the '.UnboundAddData' method for this object because the 'RowBuf:=' parameter is missing."
  435.         Exit Function
  436.     End If
  437.     If IsMissing(NewRowBookmark) Then
  438.         pvtErrorMessage TypeName(Me) & " cannot process the '.UnboundAddData' method for this object because the 'NewRowBookmark:=' parameter is missing."
  439.         Exit Function
  440.     End If
  441.     If Not pvtSetSample( _
  442.             Sample:=Sample, _
  443.             MethodName:="UnboundAddData") Then
  444.         Set UnboundAddData = Nothing
  445.         GoTo UnboundAddData_Exit
  446.     End If
  447.  
  448. ' pick a value for Parent
  449.     Set tempParent = pvtCollection.Parent
  450.     If Not IsMissing(Parent) Then
  451.         Set tempParent = Parent
  452.     End If
  453.  
  454.     Set UnboundAddData = _
  455.         pvtCollection. _
  456.             pvtDBGridUnboundAddData( _
  457.                 DBGrid:=pvtDBGrid, _
  458.                 RowBuf:=RowBuf, _
  459.                 NewRowBookmark:=NewRowBookmark, _
  460.                 Sample:=Sample, _
  461.                 Parent:=tempParent)
  462.  
  463.     Refresh
  464.     
  465. UnboundAddData_Exit:
  466.     Exit Function
  467. End Function
  468.  
  469. Public Function UnboundWriteData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional WriteLocation As Variant) As Variant
  470. Attribute UnboundWriteData.VB_Description = "Processes the DBGrid UnboundWriteData event procedure"
  471. ' Processes the UnboundWriteData event of the DBGrid.
  472. '
  473. ' Parameters:
  474. '   RowBuf:= is the same RowBuf parameter found
  475. '       in the application's UnboundWriteData event
  476. '       handler
  477. '   WriteLocation:= is the same WriteLocation
  478. '       parameter found in the application's
  479. '       UnboundWriteData event handler
  480. '
  481. ' Note:  this method should be coded as follows:
  482. '   Private Sub DBGrid1_UnboundWriteData(ByVal RowBuf As RowBuffer, WriteLocation As Variant)
  483. '       MyDBGridWrapper.UnboundWriteData _
  484. '           RowBuf:=RowBuf, _
  485. '           WriteLocation:=WriteLocation
  486. '   End Sub
  487.     
  488.     On Local Error Resume Next
  489.     
  490.     If Not pvtIsFullyInitialized( _
  491.         DBGrid:=DBGrid) _
  492.     Then
  493.         Exit Function
  494.     End If
  495.  
  496.     Set UnboundWriteData = _
  497.         pvtCollection. _
  498.             pvtDBGridUnboundWriteData( _
  499.                 DBGrid:=pvtDBGrid, _
  500.                 RowBuf:=RowBuf, _
  501.                 WriteLocation:=WriteLocation)
  502.  
  503.     Refresh
  504.     
  505. UnboundWriteData_Exit:
  506.     Exit Function
  507. End Function
  508.  
  509.  
  510.  
  511. Public Function UnboundReadData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional StartLocation As Variant, Optional ReadPriorRows As Variant) As Long
  512. Attribute UnboundReadData.VB_Description = "Processes the DBGrid UnboundReadData event procedure"
  513. ' Populates the DBGrid with one row of information
  514. '   for each object in the associated
  515. '   VBOFCollection.
  516. ' Returns the number of rows added to the DBGrid
  517. ' Note:  the referenced objects must contain the
  518. '   method 'ObjectDBGridValue', which must populate
  519. '   and return the RowBuffer object
  520. '   (for more information, find "RowBuffer" in the
  521. '   online VB Help.)
  522. '
  523. ' Note:  this method should be coded in the
  524. '   DBGrid's UnboundReadData Event Procedure,
  525. '   as follows:
  526. '
  527. '   Private Sub DBGrid1_UnboundReadData(ByVal RowBuf As RowBuffer, StartLocation As Variant, ByVal ReadPriorRows As Boolean)
  528. '       MyDBGridWrapper.UnboundReadData _
  529. '           RowBuf:=RowBuf, _
  530. '           StartLocation:=StartLocation, _
  531. '           ReadPriorRows:=ReadPriorRows
  532. '   End Sub
  533.     
  534.     On Local Error Resume Next
  535.     
  536. ' bullet-proofing
  537.     If IsMissing(RowBuf) Then
  538.         pvtErrorMessage TypeName(Me) & " cannot process the '.UnboundReadData' method for this object because the 'RowBuf:=' parameter is missing."
  539.         Exit Function
  540.     End If
  541.     If IsMissing(StartLocation) Then
  542.         pvtErrorMessage TypeName(Me) & " cannot process the '.UnboundReadData' method for this object because the 'StartLocation:=' parameter is missing."
  543.         Exit Function
  544.     End If
  545.     If IsMissing(ReadPriorRows) Then
  546.         pvtErrorMessage TypeName(Me) & " cannot process the '.UnboundReadData' method for this object because the 'ReadPriorRows:=' parameter is missing."
  547.         Exit Function
  548.     End If
  549.     If Not pvtIsFullyInitialized( _
  550.         DBGrid:=DBGrid) _
  551.     Then
  552.         Exit Function
  553.     End If
  554.  
  555.     UnboundReadData = _
  556.         pvtCollection. _
  557.             pvtDBGridUnboundReadData( _
  558.                 DBGrid:=pvtDBGrid, _
  559.                 RowBuf:=RowBuf, _
  560.                 StartLocation:=StartLocation, _
  561.                 ReadPriorRows:=ReadPriorRows)
  562.  
  563. End Function
  564.  
  565. Public Function UnboundDeleteRow(Optional DBGrid As Variant, Optional Bookmark As Variant) As Long
  566. Attribute UnboundDeleteRow.VB_Description = "Processes the DBGrid UnboundDeleteRow event procedure"
  567. ' Removes the Bookmarked Object from the DBGrid
  568. '   and from associated VBOFCollection.
  569. ' Returns the number of rows currently ino the
  570. '   DBGrid
  571. '
  572. ' Note:  this method should be coded in the
  573. '   DBGrid's UnboundReadData Event Procedure,
  574. '   as follows:
  575. '
  576. '   Private Sub DBGrid1_UnboundDeleteRow(ByVal Bookmark As RowBuffer)
  577. '       MyDBGridWrapper.UnboundDeleteRow _
  578. '           Bookmark:=Bookmark
  579. '   End Sub
  580.     
  581.     On Local Error Resume Next
  582.     
  583. ' bullet-proofing
  584.     If IsMissing(Bookmark) Then
  585.         pvtErrorMessage TypeName(Me) & " cannot process the '.UnboundDeleteRow' method for this object because the 'Bookmark:=' parameter is missing."
  586.         Exit Function
  587.     End If
  588.     If Not pvtIsFullyInitialized( _
  589.         DBGrid:=DBGrid) _
  590.     Then
  591.         Exit Function
  592.     End If
  593.  
  594.     UnboundDeleteRow = _
  595.         pvtCollection. _
  596.             pvtDBGridUnboundDeleteRow( _
  597.                 DBGrid:=pvtDBGrid, _
  598.                 Bookmark:=Bookmark)
  599.  
  600.     Refresh
  601.     
  602.     UnboundDeleteRow = _
  603.         pvtCollection.Count
  604. End Function
  605.  
  606.  
  607. Private Sub Class_Initialize()
  608.     pvtSupportedTypeNames = "DBGrid"
  609. End Sub
  610.  
  611.  
  612. Private Sub Class_Terminate()
  613.     If Not ObjectManager Is Nothing Then
  614.         ObjectManager.TerminateObject _
  615.             Object:=Me
  616.     End If
  617. End Sub
  618.  
  619.