Guest User

Untitled

a guest
Nov 29th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. ' ADO Abstraction Class for VBA
  2. ' Christopher Harrison
  3.  
  4. ' This is meant for simple, read-only access to an ODBC database (e.g., for
  5. ' report writing in Excel, etc.). It constructs parameterised queries, with
  6. ' optional varchar parameters (ordered, not named) passed as a collection.
  7. ' (SELECT statements, at least, are weakly typed (or can be casted), so using
  8. ' strings isn't really a concern.)
  9.  
  10. ' Notes:
  11. ' 1. The DSN password is encrypted in memory, for some semblance of security.
  12. ' However, so is the encryption key, so don't rely on this!
  13. ' 2. Contains code that changes the mouse pointer to a waiting cursor (when
  14. ' connecting and fetching data) that isn't necessarily portable between
  15. ' applications. The code here is specific to Microsoft Excel.
  16.  
  17. ' Requires:
  18. ' * Microsoft ActiveX Data Objects Library
  19.  
  20. ' Example:
  21.  
  22. ' Dim myDB as dbConnection
  23. ' Dim myQuery as String
  24. ' Dim myParameters as Collection
  25. ' Dim myData as ADODB.Recordset
  26. '
  27. ' Set myDB = New dbConnection
  28. ' If myDB.Connect("someDSN", "username", "password") Then
  29. ' Debug.Print "Connected to " & myDB.Connected
  30. '
  31. ' myQuery = "select id, name from table where id > ? and id < ?"
  32. '
  33. ' Set myParameters = New Collection
  34. ' myParameters.Add "1"
  35. ' myParameters.Add "5"
  36. '
  37. ' Set myData = myDB.Query(myQuery, myParameters)
  38. ' If Not myData Is Nothing Then
  39. ' myData.MoveFirst
  40. ' While Not myData.EOF
  41. ' Debug.Print myData!id & ": " & myData!name
  42. ' myData.MoveNext
  43. ' Wend
  44. ' Set myData = Nothing
  45. ' Else
  46. ' Debug.Print "No data found"
  47. ' End If
  48. '
  49. ' Set myParameters = Nothing
  50. ' Else
  51. ' Debug.Print "Cannot connect"
  52. ' End If
  53. '
  54. ' Set myDB = Nothing
  55.  
  56. Private pDSN As String
  57. Private pUsername As String
  58. Private pXPassword As String
  59. Private pKey As String
  60.  
  61. Private DB As ADODB.Connection
  62.  
  63. Public Property Get Connected() As Variant
  64. If DB.State = adStateOpen Then Connected = pUsername & "@" & pDSN Else Connected = False
  65. End Property
  66.  
  67. Public Function Connect(ByVal DSN As String, ByVal Username As String, ByVal Password As String) As Boolean
  68. pDSN = DSN
  69. pUsername = Username
  70. pXPassword = XorC(Password, pKey)
  71.  
  72. Application.Cursor = xlWait ' <- Non-portable between VBA applications
  73. Connect = dbOpen
  74. Application.Cursor = xlDefault ' <- Non-portable between VBA applications
  75. End Function
  76.  
  77. Public Function Query(ByVal QuerySQL As String, Optional Parameters As Variant) As ADODB.Recordset
  78. Dim dbQuery As ADODB.Command
  79. Dim Parameter As ADODB.Parameter
  80. Dim Output As ADODB.Recordset
  81.  
  82. Dim param As Variant
  83.  
  84. If DB.State <> adStateOpen Then
  85. Set Query = Nothing
  86. Else
  87. Application.Cursor = xlWait ' <- Non-portable between VBA applications
  88.  
  89. Set dbQuery = New ADODB.Command
  90. dbQuery.ActiveConnection = DB
  91. dbQuery.CommandText = QuerySQL
  92.  
  93. If Not IsMissing(Parameters) Then
  94. For Each param In Parameters
  95. Set Parameter = dbQuery.CreateParameter(, adVarChar, adParamInput, Len(param), param)
  96. dbQuery.Parameters.Append Parameter
  97. Next
  98. Set Parameter = Nothing
  99. End If
  100.  
  101. Set Output = New ADODB.Recordset
  102. Output.CursorType = adOpenStatic
  103. Output.CursorLocation = adUseClient
  104. Output.Open dbQuery
  105.  
  106. If Output.EOF Then
  107. Set Query = Nothing
  108. Else
  109. Set Query = Output
  110. End If
  111.  
  112. Set Output = Nothing
  113. Set Parameter = Nothing
  114. Set dbQuery = Nothing
  115.  
  116. Application.Cursor = xlDefault ' <- Non-portable between VBA applications
  117. End If
  118. End Function
  119.  
  120. Private Sub Class_Initialize()
  121. pKey = XorC(Now, Environ("username"))
  122. Set DB = Nothing
  123. End Sub
  124.  
  125. Private Sub Class_Terminate()
  126. If Not DB Is Nothing Then
  127. If DB.State = adStateOpen Then DB.Close
  128. End If
  129. Set DB = Nothing
  130. End Sub
  131.  
  132. Private Function XorC(ByVal Text As String, ByVal Password As String) As String
  133. Dim i As Integer
  134. Dim iPass As Integer
  135.  
  136. XorC = ""
  137. For i = 1 To Len(Text)
  138. iPass = i Mod Len(Password)
  139. If iPass = 0 Then iPass = Len(Password)
  140. XorC = XorC + Chr(Asc(Mid(Text, i, 1)) Xor Asc(Mid(Password, iPass, 1)))
  141. Next
  142. End Function
  143.  
  144. Private Function dbOpen() As Boolean
  145. On Error Resume Next
  146.  
  147. Set DB = New ADODB.Connection
  148. DB.Open pDSN, pUsername, XorC(pXPassword, pKey)
  149.  
  150. dbOpen = (DB.State = adStateOpen)
  151. End Function
Add Comment
Please, Sign In to add comment