0

上个月,我使用 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 语句中的两个字段一起使用?

4

1 回答 1

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

...........

于 2019-05-06T13:42:23.367 回答