当我在“创建”>“一个接一个地查询”中执行以下两个查询时,它们可以正常工作。当我尝试在VBA中执行查询时,仅第一个(发票行)有效。
我知道事实上不是问题本身,而是我可能建立连接的方式。尝试执行查询时出现错误无效词法。
按下按钮时,此查询将正确执行
buttonOne_Click()
Const adOpenStatic = 3
Const adLockOptimistic = 3
Dim oConnection
Dim oRecordset
Dim sMsg
Dim sConnectString
Dim sSQL
sConnectString = "DSN=Quickbooks Data;OLE DB Services=-2;"
sSQL = "INSERT INTO InvoiceLine (InvoiceLineItemRefListID, InvoiceLineDesc, InvoiceLineRate, InvoiceLineAmount, InvoiceLineSalesTaxCodeRefListID, FQSaveToCache) VALUES ('80002436-1519061496', 'Building permit 1', 1.00000, 1.00, '80000001-1478562826', 1)"
Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
oConnection.Open sConnectString
oConnection.Execute (sSQL)
sMsg = sMsg & "Invoice details were gathered!"
MsgBox sMsg
End Sub
这失败并显示错误
buttonTwo_Click()
Const adOpenStatic = 3
Const adLockOptimistic = 3
Dim oConnection
Dim oRecordset
Dim sMsg
Dim sConnectString
Dim sSQL
sConnectString = "DSN=Quickbooks Data;OLE DB Services=-2;"
sSQL = "INSERT INTO Invoice (CustomerRefListID, ARAccountRefListID, TxnDate, RefNumber, BillAddressAddr1, BillAddressAddr2, BillAddressCity, BillAddressState, BillAddressPostalCode, BillAddressCountry, IsPending, TermsRefListID, DueDate, ShipDate, ItemSalesTaxRefListID, Memo, IsToBePrinted, CustomerSalesTaxCodeRefListID) VALUES ('800001F6-1482536280', '8000001E-1478562986', #9/23/2020#, '1', 'Brad Lamb', '1921 Appleseed Lane', 'Bayshore', 'CA', '94326', 'USA', 0, '80000002-1478562832', #10/31/2020#, #10/01/2020#, '8000295C-1541711590', 'Memo Test', 0, '80000001-1478562826')"
Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
oConnection.Open sConnectString
oConnection.Execute (sSQL)
sMsg = sMsg & "Invoice was Sent to QuickBooks"
MsgBox sMsg
End Sub
第二个查询依赖于第一个查询完成,再次使两个查询在Microsoft Access的查询设计器中都能正常工作,但是第二个查询在VBA SQL中的宏或过程中运行时失败。
这是我看到的错误:
运行时错误'-2147217900(80040e14)':
[QODBC] [sql语法错误]找不到预期的词法元素:=)
更新
我没有运气就尝试了所有这些,还有其他建议吗?
2005年10月1日
2005-01-10
2005年10月10日
2005/01/10
答案 0 :(得分:1)
正确的格式似乎是{d'YYYY-MM-DD'} 提供了使日期成为正确字符串的功能;
功能:
Function fncqbDate(myDate As Date) As String
myDate = Nz(myDate, Now)
fncqbDate = "{d '" & Year(myDate) & "-" & Right("00" & Month(myDate), 2) & "-" & Right("00" & Day(myDate), 2) & "'}"
End Function
答案 1 :(得分:1)
正如@Minty所评论和发布的那样,您的日期才是问题。
没有两个SQL方言是相同的,但是大多数尝试符合ANSI标准。因此,相同的查询 可能在查询设计器和VBA之间失败。
MS Access日期(如果按字面发送)应该用##
封装:
INSERT INTO Invoice (CustomerRefListID, ARAccountRefListID, TxnDate, RefNumber,
BillAddressAddr1, BillAddressAddr2, BillAddressCity, BillAddressState,
BillAddressPostalCode, BillAddressCountry, IsPending, TermsRefListID,
DueDate, ShipDate, ItemSalesTaxRefListID, [Memo], IsToBePrinted,
CustomerSalesTaxCodeRefListID)
VALUES ('800001F6-1482536280', '8000001E-1478562986', #9/23/2020#, '1', 'Brad Lamb',
'1921 Appleseed Lane', 'Bayshore', 'CA', '94326', 'USA', 0, '80000002-1478562832',
#10/31/2020#, #10/01/2020#, '8000295C-1541711590',
'Memo Test', 0, '80000001-1478562826')
或者,使用CDate()
将字符串转换为日期:
INSERT INTO Invoice (CustomerRefListID, ARAccountRefListID, TxnDate, RefNumber,
BillAddressAddr1, BillAddressAddr2, BillAddressCity, BillAddressState,
BillAddressPostalCode, BillAddressCountry, IsPending, TermsRefListID,
DueDate, ShipDate, ItemSalesTaxRefListID, [Memo], IsToBePrinted,
CustomerSalesTaxCodeRefListID)
VALUES ('800001F6-1482536280', '8000001E-1478562986', CDate('9/23/2020'), '1', 'Brad Lamb',
'1921 Appleseed Lane', 'Bayshore', 'CA', '94326', 'USA', 0, '80000002-1478562832',
CDate('10/31/2020'), CDate('10/01/2020'), '8000295C-1541711590',
'Memo Test', 0, '80000001-1478562826')
在Quickbooks中,您必须遵守其日期要求{d 'YYYY-MM-DD'}
或功能形式fncqbDate()
。但是,关于这些方法的文档很少,并且随ODBC驱动程序版本的不同而不同。
sql = "INSERT INTO Invoice (CustomerRefListID, ARAccountRefListID, TxnDate, RefNumber, " _
& " BillAddressAddr1, BillAddressAddr2, BillAddressCity, " _
& " BillAddressState, BillAddressPostalCode, BillAddressCountry, " _
& " IsPending, TermsRefListID, DueDate, ShipDate, " _
& " ItemSalesTaxRefListID, [Memo], IsToBePrinted, " _
& " CustomerSalesTaxCodeRefListID) " _
& " VALUES ('800001F6-1482536280', '8000001E-1478562986', {d '2020-09-23'}, '1', " _
& " 'Brad Lamb', '1921 Appleseed Lane', 'Bayshore', 'CA', '94326', 'USA', 0, " _
& " '80000002-1478562832', {d '2020-10-31'}, {d '2020-10-01'}, " _
& " '8000295C-1541711590', 'Memo Test', 0, '80000001-1478562826')"
' PREPARED STATEMENT WITH PLACEHOLDERS (NO LITERAL DATA)
sql = "INSERT INTO Invoice (CustomerRefListID, ARAccountRefListID, TxnDate, RefNumber, " _
& " BillAddressAddr1, BillAddressAddr2, BillAddressCity, " _
& " BillAddressState, BillAddressPostalCode, BillAddressCountry, " _
& " IsPending, TermsRefListID, DueDate, ShipDate, " _
& " ItemSalesTaxRefListID, [Memo], IsToBePrinted, " _
& " CustomerSalesTaxCodeRefListID) " _
& " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?," _
& " ?, ?, ?, ?, ?, ?, ?, ?, ?)"
' OPEN CONNECTION
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open sConnectString
' INITIALIZE AND RUN COMMAND
Set oCmd = CreateObject("ADODB.Command") ' NEW ADO OBJECT
With oCmd
.ActiveConnection = oConnection
.CommandText = sql
.CommandType = adCmdText
' BIND PARAMETERS
.Parameters.Append .CreateParameter("pm1", adVarChar, adParamInput, ,"800001F6-1482536280")
.Parameters.Append .CreateParameter("pm2", adVarChar, adParamInput, ,"8000001E-1478562986")
.Parameters.Append .CreateParameter("pm3", adDate, adParamInput, , CDate("9/23/2020"),
.Parameters.Append .CreateParameter("pm4", adVarChar, adParamInput, ,"1")
.Parameters.Append .CreateParameter("pm5", adVarChar, adParamInput, ,"Brad Lamb")
.Parameters.Append .CreateParameter("pm6", adVarChar, adParamInput, ,"1921 Appleseed Lane")
.Parameters.Append .CreateParameter("pm7", adVarChar, adParamInput, ,"Bayshore")
.Parameters.Append .CreateParameter("pm8", adVarChar, adParamInput, ,"CA")
.Parameters.Append .CreateParameter("pm9", adVarChar, adParamInput, ,"94326")
.Parameters.Append .CreateParameter("pm10", adVarChar, adParamInput, ,"USA")
.Parameters.Append .CreateParameter("pm11", adInteger, adParamInput, , 0)
.Parameters.Append .CreateParameter("pm12", adVarChar, adParamInput, ,"80000002-1478562832")
.Parameters.Append .CreateParameter("pm13", adDate, adParamInput, , CDate("10/31/2020"))
.Parameters.Append .CreateParameter("pm14", adDate, adParamInput, , CDate("10/01/2020"))
.Parameters.Append .CreateParameter("pm15", adVarChar, adParamInput, ,"8000295C-1541711590")
.Parameters.Append .CreateParameter("pm16", adVarChar, adParamInput, ,"Memo Test")
.Parameters.Append .CreateParameter("pm17", adInteger, adParamInput, ,0)
.Parameters.Append .CreateParameter("pm18", adVarChar, adParamInput, ,"80000001-1478562826")
' RUN PARAMETERIZED QUERY
.Execute
End With
oConnection.Close
Set oCmd = Nothing: Set oConnection = Nothing
答案 2 :(得分:0)
如果这是一个Access应用程序,我建议不要将查询重写为sql / vba代码,而采取另一种方法:将工作查询保留为查询对象,而使用vba触发它:
DoCmd.OpenQuery "QueryName"
其优点是该查询对象存在于导航窗格中,因此您可以手动运行它以进行测试-无需运行任何vba。这是一种简单得多的调试策略。