如何在.CommandText Array(... SELECT ....)语句中修复运行时错误'5'

时间:2019-05-05 05:58:57

标签: excel vba qodbc

我正在使用QODBC驱动程序检索客户上个月从QuickBooks到Excel工作表的每月固定费用。一切正常,直到我从“客户”记录中添加了另一个字段。它失败,并显示运行时错误“ 5”:无效的过程调用或参数。

具有字段名称Customer.FullName作为字段之一的SELECT语句,它可以正常工作。如果我用Customer.IsActive字段替换Customer.FullName,它也可以正常工作。但是,如果我添加字段“ Customer.FullName,Customer.IsActive”,则执行SELECT语句会出现错误。我研究了这个问题,但是找不到原因或解决方法。

此代码失败:

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
    "ODBC;DSN=QuickBooks Data QRemote;SERVER=QODBC;OptimizerDBFolder=%UserProfile%\QODBC Driver for QuickBooks\Optimizer;OptimizerAllowDirtyReads" _
    ), Array("=N;SyncFromOtherTables=N;ForceSDKVersion=<default SDK>")), _
    Destination:=Range("$A$1")).QueryTable
'        .CommandText = CmdString
'        .CommandText = Array(SelClause, FromClause, WhereClause)

    .CommandText = Array( _
    "SELECT Charge.CustomerRefListID, Customer.FullName, Customer.IsActive, Charge.TxnDate, Item.FullName, Charge.Rate, Charge.""Desc""" & Chr(13) & "" & Chr(10) & _
     "FROM Charge Charge, Customer Customer, Item Item" & Chr(13) & "" & Chr(10) & _
     "WHERE Charge.CustomerRefListID = Customer.ListID AND Charge.ItemRefListID = Item.ListID AND " _
    , "((Charge.TxnDate=" & formateddate & ") AND (Item.SalesOrPurchaseAccountRefFullName='Weekly Services'))")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "Table_Query_Charges_from_QuickBooks_Data"
    .Refresh BackgroundQuery:=False
End With

下面的代码中的SELECT语句可以正常工作。

    .CommandText = Array( _
    "SELECT Charge.CustomerRefListID, Customer.FullName, Charge.TxnDate, Item.FullName, Charge.Rate, Charge.""Desc""" & Chr(13) & "" & Chr(10) & _
     "FROM Charge Charge, Customer Customer, Item Item" & Chr(13) & "" & Chr(10) & _
     "WHERE Charge.CustomerRefListID = Customer.ListID AND Charge.ItemRefListID = Item.ListID AND " _
    , "((Charge.TxnDate=" & formateddate & ") AND (Item.SalesOrPurchaseAccountRefFullName='Weekly Services'))")

这也可以。

    .CommandText = Array( _
    "SELECT Charge.CustomerRefListID, Customer.IsActive, Charge.TxnDate, Item.FullName, Charge.Rate, Charge.""Desc""" & Chr(13) & "" & Chr(10) & _
     "FROM Charge Charge, Customer Customer, Item Item" & Chr(13) & "" & Chr(10) & _
     "WHERE Charge.CustomerRefListID = Customer.ListID AND Charge.ItemRefListID = Item.ListID AND " _
    , "((Charge.TxnDate=" & formateddate & ") AND (Item.SalesOrPurchaseAccountRefFullName='Weekly Services'))")

Customer.FullName和Customer.IsActive这两个字段都是有效字段,而SELECT语句可以很好地使用其中之一。 如何使其与SELECT语句中的两个字段一起使用?

1 个答案:

答案 0 :(得分:0)

我做了更多实验,将select语句从Array()更改为String可以正常工作。 ......

   CmdString = "SELECT Charge.CustomerRefListID, Customer.FullName, Customer.IsActive, Charge.TxnDate, Item.FullName, Charge.Rate, Charge.""Desc"" FROM Charge Charge, Customer Customer, Item Item WHERE Charge.CustomerRefListID = Customer.ListID AND Charge.ItemRefListID = Item.ListID AND ((Charge.TxnDate=" & formateddate & ") AND (Item.SalesOrPurchaseAccountRefFullName='Weekly Services'))"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
    "ODBC;DSN=QuickBooks Data QRemote;SERVER=QODBC;OptimizerDBFolder=%UserProfile%\QODBC Driver for QuickBooks\Optimizer;OptimizerAllowDirtyReads" _
    ), Array("=N;SyncFromOtherTables=N;ForceSDKVersion=<default SDK>")), _
    Destination:=Range("$A$1")).QueryTable
    .CommandText = CmdString
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "Table_Query_Charges_from_QuickBooks_Data"
    .Refresh BackgroundQuery:=False
End With

........