ACCESS/VBA: Insert Into with autonumbering field 返回恼人的警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5005694/
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 12:40:14  来源:igfitidea点击:

ACCESS/VBA: Insert Into with autonumbering field returns annoying warning

sqlms-accessvbaaccess-vbams-access-2010

提问by Christian M

Ok, here's the situation
Let's have Table1(A,B,C)
A is an autonumbering field.

好的,这里的情况
让我们有 Table1(A,B,C)
A 是一个自动编号字段。

I'm feeding the table via vba.
Since A is autonumbering, I'm ignoring it like so:

我正在通过 vba 喂桌子。
由于 A 是自动编号的,我像这样忽略它:

SQL = INSERT INTO TABLE1(B,C) VALUES ('something','something else')
DoCmd.RunSQL SQL

This works ok, access gives me a 1st warning that I'll be creating a new row.
Which is ok to me. However just after that I get this:

这工作正常,访问给了我第一个警告,我将创建一个新行。
这对我来说没问题。然而,在那之后我得到了这个:

Microsoft Access can't add all the records in the update or append query.
It set 1 field(s) to Null due to a type conversion failure.
blahblahblah click ok to run the query anyway

Microsoft Access 无法在更新或追加查询中添加所有记录。
由于类型转换失败,它将 1 个字段设置为 Null。
blahblahblah 单击确定以运行查询

Which doesn't prevent it from working if I click ok, but I don't want my user to see that warning.
And anyway why does it pop up ? Isn't it normal to leave the autonumbering field blank ?
Is there another procedure I don't know about ? What am I missing ?

如果我单击确定,这不会阻止它工作,但我不希望我的用户看到该警告。
无论如何它为什么会弹出?将自动编号字段留空是不是很正常?
还有我不知道的其他程序吗?我错过了什么?

I looked around google and here but couldn't find an answer :/

我环顾了谷歌和这里,但找不到答案:/

(I don't want to setwarnings to false since I want the first warning of adding a field and any other eventual error to be visible.)

(我不想将警告设置为 false,因为我希望添加字段的第一个警告和任何其他最终错误都是可见的。)

回答by HansUp

Microsoft Access can't add all the records in the update or append query. It set 1 field(s) to Null due to a type conversion failure.

Microsoft Access 无法在更新或追加查询中添加所有记录。由于类型转换失败,它将 1 个字段设置为 Null。

That error has nothing to do with your autonumber field. Check the data types of fields B and C.

该错误与您的自动编号字段无关。检查字段 B 和 C 的数据类型。

In this example, field C is set to number, so I get the same error as you, and the INSERT succeeds but with Null in field C:

在此示例中,字段 C 设置为数字,因此我得到与您相同的错误,并且 INSERT 成功,但字段 C 中为 Null:

DoCmd.RunSQL "INSERT INTO TABLE1(B,C) VALUES ('something','something else')"

However, inserting a numeric value into field C works fine.

但是,将数字值插入字段 C 可以正常工作。

DoCmd.RunSQL "INSERT INTO TABLE1(B,C) VALUES ('something',99)"

Edit: This one also works in spite of supplying a text value for field C. The difference is the text value is one the database engine can convert to a valid number:

编辑:尽管为字段 C 提供了文本值,但该值也有效。不同之处在于文本值是数据库引擎可以转换为有效数字的值:

DoCmd.RunSQL "INSERT INTO TABLE1(B,C) VALUES ('something','27')"

回答by Tiago Cardoso

Is it clearly due to the autonumbering field? Once the record is added (running the query clicking on 'run anyway'), only the autonumber field is blank?

是否显然是由于自动编号字段?添加记录后(运行查询,单击“无论如何都要运行”),只有自动编号字段为空?

This error shouldn't be happening for Autonumber; at least, I've never seen it before.

Autonumber 不应发生此错误;至少,我以前从未见过。

回答by Justin Clarke

It sounds like you are passing in an empty string to one of the values and it's setting that column to NULL.

听起来您正在将一个空字符串传递给其中一个值,并将该列设置为 NULL。

When you say

当你说

Which doesn't prevent it from working if I click ok

如果我单击确定,这不会阻止它工作

Do all of the fields get populated correctly? Including the Autonumber?

是否所有字段都正确填充?包括自动编号?