2017-01-08 122 views
0

我試圖將記錄添加到一個名爲「主題」(截至以下代碼SelectedTopicsCtl.ItemsSelected中的每個SelectedTopic「部分」)的現有表。 執行代碼時,我總是得到「運行時錯誤'3022':您請求的表的更改不成功,因爲它們會在索引,主鍵或關係中創建重複值,所以創建時出錯自動編號在字段「ID」(=唯一編入索引的字段 - 無重複) 調試時,下面代碼中的「TopicRecord.Update」行被突出顯示 我已閱讀了關於此主題的多篇文章論壇和其他論壇,但仍不能得到這個工作 - 我必須忽視的東西....訪問2010年 - 運行時錯誤3022

Private Sub Copy_Click() 

Dim JournalEntrySourceRecord, JournalEntryDestinationRecord, TopicRecord As Recordset 
Dim JournalEntryToCopyFromCtl, JournalEntryToCopyToCtl, JournalEntryDateCreatedCtl, SelectedTopicsCtl As Control 
Dim Counter, intI As Integer 
Dim SelectedTopic, varItm As Variant 

Set JournalEntryToCopyFromCtl = Forms![Copy Journal Entry]!JournalEntryToCopyFrom 
Set JournalEntryToCopyToCtl = Forms![Copy Journal Entry]!JournalEntryToCopyTo 
Set JournalEntryDateCreatedCtl = Forms![Copy Journal Entry]!JournalEntryDateCreated 
Set JournalEntrySourceRecord = CurrentDb.OpenRecordset("Select * from JournalEntries where ID=" & JournalEntryToCopyFromCtl.Value) 
Set JournalEntryDestinationRecord = CurrentDb.OpenRecordset("Select * from JournalEntries where ID=" & JournalEntryToCopyToCtl.Value) 
Set SelectedTopicsCtl = Forms![Copy Journal Entry]!TopicsToCopy 
Set TopicRecord = CurrentDb.OpenRecordset("Topics", dbOpenDynaset, dbSeeChanges) 


With JournalEntryDestinationRecord 
    .Edit 
    .Fields("InitiativeID") = JournalEntrySourceRecord.Fields("InitiativeID") 
    .Fields("DateCreated") = JournalEntryDateCreatedCtl.Value 
    .Fields("Comment") = JournalEntrySourceRecord.Fields("Comment") 
    .Fields("Active") = "True" 
    .Fields("InternalOnly") = JournalEntrySourceRecord.Fields("InternalOnly") 
    .Fields("Confidential") = JournalEntrySourceRecord.Fields("Confidential") 
    .Update 
    .Close 
End With 
JournalEntrySourceRecord.Close 
Set JournalEntrySourceRecord = Nothing 
Set JournalEntryDestinationRecord = Nothing 

For Each SelectedTopic In SelectedTopicsCtl.ItemsSelected 
    TopicRecord.AddNew 
    For Counter = 3 To SelectedTopicsCtl.ColumnCount - 1 
     TopicRecord.Fields(Counter) = SelectedTopicsCtl.Column(Counter, SelectedTopic) 
    Next Counter 
    TopicRecord.Fields("JournalEntryID") = JournalEntryToCopyToCtl.Value 
    TopicRecord.Fields("DateCreated") = JournalEntryDateCreatedCtl.Value 
    TopicRecord.Update 
Next SelectedTopic 
TopicRecord.Close 
Set TopicRecord = Nothing 
End Sub 

回答

0

首先,你的DIMS不會像您期望的工作中使用:

Dim JournalEntrySourceRecord As Recordset 
Dim JournalEntryDestinationRecord As Recordset 
Dim TopicRecord As Recordset 

其次,它看起來像你這裏包括您的ID:

TopicRecord.Fields(Counter) 

主題是一個查詢,包括它在某種程度上。嘗試專門指定字段和/或像這樣調試:

For Counter = 3 To SelectedTopicsCtl.ColumnCount - 1 
    TopicRecord.Fields(Counter).Value = SelectedTopicsCtl.Column(Counter, SelectedTopic) 
    Debug.Print Counter, TopicRecord.Fields(Counter).Name 
Next Counter