vba 在访问中将数据从表单保存到表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15187943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:51:25  来源:igfitidea点击:

saving data from form to table in access

vbams-accessaccess-vba

提问by Kate Leiderman

I have a form, and I want to fill it, and then save some of the fields into an existing table called Order. I'm trying to do this with this line of code:

我有一个表单,我想填写它,然后将一些字段保存到名为 Order 的现有表中。我试图用这行代码来做到这一点:

CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES (' " & Me.order & " ')"

CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES (' " & Me.order & " ')"

I have also tried it like this

我也试过这样

CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES ( " & Me.order & " )"

CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES (" & Me.order & " )"

but it doesn't seem to make a difference. I keep getting the following error:

但它似乎没有什么区别。我不断收到以下错误:

run-time error '3134': syntax error in INSERT INTO statement.

运行时错误“3134”:INSERT INTO 语句中的语法错误。

what am I doing wrong?

我究竟做错了什么?

采纳答案by HansUp

Orderis a reserved word. If you must keep that as the table name, bracket it to avoid confusing the db engine.

Order是保留字。如果您必须将其保留为表名,请将其括起来以避免混淆数据库引擎。

Dim strInsert As String
strInsert = "INSERT INTO [Order] (OrderNumber) VALUES ('" & Me.order & "')"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailOnError

If OrderNumberis numeric data type instead of text, discard those single quotes from the INSERTstatement.

如果OrderNumber是数字数据类型而不是文本,则从INSERT语句中丢弃那些单引号。

Store your statement in a string variable. Then use Debug.Printto examine the completed statement you're asking the engine to execute. You can view the Debug.Printoutput in the Immediate window. Go there with Ctrl+gCopy the statement and paste it into SQL View of a new Access query for troubleshooting.

将您的语句存储在字符串变量中。然后用于Debug.Print检查您要求引擎执行的完整语句。您可以Debug.Print在“立即”窗口中查看输出。转到那里Ctrl+g复制语句并将其粘贴到新 Access 查询的 SQL 视图中以进行故障排除。