使用VBA将参数传递给SQL过程的问题

时间:2010-04-11 07:59:16

标签: sql vba stored-procedures parameters

我正在尝试使用VBA将@intDocumentNo@intCustomerNo传递给存储过程,但只在dbo.tblOrderHead中更新了@intCustomerNo。我认为问题不在于程序,因为如果我手动输入值,它就可以正常运行。

我做错了什么?

VBA代码:

Private Sub intCustomerNo_Click()

Dim cmdCommand As New ADODB.Command

With cmdCommand
    .ActiveConnection = CurrentProject.Connection
    .CommandType = adCmdStoredProc
    .CommandText = "uspSelectCustomer"

        '@intDocumentNo
        .Parameters("@intDocumentNo").Value = Forms![frmSalesOrder].[IntOrderNo]

        '@intCustomerNo
        .Parameters("@intCustomerNo").Value = Me![intCustomerNo]

    .Execute

End With

DoCmd.Close
Forms![frmSalesOrder].Requery

End Sub

步骤:

UPDATE      dbo.tblOrderHead
SET         dbo.tblOrderHead.intCustomerNo   = @intCustomerNo ,
            dbo.tblOrderHead.intPaymentCode  = dbo.tblCustomer.intPaymentCode,
            dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
            dbo.tblOrderHead.txtRegionCode   = dbo.tblCustomer.txtRegionCode,
            dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
            dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM        dbo.tblOrderHead
INNER JOIN  dbo.tblCustomer ON dbo.tblOrderHead.intCustomerNo = 
            dbo.tblCustomer.intCustomerNo
AND         dbo.tblOrderHead.intOrderNo = @intDocumentNo

解决方案

将程序更改为此(以下建议可能也有效,但我尚未测试):

UPDATE      dbo.tblOrderHead
SET         dbo.tblOrderHead.intCustomerNo   = @intCustomerNo
WHERE       dbo.tblOrderHead.intOrderNo      = @intDocumentNo;


UPDATE      dbo.tblOrderHead
SET         dbo.tblOrderHead.intPaymentCode  = dbo.tblCustomer.intPaymentCode,
            dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
            dbo.tblOrderHead.txtRegionCode   = dbo.tblCustomer.txtRegionCode,
            dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
            dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM        dbo.tblOrderHead
INNER JOIN  dbo.tblCustomer ON dbo.tblOrderHead.intCustomerNo = 
            dbo.tblCustomer.intCustomerNo
AND         dbo.tblOrderHead.intOrderNo = @intDocumentNo

3 个答案:

答案 0 :(得分:1)

尝试在SQL sp中使用SET NOCOUNT OFF

updateinsert运行时,它会返回有关受影响的行数的信息(例如,当您在SSMS中运行时)。

当adodb.command收到此信息时,它会停止执行,因此只会执行您的第一个语句。

答案 1 :(得分:0)

您可以尝试创建Parameter个对象,然后将它们附加到Parameters集合。

以下是未经测试的。

Private Sub intCustomerNo_Click()

Dim cmdCommand As New ADODB.Command
Dim paramDocNo as ADODB.Parameter
Dim paramCustNo as ADODB.Parameter

Set paramDocNo = cmdCommand.CreateParameter("@intDocumentNo", adInteger, adParamInput)
Set paramCustNo = cmdCommand.CreateParameter("@intCustomerNo", adInteger, adParamInput)

cmdCommand.Parameters.Append paramDocNo
cmdCommand.Parameters.Append paramCustNo

paramDocNo.Value = Forms![frmSalesOrder].[IntOrderNo]
paramCustNo.Value = Me![intCustomerNo]

With cmdCommand
    .ActiveConnection = CurrentProject.Connection
    .CommandType = adCmdStoredProc
    .CommandText = "uspSelectCustomer"
    .Execute
End With

DoCmd.Close
Forms![frmSalesOrder].Requery

End Sub

答案 2 :(得分:0)

可能但你没有声明参数并使用旧的缓存副本吗?我个人发布参数时,我会使用这样的东西。我不是说这是导致问题的原因,而是以这种方式尝试并看看它是否有帮助

Set cmd = New ADODB.Command
With cmd
    .CommandText = "sptblTest_answers_UPSERT"
    .CommandType = adCmdStoredProc
    .ActiveConnection = dbCon

    .Parameters.Append .CreateParameter("@Answer_ID", adInteger, adParamInput, , Me.txtAnswer_ID)

    .Parameters.Append .CreateParameter("@Question_ID", adInteger, adParamInput, , Me.txtQuestion_ID)

    .Parameters.Append .CreateParameter("@Answer_number", adTinyInt, adParamInput, , Me.txtAnswer_number)


    .Execute
End with