重用ADO命令对象 - 第二次执行失败

时间:2017-08-02 15:48:55

标签: sql vba ms-access ado

我尝试重用ADO Command对象,从Excel工作表中的多个记录上运行相同的查询。就目前而言,代码在我删除参数并重新添加它们时起作用,但如果我在循环外添加一次参数然后尝试更改值,我会得到一个"数据类型不匹配的标准表达" cmd.Execute行上的错误。奇怪的是,第一次执行cmd.Execute时,没有错误。它是第二次尝试运行cmd.Execute引发错误。我不确定为什么除了不断删除和添加参数之外我什么都不做更改。这是表单中失败的代码片段,在循环外添加参数:

    cmd.CommandText = "INSERT INTO tblSALES VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
cmd.ActiveConnection = conn

trxLevel = conn.BeginTrans

With cmd.Parameters
    .Append cmd.CreateParameter("Customer", adWChar, adParamInput, 10)
    .Append cmd.CreateParameter("Item Code", adWChar, adParamInput, 8)  
    .Append cmd.CreateParameter("Start Date", adDate, adParamInput, 10)
    .Append cmd.CreateParameter("End Date", adDate, adParamInput, 10)

    .Append cmd.CreateParameter("Customer Sell-to", adWChar, adParamInput, 10)
    .Append cmd.CreateParameter("Invoice Id", adInteger, adParamInput)
    .Append cmd.CreateParameter("Invoice Date", adDate, adParamInput, 10)
    .Append cmd.CreateParameter("Order Date", adDate, adParamInput, 10)
    .Append cmd.CreateParameter("Order Number", adWChar, adParamInput, 10)
    .Append cmd.CreateParameter("PO Number", adWChar, adParamInput, 100)
    .Append cmd.CreateParameter("Trx Src Type", adWChar, adParamInput, 20)
    .Append cmd.CreateParameter("Order Type", adWChar, adParamInput, 10)
    .Append cmd.CreateParameter("Invoice Dtl Id", adInteger, adParamInput, 10)
    .Append cmd.CreateParameter("Ship To", adWChar, adParamInput, 6)
    .Append cmd.CreateParameter("Is Crossdock", adBoolean, adParamInput, 10)
    .Append cmd.CreateParameter("Division", adWChar, adParamInput, 2)
    .Append cmd.CreateParameter("Dept", adWChar, adParamInput, 3)
    .Append cmd.CreateParameter("Sales Rep", adWChar, adParamInput, 5)
    .Append cmd.CreateParameter("PL2", adWChar, adParamInput, 2)
    .Append cmd.CreateParameter("PL3", adWChar, adParamInput, 3)
    .Append cmd.CreateParameter("PL4", adWChar, adParamInput, 4)
    .Append cmd.CreateParameter("MDSE Grp", adWChar, adParamInput, 5)
    .Append cmd.CreateParameter("COA Prod Cd", adWChar, adParamInput, 2)
    .Append cmd.CreateParameter("Set Mdse Code", adWChar, adParamInput, 8)
    .Append cmd.CreateParameter("Line Sub Pos", adInteger, adParamInput)
    .Append cmd.CreateParameter("Price", adCurrency, adParamInput)
    .Append cmd.CreateParameter("Discount Price", adCurrency, adParamInput)
    .Append cmd.CreateParameter("Qty", adInteger, adParamInput)
    .Append cmd.CreateParameter("Amt", adCurrency, adParamInput)
End With

cmd.Parameters("Start Date").Value = Trim(StartDate(1))
cmd.Parameters("End Date").Value = Trim(EndDate(1))

For x = 2 To lrow
    If sht.Rows(x).EntireRow.Hidden = True Then
        GoTo nextIter
    End If
    If sht.Cells(x, 1).Value = "TOTAL" Then
        GoTo nextIter
    End If

    For Each param In cmd.Parameters
        If Not param.Name = "Start Date" And Not param.Name = "End Date" Then
            If param.Name = "Is Crossdock" Then
                param.Value = IIf(sht.Cells(x, dy.Item(param.Name)).Value = "Yes", True, False)
            ElseIf param.Name = "Division" Then
                arr = Split(sht.Cells(x, dy.Item(param.Name)).Value, "-")
                param.Value = Trim(arr(0))
            ElseIf param.Name = "Set Mdse Code" Or param.Name = "Item Code" Then
                If IsEmpty(sht.Cells(x, dy.Item(param.Name)).Value) Or sht.Cells(x, dy.Item(param.Name)).Value = "" Then
                    param.Value = "(blank)"
                Else
                    param.Value = Left(sht.Cells(x, dy.Item(param.Name)).Value, Len(sht.Cells(x, dy.Item(param.Name)).Value) - 2)
                End If
            ElseIf param.Name = "Order Type" And (IsEmpty(sht.Cells(x, orderType.Column).Value) Or sht.Cells(x, orderType.Column).Value = "") Then
                param.Value = "(blank)"
            Else
                param.Value = sht.Cells(x, dy.Item(param.Name)).Value
            End If
        End If
    Next param
    cmd.Execute
NextIter:
Next x

0 个答案:

没有答案