Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. Option Explicit
  2. Private strUser As String
  3. Private strProvider As String
  4. Private strPath As String
  5. Private strProperties As String
  6. Private strSQL As String
  7. Private strPassword As String
  8. Private con As ADODB.Connection
  9. Property Let ConnProvider(strCPR As String)
  10.  
  11. strProvider = strCPR
  12.  
  13. End Property
  14. Property Let ConnPath(strCPA As String)
  15.  
  16. strPath = strCPA
  17.  
  18. End Property
  19. Property Let ConnProperties(strCPP As String)
  20.  
  21. strProperties = strCPP
  22.  
  23. End Property
  24. Property Let SQLString(strSQLQuery As String)
  25.  
  26. strSQL = strSQLQuery
  27.  
  28. End Property
  29. Property Let SQLPassword(strSQLPassword As String)
  30.  
  31. strPassword = strSQLPassword
  32.  
  33. End Property
  34. Public Property Get RecordsetADODB(strSQL As String) As ADODB.Recordset
  35.  
  36. Dim rst As ADODB.Recordset
  37.  
  38. Set rst = New ADODB.Recordset
  39. rst.CursorLocation = adUseClient
  40. rst.Open strSQL, con, adOpenStatic, adLockReadOnly
  41.  
  42. Set RecordsetADODB = rst
  43.  
  44. End Property
  45. Public Property Get RecordsetArray(strSQL As String) As Variant
  46.  
  47. Dim rst As ADODB.Recordset
  48.  
  49. Set rst = New ADODB.Recordset
  50. rst.CursorLocation = adUseClient
  51. rst.Open strSQL, con, adOpenStatic, adLockReadOnly
  52.  
  53. RecordsetArray = rst.GetRows
  54.  
  55. End Property
  56. Public Property Get ListSheets() As Variant
  57.  
  58. Dim i As Integer
  59. Dim rst As ADODB.Recordset
  60. Dim intColumnCount As Integer
  61. Dim strColumnName As String
  62. Dim varItems As Variant
  63.  
  64. Set rst = con.OpenSchema(adSchemaTables)
  65. intColumnCount = rst.RecordCount
  66. ReDim varItems(0)
  67.  
  68. For i = 1 To intColumnCount
  69.  
  70. ReDim Preserve varItems(1 To UBound(varItems) + 1)
  71. varItems(UBound(varItems)) = rst.Fields("TABLE_NAME").Value
  72. rst.MoveNext
  73.  
  74. Next i
  75.  
  76. ListSheets = varItems
  77.  
  78. End Property
  79. Public Property Get ListFields(strTableName As String) As Variant
  80.  
  81. Dim i As Integer
  82. Dim rst As ADODB.Recordset
  83. Dim intColumnCount As Integer
  84. Dim varItems As Variant
  85.  
  86. Set rst = con.OpenSchema(adSchemaColumns, Array(Empty, Empty, strTableName, Empty))
  87. intColumnCount = rst.RecordCount
  88. ReDim varItems(0)
  89.  
  90. For i = 1 To intColumnCount
  91.  
  92. ReDim Preserve varItems(1 To UBound(varItems) + 1)
  93. varItems(UBound(varItems)) = rst.Fields("COLUMN_NAME").Value
  94. rst.MoveNext
  95.  
  96. Next i
  97.  
  98. ListFields = varItems
  99.  
  100. End Property
  101. Property Set ConnAddProperties(cn As ADODB.Connection)
  102.  
  103. On Error Resume Next
  104. With cn
  105.  
  106. .Provider = strProvider
  107. .ConnectionString = "Data Source=" & strPath & "; Extended Properties='" & strProperties & "'"
  108. .CursorLocation = adUseClient
  109.  
  110. End With
  111. On Error GoTo 0
  112.  
  113. If Err.Number = 0 Then
  114. Set con = cn
  115. Else
  116. MsgBox "Connection properties were not added.", vbCritical + vbOKOnly, "Error"
  117. End If
  118. Err.Clear
  119.  
  120. End Property
  121. Sub ConnOpen()
  122.  
  123. On Error Resume Next
  124. con.Open
  125. On Error GoTo 0
  126.  
  127. If Err.Number <> 0 Then
  128. MsgBox "Connection was not created.", vbCritical + vbOKOnly, "Error"
  129. End If
  130. Err.Clear
  131.  
  132. End Sub
  133. Sub ConnClose()
  134.  
  135. con.Close
  136.  
  137. End Sub
  138. Sub ConnSet()
  139.  
  140. Dim cn As ADODB.Connection
  141. Set cn = New ADODB.Connection
  142.  
  143. If strProvider = "" Or strPath = "" Or strProperties = "" Then
  144. MsgBox "Connection parameters were not provided."
  145. Exit Sub
  146. Else
  147. Set Me.ConnAddProperties = cn
  148. End If
  149.  
  150. End Sub
  151. Private Sub Class_Initialize()
  152.  
  153. strUser = Environ("username")
  154.  
  155. End Sub
  156. Private Sub Class_Terminate()
  157.  
  158. If Not con Is Nothing Then
  159.  
  160. Call ConnClose
  161. Set con = Nothing
  162.  
  163. End If
  164.  
  165. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement