VBA Run-time error 3134

时光总嘲笑我的痴心妄想 提交于 2020-01-03 15:12:32

问题


The following code creates a SQL string which produces a syntax error (3134) in MS Access.

        sql = "INSERT INTO tblItems (desc, descExtended, itemNumber, currentPrice) " & _
                     "VALUES (" & _
                     "'" & rs.Fields("Field6") & "', " & _
                     "'" & rs.Fields("Field7") & "', " & _
                     rs.Fields("Field1") & ", " & _
                     rs.Fields("Field8") & _
                     ")"
        db.Execute sql, dbFailOnError

The value of the "sql" string which produces the syntax error is:

"INSERT INTO tblItems (desc, descExtended, itemNumber, currentPrice) VALUES ('APPLE GRANNY SMITH SLI IQF', 'GEMS OF FRUIT', 2050791, 49)"

The table and field names are correct. The "desc" and "descExtended" fields are of type Text. "itemNumber" and "currentPrice" are Number.


回答1:


It's your field name. DESC is descending in SQL not description. DESC is a reserved word in SQL syntax. You will either need to put it in [] or change it. (I'd recommend the latter if its not too late to save future headache.) Avoid using reserved words as table or field names.

INSERT INTO tblItems ([desc], descExtended, itemNumber, currentPrice) 
VALUES ('APPLE GRANNY SMITH SLI IQF', 'GEMS OF FRUIT', 2050791, 49)

or better

INSERT INTO tblItems (Descript, descExtended, itemNumber, currentPrice) 
VALUES ('APPLE GRANNY SMITH SLI IQF', 'GEMS OF FRUIT', 2050791, 49)


来源:https://stackoverflow.com/questions/10593422/vba-run-time-error-3134

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!