Guest User

Untitled

a guest
Jan 16th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. Sub CallStoredProcedure()
  2.  
  3. Dim Conn As ADODB.Connection
  4. Dim RecordSet As ADODB.RecordSet
  5. Dim Command As ADODB.Command
  6. Dim ConnectionString As String, StoredProcName As String
  7. Dim MonthVar As ADODB.Parameter
  8. Dim YearVar As ADODB.Parameter
  9. Dim TPm1Var As ADODB.Parameter
  10. Dim TPm2Var As ADODB.Parameter
  11. Dim TPm3Var As ADODB.Parameter
  12.  
  13.  
  14. Dim MonthInput As Integer
  15. Dim YearInput As Integer
  16. Dim TPm1Input As String
  17. Dim TPm2Input As String
  18. Dim TPm3Input As String
  19.  
  20.  
  21. Dim fldCount As Long
  22. Dim iCol As Long
  23. Dim sht As Worksheet
  24. Dim LastRow As Long
  25.  
  26. Dim username As String
  27. Dim password As String
  28. Dim IPaddress As String
  29.  
  30. ' CMF QUERY
  31.  
  32. Application.ScreenUpdating = False
  33.  
  34. Set Conn = New ADODB.Connection
  35. Set RecordSet = New ADODB.RecordSet
  36. Set Command = New ADODB.Command
  37.  
  38. username = "Uid=" & Worksheets("Input").Range("H5").Value & ";"
  39. password = "Pwd=" & Worksheets("Input").Range("I5").Value & ";"
  40. IPaddress = "Server=" & Worksheets("Input").Range("J5").Value & ";"
  41.  
  42. ConnectionString = "Provider=SQLNCLI11;" _
  43. & IPaddress _
  44. & "Database=SESG_General_DB;" _
  45. & username _
  46. & password _
  47. & "DataTypeCompatibility=80;"
  48.  
  49.  
  50. Conn.Open ConnectionString
  51.  
  52. StoredProcName = "[dbo].[IFRS_DATA_CMF_CHF_Accuracy]"
  53.  
  54. On Error Resume Next
  55. With Sheet2.ListObjects("Sheet2")
  56. .Range.AutoFilter
  57. .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
  58. .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
  59. End With
  60.  
  61.  
  62. MonthInput = Worksheets("Input").Range("C5").Value
  63. YearInput = Worksheets("Input").Range("B5").Value
  64.  
  65.  
  66. Set MonthVar = Command.CreateParameter("@Month", adInteger, adParamInput, , MonthInput)
  67. Set YearVar = Command.CreateParameter("@Year", adInteger, adParamInput, , YearInput)
  68.  
  69.  
  70. With Command
  71. .ActiveConnection = Conn
  72. .CommandType = adCmdStoredProc
  73. .CommandText = StoredProcName
  74. .NamedParameters = True
  75. .Parameters.Append MonthVar
  76. .Parameters.Append YearVar
  77.  
  78.  
  79. End With
  80.  
  81.  
  82. Set RecordSet = Command.Execute
  83.  
  84. fldCount = RecordSet.Fields.Count
  85. For iCol = 1 To fldCount
  86. Sheet2.Cells(3, iCol).Value = RecordSet.Fields(iCol - 1).Name
  87. Next iCol
  88. Sheet2.Range("A4").CopyFromRecordset RecordSet
  89.  
  90.  
  91.  
  92.  
  93. Set RecordSet = Command.Execute
  94.  
  95. Sheet2.Range("A" & LastRow).CopyFromRecordset RecordSet
  96.  
  97.  
  98.  
  99. RecordSet.Close
  100. Conn.Close
  101.  
  102. Application.ScreenUpdating = True
  103.  
  104.  
  105. Application.Calculate
  106.  
  107. ' ACTUAL QUERY
  108.  
  109. Application.ScreenUpdating = False
  110.  
  111. Set Conn = New ADODB.Connection
  112. Set RecordSet = New ADODB.RecordSet
  113. Set Command = New ADODB.Command
  114.  
  115. username = "Uid=" & Worksheets("Input").Range("H5").Value & ";"
  116. password = "Pwd=" & Worksheets("Input").Range("I5").Value & ";"
  117. IPaddress = "Server=" & Worksheets("Input").Range("J5").Value & ";"
  118.  
  119.  
  120. ConnectionString = "Provider=SQLNCLI11;" _
  121. & IPaddress _
  122. & "Database=SESG_General_DB;" _
  123. & username _
  124. & password _
  125. & "DataTypeCompatibility=80;"
  126.  
  127.  
  128.  
  129. Conn.Open ConnectionString
  130.  
  131. StoredProcName = "[dbo].[IFRS_DATA_ACTUAL_CHF_Accuracy]"
  132.  
  133. On Error Resume Next
  134. With Sheet2.ListObjects("Sheet2")
  135. .Range.AutoFilter
  136. .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
  137. .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
  138. End With
  139.  
  140.  
  141. MonthInput = Worksheets("Input").Range("C5").Value
  142. YearInput = Worksheets("Input").Range("B5").Value
  143.  
  144.  
  145. Set MonthVar = Command.CreateParameter("@Month", adInteger, adParamInput, , MonthInput)
  146. Set YearVar = Command.CreateParameter("@Year", adInteger, adParamInput, , YearInput)
  147.  
  148.  
  149. With Command
  150. .ActiveConnection = Conn
  151. .CommandType = adCmdStoredProc
  152. .CommandText = StoredProcName
  153. .NamedParameters = True
  154. .Parameters.Append MonthVar
  155. .Parameters.Append YearVar
  156.  
  157.  
  158. End With
  159.  
  160.  
  161. Set RecordSet = Command.Execute
  162.  
  163. fldCount = RecordSet.Fields.Count
  164. For iCol = 1 To fldCount
  165. Sheet2.Cells(14, iCol).Value = RecordSet.Fields(iCol - 1).Name
  166. Next iCol
  167. Sheet2.Range("A15").CopyFromRecordset RecordSet
  168.  
  169.  
  170.  
  171.  
  172. Set RecordSet = Command.Execute
  173.  
  174. Sheet2.Range("A" & LastRow).CopyFromRecordset RecordSet
  175.  
  176.  
  177.  
  178. RecordSet.Close
  179. Conn.Close
  180.  
  181. Application.ScreenUpdating = True
  182.  
  183.  
  184. Application.Calculate
  185.  
  186. 'TP QUERY
  187.  
  188. Application.ScreenUpdating = False
  189.  
  190. Set Conn = New ADODB.Connection
  191. Set RecordSet = New ADODB.RecordSet
  192. Set Command = New ADODB.Command
  193.  
  194. username = "Uid=" & Worksheets("Input").Range("H5").Value & ";"
  195. password = "Pwd=" & Worksheets("Input").Range("I5").Value & ";"
  196. IPaddress = "Server=" & Worksheets("Input").Range("J5").Value & ";"
  197.  
  198.  
  199. ConnectionString = "Provider=SQLNCLI11;" _
  200. & IPaddress _
  201. & "Database=SESG_General_DB;" _
  202. & username _
  203. & password _
  204. & "DataTypeCompatibility=80;"
  205.  
  206.  
  207.  
  208. Conn.Open ConnectionString
  209.  
  210. StoredProcName = "[dbo].[IFRS_DATA_TP_CHF_Accuracy]"
  211.  
  212. On Error Resume Next
  213. With Sheet2.ListObjects("Sheet2")
  214. .Range.AutoFilter
  215. .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
  216. .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
  217. End With
  218.  
  219.  
  220. ' January Data
  221. MonthInput = Worksheets("Input").Range("C5").Value
  222. YearInput = Worksheets("Input").Range("B5").Value
  223. TPm1Input = Worksheets("Input").Range("F5").Value
  224. TPm2Input = Worksheets("Input").Range("E5").Value
  225. TPm3Input = Worksheets("Input").Range("D5").Value
  226.  
  227.  
  228.  
  229. Set MonthVar = Command.CreateParameter("@Month", adInteger, adParamInput, , MonthInput)
  230. Set YearVar = Command.CreateParameter("@Year", adInteger, adParamInput, , YearInput)
  231. Set TPm1Var = Command.CreateParameter("@TPM1", adVarChar, adParamInput, 12, TPm1Input)
  232. Set TPm2Var = Command.CreateParameter("@TPM2", adVarChar, adParamInput, 12, TPm2Input)
  233. Set TPm3Var = Command.CreateParameter("@TPM3", adVarChar, adParamInput, 12, TPm3Input)
  234.  
  235.  
  236. With Command
  237. .ActiveConnection = Conn
  238. .CommandType = adCmdStoredProc
  239. .CommandText = StoredProcName
  240. .NamedParameters = True
  241. .Parameters.Append YearVar
  242. .Parameters.Append MonthVar
  243. .Parameters.Append TPm3Var
  244. .Parameters.Append TPm2Var
  245. .Parameters.Append TPm1Var
  246. End With
  247.  
  248.  
  249.  
  250. Set RecordSet = Command.Execute
  251.  
  252. fldCount = RecordSet.Fields.Count
  253. For iCol = 1 To fldCount
  254. Sheet2.Cells(25, iCol).Value = RecordSet.Fields(iCol - 1).Name
  255. Next iCol
  256. Sheet2.Range("A26").CopyFromRecordset RecordSet
  257.  
  258. Set RecordSet = Command.Execute
  259.  
  260. Sheet2.Range("A" & LastRow).CopyFromRecordset RecordSet
  261.  
  262.  
  263.  
  264. RecordSet.Close
  265. Conn.Close
  266.  
  267. Application.ScreenUpdating = True
  268.  
  269.  
  270. Application.Calculate
  271.  
  272.  
  273. End Sub
Add Comment
Please, Sign In to add comment