Access 2013 - 更改导致重复(运行时错误3022) [英] Access 2013 - Changes Caused Duplicate (Run-Time Error 3022)

查看:126
本文介绍了Access 2013 - 更改导致重复(运行时错误3022)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将记录添加到名为Topics的现有表中。 (以下代码中For EachToticsCtl.ItemsSelected中的For Each SelectedTopic部分)。执行代码时,我总是得到运行时错误'3022'':您向表请求的更改未成功,因为它们会在索引,主键或关系中创建重复值。因此,在字段ID中创建自动编号时出错了。 (=唯一被索引的字段 - 没有重复)。在调试时,行TopicRecord.Update在下面的代码中突出显示。我已经在这个论坛和其他论坛上阅读了几个关于这个主题的帖子,但仍然无法让它工作 - 我必须忽略一些东西......


展开 | 选择 | Wrap | 行号

解决方案

Jurgen:

因此,在字段ID中创建自动编号时出错了。 (=唯一被索引的字段 - 没有重复)。



你确定这是该字段的名称吗?看来你有另一个名为[JournalEntryID]的东西似乎对我来说更有可能,但我只是猜测来自有限的信息。


假设这是该字段的名称,而你的问题有这样的误导,那么你应该明白设置自动编号字段的值(正如你在第13行中所做的那样)通常是不正确的。有些情况下它是有意义的,例如从存档中恢复数据时,但作为一般规则,自动编号的全部要点是您不能手动设置它但允许Access(或Jet / ACE更准确) )自动为你设置。


我的假设当然是错的。请让我们知道完整的故事。


您好NeoPa,

感谢您的回复。

该字段的名称确实是" ID" - 这是一个自动编号字段,也是表格主题中的主键。字段JournalEntryID是另一个字段(没有索引,不是主键),是指向另一个名为JournalEntries的表的链接。

所以在附加代码的第13行中,我没有设置和AutoNumber的值领域。 AutoNumber字段位于TopicRecord.Fields(0)或TopicRecord.Fields(ID)。


  1. 我做了一个对您的代码进行一些更改,您现在应该可以正常工作。我看到的主要问题是您创建了一个名为SelectedTopicsCtl的对象变量作为Control,但未能设置对实际Control的显式引用。
  2. 您的变量声明中还需要更严格的Scope。
  3. With ... End With Construct应该比简单地重复Recordset对象更有效。
  4. 应该检查是否存在Items Items。
  5. 我只是为了方便而在本次演示中使用了Literals。
  6. 祝你好运。

    < span class =codeLinkonclick =Blur(this,this.parentNode.parentNode,getChildren(this),true);>展开 | 选择 | Wrap | 行号< /跨度>

I''m trying to add records to an exisiting table called "Topics" (section as of "For Each SelectedTopic In SelectedTopicsCtl.ItemsSelected" in the code below). When executing the code i always get "Run-time error ''3022'': The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship". So it goes wrong at the creation of the Autonumber in the field "ID" (= the only field that is indexed - no duplicates). When debugging, line "TopicRecord.Update" in the code below is highlighted. I have read several posts on this topic on this forum and on other forums but still cannot get this to work - i must be overlooking something....


Expand|Select|Wrap|Line Numbers

解决方案

Jurgen:
So it goes wrong at the creation of the Autonumber in the field "ID" (= the only field that is indexed - no duplicates).

Are you sure that''s the name of the field? It seems you have another called [JournalEntryID] which seems more likely to me, but I''m only guessing from limited information.

Assuming that is the name of the field, and your question had such a misdirection in it, then you should understand that setting the value of an AutoNumber field (as you seem to do in line #13) is generally not correct. There are situations where it makes sense, for instance when recovering data from an archive, but as a general rule the whole point of an AutoNumber is that you don''t set it manually but allow Access (or Jet/ACE to be more accurate) to set it for you automatically.

My assumption could be wrong of course. Please let us know the full story.


Hi NeoPa,
Thanks for your reply.
The name of the field is indeed "ID" - this is an AutoNumber field and is also the primary key in table "Topics". The field "JournalEntryID" is another field (not indexed, not a primary key) and is the link to another table called "JournalEntries".
So in line 13 of the attached code i am not setting the value of and AutoNumber field. The AutoNumber field is in TopicRecord.Fields(0) or TopicRecord.Fields(ID).


  1. I made a couple of changes to your Code and you should now have no problem getting it to work. The main problem that I saw was that you create an Object Variable named SelectedTopicsCtl as Control, but fail to set an explicit Reference to the actual Control.
  2. You also need much tighter Scope in your Variable Declarations.
  3. The With...End With Construct should be more efficient than simply repeating the Recordset Object.
  4. A check for the existence of Items Selected should be performed.
  5. I used Literals in this Demo only for convenience sake.
  6. Good Luck with your Project.

    Expand|Select|Wrap|Line Numbers


这篇关于Access 2013 - 更改导致重复(运行时错误3022)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆