Advertisement
nandordudas

DataObject

May 18th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "DataObject"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Compare Database
  11. Option Explicit
  12.  
  13. Private pModuleName As String
  14. Private pConnection As Object
  15. Private pQuery As String
  16. Private pParameters As Object
  17. Private pRecords As Object
  18. Private pAffectedRows As Long
  19. Private pRecordCount As Long
  20.  
  21. Private Sub Class_Initialize()
  22.  
  23.     pModuleName = VBE.ActiveCodePane.CodeModule.Name
  24.     Set pConnection = CurrentProject.Connection
  25.     Set pParameters = CreateObject("Scripting.Dictionary")
  26.  
  27. End Sub
  28.  
  29. Private Sub Class_Terminate()
  30.  
  31.     pModuleName = ""
  32.     Set pConnection = Nothing
  33.     Set pParameters = Nothing
  34.     Set pRecords = Nothing
  35.  
  36. End Sub
  37.  
  38. Public Property Get Query() As String
  39.  
  40.     Query = pQuery
  41.  
  42. End Property
  43.  
  44. Public Property Let Query(ByVal NewValue As String)
  45.  
  46.     pQuery = NewValue
  47.  
  48. End Property
  49.  
  50. Public Property Get RecordCount() As Long
  51.  
  52.     RecordCount = pRecordCount
  53.  
  54. End Property
  55.  
  56. Public Property Get AffectedRows() As Long
  57.  
  58.     AffectedRows = pAffectedRows
  59.  
  60. End Property
  61.  
  62. Public Function Execute() As Boolean
  63.  
  64.     On Error GoTo Catch
  65.  
  66.     If Not CBool(Len(pQuery)) Then _
  67.         Err.Raise vbObjectError + 515, Description:="Query is empty"
  68.  
  69.     Dim Command As Object, Parameter As Object, ParameterName As Variant, _
  70.         Result As Object, Executed As Boolean
  71.  
  72.     Set Command = CreateObject("ADODB.Command")
  73.     Set Result = CreateObject("Scripting.Dictionary")
  74.  
  75.     With Command
  76.         .ActiveConnection = pConnection
  77.         .ActiveConnection.CursorLocation = 3
  78.         .CommandType = 1
  79.         .CommandText = pQuery
  80.  
  81.         For Each ParameterName In pParameters.Keys
  82.  
  83.             Set Parameter = pParameters(ParameterName)
  84.  
  85.             .Parameters.Append Parameter
  86.  
  87.             DoEvents
  88.         Next ParameterName
  89.  
  90.         Set pRecords = .Execute(pAffectedRows)
  91.  
  92.         If pRecords Is Nothing Then
  93.             Err.Raise vbObjectError + 516, Description:="Recordset is empty"
  94.         Else
  95.             If CBool(pRecords.State) Then pRecordCount = pRecords.RecordCount
  96.         End If
  97.  
  98.         Executed = True
  99.  
  100.     End With
  101.  
  102.     GoTo Finally
  103.  
  104. Catch:
  105.     RollbackTransaction
  106.  
  107.     Handle Err.Number, Err.Description, "", "Function Execute", pModuleName
  108.  
  109. Finally:
  110.     Execute = Executed
  111.     pParameters.RemoveAll
  112.     Set Parameter = Nothing
  113.     Set Result = Nothing
  114.     Set Command = Nothing
  115.  
  116. End Function
  117.  
  118. Public Sub BeginTransaction()
  119.  
  120.     pConnection.BeginTrans
  121.  
  122. End Sub
  123.  
  124. Public Sub CommitTransaction()
  125.  
  126.     pConnection.CommitTrans
  127.  
  128. End Sub
  129.  
  130. Public Sub RollbackTransaction()
  131.  
  132.     pConnection.RollbackTrans
  133.  
  134. End Sub
  135.  
  136. Public Sub Bind(ByVal ParameterName As String, ByVal ParameterValue As Variant)
  137.  
  138.     On Error GoTo Catch
  139.  
  140.     If pParameters.Exists(ParameterName) Then _
  141.         Err.Raise vbObjectError + 513, _
  142.             Description:="Parameter exists [" & ParameterName & "]"
  143.  
  144.     Dim Parameter As Object
  145.  
  146.     Set Parameter = CreateObject("ADODB.Parameter")
  147.  
  148.     With Parameter
  149.         .Name = ParameterName
  150.  
  151.         Select Case VarType(ParameterValue)
  152.  
  153.             Case vbString
  154.                 .Type = 200
  155.                 .Value = CStr(ParameterValue)
  156.                 .Size = Len(.Value)
  157.  
  158.             Case vbInteger, vbLong
  159.                 .Type = vbLong
  160.                 .Value = CLng(ParameterValue)
  161.  
  162.             Case vbDouble
  163.                 .Type = vbDouble
  164.                 .Value = CDbl(ParameterValue)
  165.  
  166.             Case vbDate
  167.                 .Type = vbDate
  168.                 .Value = CDate(ParameterValue)
  169.  
  170.             Case vbBoolean
  171.                 .Type = vbBoolean
  172.                 .Value = CBool(ParameterValue)
  173.  
  174.             Case Else
  175.  
  176.         End Select
  177.  
  178.     End With
  179.  
  180.     pParameters.Add ParameterName, Parameter
  181.  
  182.     GoTo Finally
  183.  
  184. Catch:
  185.     Handle Err.Number, Err.Description, "", "Sub Bind", pModuleName
  186.  
  187. Finally:
  188.     Set Parameter = Nothing
  189.  
  190. End Sub
  191.  
  192. Public Function ResultSet() As Object
  193.  
  194.     On Error GoTo Catch
  195.  
  196.     Dim Row As Long, Rows As Variant, RowsCount As Long, _
  197.         FieldCounter As Long, Field As Object, RecordRow As Object, _
  198.         Result As Object, FieldsCount As Long
  199.  
  200.     BeginTransaction
  201.  
  202.     If Not CBool(Execute) Then _
  203.         Err.Raise vbObjectError + 514, Description:="Doesn't executed"
  204.  
  205.     CommitTransaction
  206.  
  207.     Set Result = CreateObject("Scripting.Dictionary")
  208.  
  209.     If (CBool(AffectedRows) Or Not CBool(pRecordCount)) Then GoTo Finally
  210.  
  211.     FieldsCount = pRecords.Fields.Count
  212.  
  213.     Rows = pRecords.GetRows(pRecordCount)
  214.  
  215.     RowsCount = UBound(Rows, 2)
  216.  
  217.     For Row = 0 To RowsCount
  218.  
  219.         Set RecordRow = CreateObject("Scripting.Dictionary")
  220.  
  221.         For FieldCounter = 0 To (FieldsCount - 1)
  222.  
  223.             Set Field = pRecords.Fields(FieldCounter)
  224.  
  225.             RecordRow.Add Field.Name, Rows(FieldCounter, Row)
  226.         Next FieldCounter
  227.  
  228.         Result.Add Result.Count, RecordRow
  229.     Next Row
  230.  
  231.     Set ResultSet = IIf((1 = pRecordCount), Result(0), Result)
  232.  
  233.     Erase Rows
  234.  
  235.     GoTo Finally
  236.  
  237. Catch:
  238.     Handle Err.Number, Err.Description, "", "Function ResultSet", pModuleName
  239.  
  240. Finally:
  241.     Set Field = Nothing
  242.     Set RecordRow = Nothing
  243.     Set Result = Nothing
  244.  
  245. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement