+1  A: 

More of a strategy then a specific answer but ...

When importing data from external sources we'll often insert the data into staging tables that do not have the same keys/contraints placed on them and then sanitize the data prior to insertion.

What is done during "sanitation" depends on your requirements (for example, when you have two of the same account numbers are the records the same or are the data fields different, if the fields are different, how do you choose which data to use?). And then insert/move it into the production table once sanitization is complete.

James Conigliaro
+1  A: 

I ran into this problem and what I did is to make a collection that I stored the object and the key into the Key. If I try to add a duplicated key I get an error.

This is the easyest way I found to do this in vb6. in c# is dictionary.

Stéphane
i mean how to do that? can you help me.
pbrp
hi stephane,I didnt get you. can you explain me. Thanks in advance.
pbrp
My VB6 is a long way off. But here it goes. You create a collection. In this collection you try to add the key of you record, this can be a single column or an addition of multiple column... but something that you want to be unique. When you try to add this to the collection, if it's not there there won't be any problem. If the key is already there there will be an error generated by the collection. All you have to do is trap this error and act appropriately. At the end of it all you'll have only unique keys. Hope that helps
Stéphane
A: 

My suggestion would be to add error handling to CommitNew to see if the row inserted would create a primary key violation, and if so then perform other handling.

Example:

Sub CommitNew()
  ''#Add records to DneFrc table 
  On Error GoTo CommitNew_Error
  With rcddnefrc
    .Requery
    .AddNew
    .Fields![RTN] = rcdDNE.Fields![RTN]
    .Fields![AccountNbr] = rcdDNE.Fields![AccountNbr]
    .Fields![FirstName] = rcdDNE.Fields![FirstName]
    .Fields![MiddleName] = rcdDNE.Fields![MiddleName]
    .Fields![LastName] = rcdDNE.Fields![LastName]
    .Fields![Amount] = rcdDNE.Fields![Amount]
    .Update
  End With
  Exit Sub   ''# If no error, exit routine.
CommitNew_Error:
  If Err.Number = -2147217873 Then
    ''# code here will only execute if the constraint violation occurs
    Call WriteDuplicateAccountToFile()
    Err.Clear()       ''# This clears the error, since you handled it
  Else
    ''# Do stuff with other errors.
    ''# If you're not sure, at least display what error its giving, like so
    MsgBox "The following error was encountered when new record was saved:" & _
      vbNewLine & CStr(Err.Number) & " - " & Err.Description & vbNewLine & _
      "New record not saved.", vbOkOnly + vbCritical, "Error"
  End If
End Sub
Heather
Hi Heather,how the code will know that the particular error is primary key error?what is err.number? when i tried to execute the program I am getting error like "type mismatch"
pbrp
"Err.Number" is the number of the error thrown between "On Error Gogo CommitNew_Error" and "Exit Sub". You need to replace the words "numberOfPrimaryKeyError" with the actual number of the error message being displayed to you when the primary key violation is taking place.
Heather
This what I am getting,run time error'-2147217873(80040e2f)':violation of PRIMARY KEY CONSTRAINT'PK_t_DATA_DneFrc_5c29536': cannot insert duplicate key.in this which one do i need to insert.?? the runtime error?
pbrp
The '-2147217879' part of the error is the part you need to check against Err.Number. See my edit.
Heather