2015-07-20 73 views
0

我试图运行Excel宏(按钮)函数内部SP:试图在Excel(VBA宏)运行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 

的SP每次返回单行,和我试图用每个结果集写一行。发生什么事是我在电子表格中获得单行,例如,如上所示,我只会从B3获取第2行结果集,用于custID。如果我注释掉第一行,那么我只会得到第3行的B24 custID结果,依此类推。我永远不会超过1行,我不明白为什么(我对vb和excel宏是全新的)。任何人都可以解释发生了什么?即使我没有清除任何行,也会发生这种情况......谢谢!

更新:如果我设置了单独的按钮为每个功能的“召唤”,它运行良好......

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 

回答

0

初学者(Excel)中的错误...调用函数与SP的后第一次,活动工作表被改变为与输入不同的工作表,所以后续调用是使用空输入参数完成的! 这从帖子不明显..对不起!