Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. ' *********************************************************************************************
  2. ' Database Connection Helper
  3. '
  4. ' Description: Executes an SQL stored procedure within the specified database,
  5. ' using parameters passed by the user. It also execute SQL line statement
  6. ' with or without parameter. See HOW TO USE below.
  7. '
  8. ' Author: Jessie P. Semana
  9. ' Create Date: March 3, 2014
  10. ' Current Version: 1.0.3
  11. '
  12. ' ---------------------------------------------------------------------------------------------
  13. ' CHANGE LOG:
  14. ' ---------------------------------------------------------------------------------------------
  15. ' #1 March 3, 2014 - Initial Version
  16. ' #2 May 28, 2014 - Add additional method "ExecuteQuery(query as String)"
  17. ' #3 May 29, 2014 - Fix bug where cmd object doesn't initialize new instance when calling the methods.
  18. ' - Fix bug where m_sqlParams property doesn't reset.
  19. ' - Raise an error message instead of warning message when no connection string pass in the class.
  20. ' - Add class initialization by calling "Class_Initialize".
  21. ' - Add the ability to rollback transaction when error raise in the middle of your transaction.
  22. '
  23. ' ---------------------------------------------------------------------------------------------
  24. ' HOW TO USE:
  25. ' ---------------------------------------------------------------------------------------------
  26. '
  27. ' Dim conn As New DBConnection
  28. ' Dim sqlCmd As ADODB.Command
  29. ' Dim rs As New ADODB.Recordset
  30. '
  31. ' ---------------------------------------------------------------------------------------------
  32. ' Using Stored Procedure:
  33. ' ---------------------------------------------------------------------------------------------
  34. '
  35. ' conn.ConnectionString = "YourConnectionString"
  36. '
  37. ' Set sqlCmd = New ADODB.Command
  38. ' With sqlCmd
  39. ' .Parameters.Append .CreateParameter("@Mode", adInteger, adParamInput, 50, 0)
  40. ' .Parameters.Append .CreateParameter("@EmployeeID", adVarChar, adParamInput, 50, "YourValue")
  41. '
  42. ' conn.StoredProcedure = "spEmployee"
  43. ' conn.SQLParameters = sqlCmd.Parameters
  44. ' Set rs = conn.ExecuteCommand()
  45. ' End With
  46. '
  47. ' ---------------------------------------------------------------------------------------------
  48. ' Using SQL Line Statement WITHOUT parameter:
  49. ' ---------------------------------------------------------------------------------------------
  50. '
  51. ' Set rs = conn.ExecuteQuery("SELECT * FROM Employee;")
  52. '
  53. ' ---------------------------------------------------------------------------------------------
  54. ' Using SQL Line Statement WITH parameter:
  55. ' ---------------------------------------------------------------------------------------------
  56. '
  57. ' conn.ConnectionString = "YourConnectionString"
  58. '
  59. ' With sqlCmd
  60. ' .Parameters.Append .CreateParameter("@EmployeeID", adVarChar, adParamInput, 50, "YourValue")
  61. ' conn.SQLParameters = sqlCmd.Parameters
  62. ' Set rs = conn.ExecuteQuery("SELECT * FROM Employee WHERE EmployeeID = ?")
  63. ' End With
  64. ' *********************************************************************************************
  65.  
  66. Dim conn As ADODB.Connection
  67. Dim cmd As ADODB.Command
  68. Dim rs As New ADODB.Recordset
  69.  
  70. Private m_storedProc As String
  71. Private m_sqlParams As ADODB.Parameters
  72. Private m_connString As String
  73.  
  74. Public Property Get StoredProcedure() As String
  75. StoredProcedure = m_storedProc
  76. End Property
  77.  
  78. Public Property Let StoredProcedure(ByVal pStoredProcedure As String)
  79. m_storedProc = pStoredProcedure
  80. End Property
  81.  
  82. 'Public Property Get SqlParameters() As ADODB.Parameters
  83. ' SqlParameters = m_sqlParams
  84. 'End Property
  85. Public Property Let SqlParameters(ByVal pSQLParameters As ADODB.Parameters)
  86. Set m_sqlParams = pSQLParameters
  87. End Property
  88.  
  89. Public Property Get ConnectionString() As String
  90. ConnectionString = m_connString
  91. End Property
  92.  
  93. Public Property Let ConnectionString(ByVal pConnectionString As String)
  94. m_connString = pConnectionString
  95. End Property
  96.  
  97. Private Sub Class_Initialize()
  98.  
  99. ' This will trigger every time you initialize new DBConnection class.
  100.  
  101. m_connString = ""
  102. m_storedProc = ""
  103.  
  104. Set conn = Nothing
  105. Set cmd = Nothing
  106. Set rs = Nothing
  107. Set m_sqlParams = Nothing
  108.  
  109. End Sub
  110.  
  111. Public Function ExecuteNonCommand(Optional query As String) As Integer
  112. On Error GoTo TransError
  113.  
  114. Dim param As New ADODB.Parameter
  115. Dim rowAffected As Long
  116.  
  117. ' Clear any error first
  118. Err.Clear
  119.  
  120. If m_connString = "" Then
  121. Err.Raise 22001, "DBConnection class", "No valid connection string. Make sure you supply connection string property in DBConnection class."
  122. End If
  123.  
  124. Set conn = New ADODB.Connection
  125. With conn
  126. .ConnectionString = m_connString
  127. .ConnectionTimeout = 120
  128. .CommandTimeout = 120
  129. .Open
  130. End With
  131.  
  132. Set cmd = New ADODB.Command
  133. With cmd
  134. .ActiveConnection = conn
  135.  
  136. If m_storedProc <> "" Then
  137. .CommandType = adCmdStoredProc
  138. .CommandText = m_storedProc
  139. Else
  140. .CommandType = adCmdText
  141. .CommandText = query
  142. End If
  143.  
  144. If Not m_sqlParams Is Nothing Then
  145. For Each param In m_sqlParams
  146. .Parameters.Append param
  147. Next
  148. End If
  149.  
  150. ' NOTE:
  151. '
  152. ' rowAffected variable return a value when your command is Update, Insert and Delete ONLY.
  153. ' Select statement will return a -1 value. Use ExecuteCommand() or ExecuteQuery() instead.
  154.  
  155. cmd.Execute rowAffected
  156. ExecuteNonCommand = rowAffected
  157.  
  158. End With
  159.  
  160. conn.Close
  161.  
  162. Set cmd = Nothing
  163. Set conn = Nothing
  164. Set m_sqlParams = Nothing
  165.  
  166. Exit Function
  167.  
  168. TransError:
  169. conn.Close
  170.  
  171. Set cmd = Nothing
  172. Set conn = Nothing
  173. Set m_sqlParams = Nothing
  174.  
  175. MsgBox Err.Description
  176. End Function
  177.  
  178. Public Function ExecuteCommand() As Recordset
  179. On Error GoTo TransError
  180.  
  181. Dim param As New ADODB.Parameter
  182.  
  183. ' Clear any error first
  184. Err.Clear
  185.  
  186. If m_connString = "" Then
  187. Err.Raise 22001, "DBConnection class", "No valid connection string. Make sure you supply connection string property in DBConnection class."
  188. End If
  189.  
  190. Set conn = New ADODB.Connection
  191. With conn
  192. .CursorLocation = adUseClient
  193. .ConnectionString = m_connString
  194. .ConnectionTimeout = 120
  195. .CommandTimeout = 120
  196. .Open
  197. End With
  198.  
  199. Set cmd = New ADODB.Command
  200. With cmd
  201. .ActiveConnection = conn
  202. .CommandType = adCmdStoredProc
  203. .CommandText = m_storedProc
  204.  
  205. If Not m_sqlParams Is Nothing Then
  206. For Each param In m_sqlParams
  207. .Parameters.Append param
  208. Next
  209. End If
  210.  
  211. rs.CursorType = adOpenForwardOnly
  212. rs.CursorLocation = adUseClient
  213. rs.LockType = adLockOptimistic
  214.  
  215. Set rs = cmd.Execute
  216. Set ExecuteCommand = rs
  217. End With
  218.  
  219. Set rs = Nothing
  220. Set cmd = Nothing
  221. Set conn = Nothing
  222. Set m_sqlParams = Nothing
  223.  
  224. Exit Function
  225.  
  226. TransError:
  227. rs.Close
  228. conn.Close
  229.  
  230. Set cmd = Nothing
  231. Set conn = Nothing
  232. Set m_sqlParams = Nothing
  233.  
  234. MsgBox Err.Description
  235. End Function
  236.  
  237. Public Function ExecuteQuery(query As String) As Recordset
  238. On Error GoTo TransError
  239.  
  240. Dim param As New ADODB.Parameter
  241.  
  242. ' Clear any error first
  243. Err.Clear
  244.  
  245. If m_connString = "" Then
  246. Err.Raise 22001, "DBConnection class", "No valid connection string. Make sure you supply connection string property in DBConnection class."
  247. End If
  248.  
  249. Set conn = New ADODB.Connection
  250. With conn
  251. .CursorLocation = adUseClient
  252. .ConnectionString = m_connString
  253. .ConnectionTimeout = 120
  254. .CommandTimeout = 120
  255. .Open
  256. End With
  257.  
  258. Set cmd = New ADODB.Command
  259. With cmd
  260. .ActiveConnection = conn
  261. .CommandType = adCmdText
  262. .CommandText = query
  263.  
  264. If Not m_sqlParams Is Nothing Then
  265. For Each param In m_sqlParams
  266. .Parameters.Append param
  267. Next
  268. End If
  269.  
  270. rs.CursorType = adOpenStatic
  271. rs.CursorLocation = adUseClient
  272. rs.LockType = adLockOptimistic
  273.  
  274. 'rs.Open .CommandText, conn, adOpenForwardOnly, adLockOptimistic
  275. Set rs = cmd.Execute
  276. Set ExecuteQuery = rs
  277. End With
  278.  
  279. Set rs = Nothing
  280. Set cmd = Nothing
  281. Set conn = Nothing
  282. Set m_sqlParams = Nothing
  283.  
  284. Exit Function
  285.  
  286. TransError:
  287. rs.Close
  288. conn.Close
  289.  
  290. Set cmd = Nothing
  291. Set conn = Nothing
  292. Set m_sqlParams = Nothing
  293.  
  294. MsgBox Err.Description
  295. End Function
  296.  
  297. ' *********************************************************************************************
  298. ' Function ExecStoredProc
  299. '
  300. ' Description: Executes a SQL stored procedure within the specified database,
  301. ' using parameters passed by the user. Uses a parameter array
  302. ' to accept and store any possible number of supplied parameters
  303. ' Inputs: Stored procedure name, connection string, name of returned recordset,
  304. ' stored procedure parameters (any number allowed)
  305. ' Outputs: Returns an integer value representing the SQL stored procedure
  306. ' return code. 0 = success, -1 indicates VB error,
  307. ' Author: Kevin Chadwick
  308. '
  309. ' ---------------------------------------------------------------------------------------------
  310. ' Ammendments
  311. ' *********************************************************************************************
  312.  
  313. Private Function ExecStoredProc(ByVal vstrSpName As String, ByRef robjRecordSet As ADODB.Recordset, _
  314. ParamArray vntArray()) As Integer
  315.  
  316. ' On error
  317. On Error GoTo ExecStoredProcError
  318.  
  319. ' Declare the variables
  320. Dim objCommand As ADODB.Command
  321. Dim intCurrentParameter As Integer
  322.  
  323. ' Create the objects
  324. Set objCommand = New ADODB.Command
  325.  
  326. With conn
  327. .ConnectionString = m_connString
  328. .ConnectionTimeout = 120
  329. .CommandTimeout = 120
  330. .Open
  331. End With
  332.  
  333. ' Make it an active connection
  334. objCommand.ActiveConnection = conn
  335.  
  336. 'Declare the command object as a stored procedure
  337. objCommand.CommandType = adCmdStoredProc
  338. objCommand.CommandText = vstrSpName 'supplied by calling program
  339.  
  340. 'Get the supplied parameters from the ParamArray and assign them to the command object
  341. objCommand.Parameters.Append objCommand.CreateParameter("RetValue", _
  342. adInteger, adParamReturnValue) 'delcares the SQL return value
  343.  
  344. For intCurrentParameter = 1 To (UBound(vntArray) + 1)
  345. If vntArray(intCurrentParameter - 1) <> "" Then
  346. objCommand.Parameters.Append objCommand.CreateParameter("Parameter" _
  347. & intCurrentParameter, adVarChar, adParamInput, Len(vntArray(intCurrentParameter - 1)), vntArray(intCurrentParameter - 1))
  348. Else
  349. objCommand.Parameters.Append objCommand.CreateParameter("Parameter" _
  350. & intCurrentParameter, adVarChar, adParamInput, 1)
  351. End If
  352. Next intCurrentParameter
  353.  
  354. 'Get the recordset back
  355. Set robjRecordSet = New ADODB.Recordset
  356. robjRecordSet.CursorLocation = adUseClient
  357. Set robjRecordSet = objCommand.Execute
  358.  
  359. 'Clean up, send the SQL return code back to the calling program, and exit
  360. ExecStoredProc = objCommand.Parameters(0).Value 'send return code back
  361. Set objCommand = Nothing
  362.  
  363.  
  364. '--Error Handling--
  365. ExecStoredProcError:
  366. ' Add your error handling here
  367. MsgBox Err.Number & ": " & Err.Description
  368.  
  369. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement