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