从Access VBA调用存储过程

时间:2018-02-08 11:09:27

标签: sql-server vba ms-access stored-procedures

我需要使用VBA从远程SQL Server调用存储过程到Access,因为我需要传递动态变量。

我使用查询查询来调用此SP:

Exec GetPortfolioLookthroughMultiPeriod
  @PORTFOLIONAME ='wefb'
,@DATE= '2018-02-06'
, @TYPES='portfolio'
,@VALUES = 'portfolio'
,@ISLOOKTHROUGH=1
,@PORTFOLIOCURRENCY='PC'
,@EXPRISKLEVEL='currency'
,@NUMBEROFMONTHS=15
,@AssetClassStrategyName='fund'
,@NUMBEROFPERIODS=1
,@FREQUENCY='d'
,@ShowDetail=1
,@TableNo=6

使用以下ODBC连接字符串:

ODBC;Description=RiskManager;DRIVER=SQL Server Native Client 11.0;SERVER=APX-TEST01;UID=X.Tran;Trusted_Connection=Yes;DATABASE=RiskManager; 

现在我的VBA代码返回错误:

  

多步OLE DB操作生成错误。检查每个OLE DB状态值(如果可用)。没有工作。

以下是我正在使用的代码:

Dim conn As ADODB.Connection
Dim cmd As ADODB.Command

Set conn = New ADODB.Connection
conn.Open "ODBC;Description=RiskManager;DRIVER=SQL Server Native Client 11.0;SERVER=APX-TEST01;UID=X.Tran;Trusted_Connection=Yes;DATABASE=RiskManager;"
Set cmd = New ADODB.Command
With cmd
    .ActiveConnection = conn
    .CommandText = "GetPortfolioLookthroughMultiPeriod"
    .CommandType = adCmdStoredProc
    .CommandTimeout = 0
    .Parameters.Append .CreateParameter("@PORTFOLIONAME", adVarChar, adParamInput, 40, "wefb")
    .Parameters.Append .CreateParameter("@DATE", adDBDate, adParamInput, 50, #2/5/2018#)
    .Parameters.Append .CreateParameter("@TYPES", adVarChar, adParamInput, 10, "portfolio")
    .Parameters.Append .CreateParameter("@VALUES", adVarChar, adParamInput, 10, "portfolio")
    .Parameters.Append .CreateParameter("@ISLOOKTHROUGH", adVarNumeric, adParamInput, 10, 1)
    .Parameters.Append .CreateParameter("@PORTFOLIOCURRENCY", adVarChar, adParamInput, 10, "PC")
    .Parameters.Append .CreateParameter("@EXPRISKLEVEL", adVarChar, adParamInput, 10, "currency")
    .Parameters.Append .CreateParameter("@NUMBEROFMONTHS", adVarNumeric, adParamInput, 10, 15)
    .Parameters.Append .CreateParameter("@AssetClassStrategyName", adVarChar, adParamInput, 10, "fund")
    .Parameters.Append .CreateParameter("@NUMBEROFPERIODS", adVarNumeric, adParamInput, 10, 1)
    .Parameters.Append .CreateParameter("@FREQUENCY", adVarChar, adParamInput, 10, "d")
    .Parameters.Append .CreateParameter("@ShowDetail", adVarNumeric, adParamInput, 10, 1)
    .Parameters.Append .CreateParameter("@TableNo", adVarNumeric, adParamInput, 10, 6)
    .Execute
End With

1 个答案:

答案 0 :(得分:0)

您可以将调用原始文本用于sql命令

Dim conn As ADODB.Connection
Dim cmd As ADODB.Command

Set conn = New ADODB.Connection
conn.Open "ODBC;Description=RiskManager;DRIVER=SQL Server Native Client 11.0;SERVER=APX-TEST01;UID=X.Tran;Trusted_Connection=Yes;DATABASE=RiskManager;"
Set cmd = New ADODB.Command
With cmd
    .ActiveConnection = conn
    .CommandType = adCmdText
    .CommandText = "Exec GetPortfolioLookthroughMultiPeriod   @PORTFOLIONAME ='wefb' ,@DATE= '2018-02-06' , @TYPES='portfolio' ,@VALUES = 'portfolio' ,@ISLOOKTHROUGH=1 ,@PORTFOLIOCURRENCY='PC' ,@EXPRISKLEVEL='currency' ,@NUMBEROFMONTHS=15 ,@AssetClassStrategyName='fund' ,@NUMBEROFPERIODS=1 ,@FREQUENCY='d' ,@ShowDetail=1 ,@TableNo=6"
    .CommandTimeout = 0
    .Execute
End With

缺点是您不会获得结果,但是可以执行命令