vba 违反主键约束。无法使用 ADO 在对象中插入重复键

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

violation of primary key constraint .Cannot insert duplicate key in object using ADO

ms-accessvbakeyconstraints

提问by Philippe Grondier

we are working on a users apllication using Access2003(VBA) as software language and SQL Server 2005 as database. We are using ADO method and we encounter a problem. when users create new record in a ADO Screen and they want to save the record after implementing it they receive this error : error -2147217873 violation of primary key constraint 'PK_ '.Cannot insert duplicate key in object 'Pk_...' Any help will be appreciated Thanks in advance

我们正在使用 Access2003(VBA) 作为软件语言和 SQL Server 2005 作为数据库的用户应用程序。我们正在使用 ADO 方法,但遇到了问题。当用户在 ADO 屏幕中创建新记录并希望在实现后保存记录时,他们会收到此错误:错误 -2147217873 违反主键约束“PK_”。无法在对象“Pk_...”中插入重复键任何帮助将不胜感激 提前致谢

回答by Dofs

The problem occures since you can't have two primary keys with the same value.

出现问题是因为您不能有两个具有相同值的主键。

If you are using Ints as primary key, remember to put auto-increment on it. If you are using GUID as primary key, you might forget to set the guid to sometheing else than the default empty guid, and there by trying to insert and empty guid twice.

如果您使用 Int 作为主键,请记住在其上放置自动增量。如果您使用 GUID 作为主键,您可能会忘记将 guid 设置为默认空 guid 以外的其他内容,并尝试插入并清空 guid 两次。

回答by Dariusz

Check witch columnt is your PrimaryKey. If you are trying to insert value that already exist then you are getting that error.

检查女巫 columnt 是您的主键。如果您尝试插入已存在的值,则会收到该错误。

回答by Philippe Grondier

you should create your PK value either from your code or on the SQL side. On the SQL side, when creating your database, you have to indicate that default value for "myPrimaryKey" field is uniqueIdentifier, while from code, you could have something like

您应该从您的代码或在 SQL 端创建您的 PK 值。在 SQL 方面,在创建数据库时,您必须指出“myPrimaryKey”字段的默认值是 uniqueIdentifier,而从代码中,您可以有类似

myRecordset.fields("myPrimaryKey") = stGuidGen()

(check herefor the stGuidGen function)

在此处查看 stGuidGen 函数)

There are some pros and cons to each method. By implementing the SQL method, you do not have to care about generating PKs anymore. By doing it through code, you can store the newly generated value without having to requery the database, and reuse it immediatly in your code, and this can be very useful.

每种方法都有一些优点和缺点。通过实现 SQL 方法,您不必再关心生成 PK 了。通过代码来做,你可以存储新生成的值而不必重新查询数据库,并立即在你的代码中重用它,这非常有用。

回答by anonymous

Are you trying to insert a new record with the primary key field having a value that is already in the database. The primary key field must always contain unique values.

您是否尝试插入一条新记录,其主键字段的值已存在于数据库中。主键字段必须始终包含唯一值。

回答by Segun Fakuade

get the property of your primary key column and set the identity property to YES

获取主键列的属性并将身份属性设置为 YES