遇到最奇怪的问题。我有一个sql insert语句,在sql中正常工作。当我把那个sql放到vba中时,它在我的电脑上运行得非常好。但是,它不起作用并显示sql错误:缺少逗号。哪里可以问题???我使用Access 2010 plus,其他使用相同的Access版本并具有相同的ODB连接(DSN服务器)。一些代码示例:
sql = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST= ODB)(PORT=1520))" & _
"(CONNECT_DATA=(SERVICE_NAME=ABTL))); uid=IN; pwd=XXX;"
Set con = New ADODB.Connection
Set rec = New ADODB.Recordset
Set cmd = New ADODB.Command
con.Open sql
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)
Do While Not rst.EOF
insetSQL = con.Execute(" INSERT INTO STOCK (PNM_AUTO_KEY, PN, DESCRIPTION, HISTORICAL_FLAG, qty_oh, qty_adj, qty_available, CTRL_NUMBER, CTRL_ID, receiver_number, rec_date, serial_number,pcc_auto_key, cnc_auto_key, loc_auto_key, whs_auto_key, unit_cost, adj_cost, stc_auto_key, visible_mkt, remarks, ifc_auto_key, exp_date, unit_price, tagged_by, tag_date, owner, PART_CERT_NUMBER,ORIGINAL_PO_NUMBER, SHELF_LIFE, CTS_AUTO_KEY, MFG_LOT_NUM, AIRWAY_BILL )" & _
" VALUES ( " & rst![minimumas] & ", '" & rst![pn] & "', '" & "FROM_SCRIPT" & "', '" & "F" & "'," & rst![qty_oh] & "," & rst![qty_oh] & "," & rst![qty_oh] & "," & "G_STM_CTRL_NUMBER.nextval" & "," & "1" & ", '" & rst![receiver_number] & "', " & " TO_DATE('" & rst![rec_date] & "','YYYY-MM-DD'), '" & rst![serial_number] & "'," & rst![pcc_auto_key] & "," & rst![cnc_auto_key] & "," & rst![loc_auto_key] & "," & rst![whs_auto_key] & "," & rst![unit_cost] & "," & rst![unit_cost] & "," & "1" & ",'" & "T" & "', '" & rst![remarks] & "'," & _
"1" & ", " & " TO_DATE('" & rst![exp_date] & "','YYYY-MM-DD'), " & "0" & ",'" & rst![TAGGED_BY] & "', " & " TO_DATE('" & rst![tag_date] & "','YYYY-MM-DD'), '" & "" & "', '" & rst![PART_CERT_NUMBER] & "', '" & rst![ORIGINAL_PO_NUMBER] & "', '" & rst![SHELF_LIFE] & "', " & rst![CERT_SOURCE] & ", '" & rst![MFG_LOT_NUM] & "', '" & rst![AIRWAY_BILL] & "'" & " )")
Loop
答案 0 :(得分:0)
可能是某个rescordset字段是字符串数据类型,其值包含逗号,但您没有将其视为字符串。
例如
VALUES ( " & rst![minimumas] & ",
对于上述代码来处理minimumas,不能包含任何逗号。
您确定记录集中的所有字符串字段都已附加到带有“围绕它们”的SQL字符串中。
我们需要看到创建的sql字符串然后导致错误。请按要求提供。
=============================================== ==========
第2部分查看导致错误的SQL
替换
insetSQL = con.Execute(
与 在错误恢复下 strMySQl =当前在execute语句中使用的字符串 insetSQL = con.Execute(strMySQl) 如果err.number<> 0然后 停 debug.print err.number,err.description debug.print strMySQl 结束如果
然后将在即时窗口中打印的sql字符串发布到此站点。
我还建议您将计算机上生成的SQL字符串与其他计算机进行比较 - 以防发生奇怪的事情。