简体   繁体   English

尝试多次在Excel(VBA宏)中运行SP

[英]Trying to run SP in excel (vba macro) multiple times

I am trying to run a SP inside a function in an excel macro (button): 我正在尝试在excel宏(按钮)的函数内运行SP:

Sub Button13_Click()
    Call exec_sp_GetLoan_CFM_Info_by_customerID("B3", 2, 1)
    Call exec_sp_GetLoan_CFM_Info_by_customerID("B24", 3, 1)
    Call exec_sp_GetLoan_CFM_Info_by_customerID("B45", 4, 1)
    Call exec_sp_GetLoan_CFM_Info_by_customerID("B66", 5, 1)
    Call exec_sp_GetLoan_CFM_Info_by_customerID("B86", 6, 1)
End Sub

The SP returns a single row every time, and I'm trying to write a row with every result set. SP每次都返回一行,而我正在尝试为每个结果集写一行。 What happens is that I'm getting a single row in the spreadsheet, for example, as it is shown above, I would only get row 2 with result set for custID from B3. 发生的情况是,例如,如上所示,我在电子表格中只得到一行,我只会从B3中得到带有为ustID设置的结果的第二行。 If I comment out the first line, I would only get row 3 with results for custID from B24, and so on. 如果我注释掉第一行,那么我只会从B24中获得带有custID结果的第3行,依此类推。 I can never get more than 1 row, and I don't understand why (I'm completely new to vb and excel macros). 我永远不会获得超过1行,而且我也不明白为什么(我对vb和excel宏是完全陌生的)。 Can anybody explain what's happening? 有人可以解释发生了什么吗? This is happening even if I don't clear any rows... thank you! 即使我不清除任何行,这种情况仍在发生...谢谢!

Update: If I set up separate buttons for each 'Call' of the function, it runs fine... 更新:如果我为函数的每个“调用”设置了单独的按钮,它将运行正常...

Function exec_sp_GetLoan_CFM_Info_by_customerID(custIDcell As String, stRow As Integer, stCol As Integer)
 Dim con As ADODB.Connection
 Dim cmd As ADODB.Command
 Dim rs As ADODB.Recordset
 Dim WSP1 As Worksheet
 Set con = New ADODB.Connection
 Set cmd = New ADODB.Command
 Set rs = New ADODB.Recordset

 Application.DisplayStatusBar = True
 Application.StatusBar = "Contacting SQL Server..."

' Log in
 con.Open "Provider=SQLOLEDB;Data Source=xxxx;Initial Catalog=xxxx ID=xxxx;Password=xxxx;"
 cmd.ActiveConnection = con

 ' Set up the parameter for the Stored Procedure
 cmd.Parameters.Append cmd.CreateParameter("custID", adVarChar, adParamInput, 8, Range(custIDcell).Text)

 Application.StatusBar = "Running stored procedure..."
 cmd.CommandText = "sp_GetLoan_CFM_Info_by_customerID"
 Set rs = cmd.Execute(, , adCmdStoredProc)

 Set WSP1 = ActiveWorkbook.Worksheets("CIFInfo")
 WSP1.Activate

 'clear row
 WSP1.Rows(stRow).Clear

 If rs.EOF = False Then WSP1.Cells(stRow, stCol).CopyFromRecordset rs

 'cmd.Parameters("custID").Value = Range("B24").Text

 'cleanup
 rs.Close
 Set rs = Nothing
 Set cmd = Nothing

 con.Close
 Set con = Nothing

 Application.StatusBar = "Data successfully updated."

End Function

Beginner's (Excel) mistake... after calling the function with the SP for the first time, the active worksheet gets changed to a different one from the input, so subsequent calls are done with an empty input parameter!! 初学者(Excel)错误...第一次使用SP调用该函数后,活动工作表将更改为与输入不同的工作表,因此后续调用将使用空的输入参数进行! That wasn't obvious from the post.. sorry!! 从帖子中看不出来..对不起!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM