【问题标题】:MS Access 2007 Append query troubleMS Access 2007 追加查询问题
【发布时间】:2011-09-29 15:27:18
【问题描述】:

我有一个追加查询,它试图将一些记录追加到我的一个表中。但是,我收到一条错误消息,提示“由于密钥违规,没有添加 1200 条记录”。 1200 是我尝试追加的记录总数。我不明白为什么会出现此错误,因为目标表中的所有列都允许重复(即使此附加查询不重复任何信息),并且如果我复制表的结构并将记录附加到那,一切正常。

问题似乎是我将数据附加到已经有现有数据的表中。有人可以就我如何解决这个问题提供一些建议吗?

谢谢

【问题讨论】:

  • 你的表没有主键?
  • 您的表格似乎有一个自动编号 ID 字段,而您正试图附加到该字段。否则,您如何处理数据在很大程度上取决于您最终想要得到什么。
  • 您的表是否与另一个表定义了任何外键关系?
  • 自动编号字段确定该字段的默认值。但是,如果没有唯一约束,您可以在自动编号字段中插入重复值。 IOW,执行复制现有自动编号的 DML INSERT 不会触发密钥冲突错误。
  • 如果Autonumber字段是PK会触发错误,因为PK有唯一的约束。

标签: ms-access ms-access-2007


【解决方案1】:

确认您没有忽略表上的任何唯一索引。将此过程保存在标准模块中,并从即时窗口中使用目标表的名称调用它。

Public Sub InspectIndexes(ByVal pTable As String)
    Dim db As DAO.Database
    Dim i As Long
    Dim j As Long
    Dim strFields As String

    Set db = CurrentDb
    With db.TableDefs(pTable)
        Debug.Print "Indexes.Count = "; .Indexes.Count
        For i = 0 To (.Indexes.Count - 1)
        With .Indexes(i)
            Debug.Print i + 1 & ": Index Name = "; .name
            If .Primary Then
                Debug.Print vbTab & "Primary Key (Unique)"
            Else
                Debug.Print vbTab & "Unique: "; .Unique
            End If
            Debug.Print vbTab & "Fields.Count = "; .Fields.Count
            strFields = vbNullString
            For j = 0 To (.Fields.Count - 1)
                strFields = strFields & "; " & .Fields(j).name
            Next j
            strFields = Mid(strFields, 3)
            Debug.Print vbTab & "Fields: "; strFields
        End With
        Next i
    End With

    Set db = Nothing
End Sub

这是示例输出,其中 tblFoo 有 3 个索引: id 上的主键(根据定义是唯一的); num_field1 和 num_field2 上的唯一索引;以及 parent_id 上的非唯一索引。

InspectIndexes "tblfoo"
Indexes.Count =  3 
1: Index Name = both_num_fields
    Unique: True
    Fields.Count =  2 
    Fields: num_field1; num_field2
2: Index Name = parent_id
    Unique: False
    Fields.Count =  1 
    Fields: parent_id
3: Index Name = pkey
    Primary Key (Unique)
    Fields.Count =  1 
    Fields: id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多