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

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "DBAwareObjectLink"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9. ' DBAwareObjectLink is a component of the
  10. '   DBAwareCollection database-aware Collection
  11. '   class for Microsoft Visual Basic 4.0
  12.  
  13. Private pvtDatabase As Database
  14. Private pvtRecordSet As RecordSet
  15. Private pvtSQLStatement As String
  16. Private pvtDBAwareCollection As DBAwareCollection
  17. Private pvtCollectionEmulationMode As Boolean
  18.  
  19.  
  20.  
  21. Public Function CountOfParentObjectLinksToItem(Optional ByVal Parent As Variant, Optional ByVal Child As Variant) As Long
  22. ' Return the number of Parent links exist for
  23. '   the specified Child object
  24.  
  25.     Dim SQLStatement As String
  26.  
  27.     On Local Error Resume Next
  28.  
  29. ' bullet-proofing
  30.     If IsMissing(Parent) _
  31.     Or IsMissing(Child) _
  32.     Or pvtCollectionEmulationMode Then
  33.         CountOfParentObjectLinksToItem = -1
  34.         Exit Function
  35.     End If
  36.     
  37.     Err = 0
  38.  
  39. ' build the SQL statement to perform the Count
  40.     SQLStatement = _
  41.         "SELECT COUNT(*) FROM " & TableName() & " " & _
  42.         "WHERE ToObjectType = '" & _
  43.         Child.ObjectType & "' AND ToObjectID = " & _
  44.         Child.ObjectID
  45.         
  46. ' check for non-existent Object
  47.     If Err = 91 Then
  48.         CountOfParentObjectLinksToItem = 0
  49.         Exit Function
  50.     End If
  51.     
  52.     Set pvtRecordSet = pvtDatabase. _
  53.                         OpenRecordset( _
  54.                             SQLStatement, _
  55.                             dbOpenDynaset)
  56.  
  57.     If Err <> 0 Then
  58.         pvtErrorMessage pvtDBAwareCollection.Name & " received a database error while attempting to count the object containment links (Select Count(*))."
  59.         CountOfParentObjectLinksToItem = 0
  60.     Else
  61.         CountOfParentObjectLinksToItem = pvtRecordSet(0)
  62.     End If
  63.  
  64.     Set pvtRecordSet = Nothing
  65. End Function
  66. Private Function pvtErrorMessage(Optional ByVal ErrorMessage As Variant) As Long
  67.  
  68.     Dim RC As Long
  69.  
  70.     RC = MsgBox( _
  71.         ErrorMessage & vbCrLf & "Err=" & Err & ", Msg=" & Error(Err), _
  72.         vbOK + vbExclamation, _
  73.         pvtDBAwareCollection.Name & " Run-Time Error")
  74.     Err = 0
  75.     pvtErrorMessage = RC
  76. End Function
  77.  
  78. Public Function DeleteParentObjectLinksToItem(Optional ByVal Parent As Variant, Optional ByVal Child As Variant) As Long
  79. ' Remove the link between the Parent and Child
  80.     
  81.     Dim SQLStatement As String
  82.     
  83.     On Local Error Resume Next
  84.  
  85.     If pvtCollectionEmulationMode Then
  86.         DeleteParentObjectLinksToItem = True
  87.         Exit Function
  88.     End If
  89.     
  90.     Err = 0
  91.  
  92. ' delete the row from the DBAwareObjectLinks table
  93.     SQLStatement = _
  94.         "DELETE FROM " & TableName() & " WHERE FromObjectType = '" & _
  95.         Parent.ObjectType & "' AND FromObjectID = " & _
  96.         Parent.ObjectID & " AND ToObjectType = '" & _
  97.         Child.ObjectType & "' AND ToObjectID = " & _
  98.         Child.ObjectID
  99.     
  100. ' check for illegal Object
  101.     If Err = 91 Then
  102.         DeleteParentObjectLinksToItem = False
  103.         Exit Function
  104.     End If
  105.     
  106.     pvtDatabase.Execute SQLStatement
  107.     If Err <> 0 And Err <> 3078 Then '
  108.         pvtErrorMessage pvtDBAwareCollection.Name & " received a database error while attempting to remove an object containment link (Delete)."
  109.         DeleteParentObjectLinksToItem = False
  110.         Exit Function
  111.     End If
  112.         
  113.     DeleteParentObjectLinksToItem = True
  114. End Function
  115.  
  116.  
  117. Public Sub SetDatabaseParameters(Optional ByVal Database As Variant, Optional ByVal DBAwareCollection As Variant, Optional ByVal CollectionEmulationMode As Variant)
  118. ' Receive user-defined parameters
  119.  
  120.     If Not IsMissing(Database) Then
  121.         Set pvtDatabase = Database
  122.     End If
  123.  
  124.     If Not IsMissing(DBAwareCollection) Then
  125.         Set pvtDBAwareCollection = DBAwareCollection
  126.     End If
  127.  
  128.     If Not IsMissing(CollectionEmulationMode) Then
  129.         pvtCollectionEmulationMode = CollectionEmulationMode
  130.     End If
  131.  
  132. End Sub
  133.  
  134.  
  135. Public Function LinkParentObjectToChildObject(Optional ByVal Parent As Variant, Optional ByVal Child As Variant) As Long
  136.  
  137.     Dim SQLStatement As String
  138.     
  139.     On Local Error Resume Next
  140.     
  141.     If pvtCollectionEmulationMode Then
  142.         LinkParentObjectToChildObject = True
  143.     End If
  144.  
  145. ' insert a row into the DBAwareObjectLinks table
  146.     SQLStatement = _
  147.         "INSERT INTO " & TableName() & " " & _
  148.         "(FromObjectType" & _
  149.         ",FromObjectID" & _
  150.         ",ToObjectType" & _
  151.         ",ToObjectID"
  152.     SQLStatement = SQLStatement & _
  153.         ") VALUES " & _
  154.         "('" & Parent.ObjectType & "'" & _
  155.         ", " & Parent.ObjectID & "" & _
  156.         ",'" & Child.ObjectType & "'" & _
  157.         ", " & Child.ObjectID & ""
  158.     SQLStatement = SQLStatement & _
  159.         ")"
  160.     
  161.     pvtDatabase.Execute SQLStatement
  162.     If Err <> 0 Then '
  163.         pvtErrorMessage pvtDBAwareCollection.Name & " received a database error while attempting to establish an object containment link (Insert)."
  164.         LinkParentObjectToChildObject = False
  165.         Exit Function
  166.     End If
  167.         
  168.     LinkParentObjectToChildObject = True
  169. End Function
  170. Public Function ObjectType() As String
  171.     ObjectType = "DBAwareObjectLink"
  172. End Function
  173.  
  174.  
  175.  
  176. Public Property Get ObjectID()
  177.     ObjectID = -1
  178. End Property
  179.  
  180.  
  181. Public Function TableName() As String
  182.     TableName = "DBAwareObjectLinks"
  183. End Function
  184.  
  185.  
  186. Private Sub Class_Initialize()
  187.  
  188.     pvtCollectionEmulationMode = False
  189.  
  190. End Sub
  191.  
  192.  
  193.