Runtime error 2147467259(80004005): The changes you requested to the table were not successful because they would create duplicate values in the index, primary key..How to catch the exception?

Unfortunately there is no try catch block in VB6... You have to go back to the good old fashioned On Error

Do you want to handle the error in your code? or are you trying to figure out where it occurred?

If you're handling in the code you have two choices:

'Example of sub routine with GOTO
sub myGotoRoutine()

On Error Goto Err_Handler
'..... do what ever
Exit sub
'What do you want to do with the Error?
exit sub

'Example of sub routine with resume next
sub myNextRoutine()
On Error Resume Next
'Doing something that could lead to an error    
if Err.Number <> 0 then
        'Handle error
End if
Exit sub

The On Error Goto Option means that if there is an error go directly to the marked line of code. In the example I've done if there is an error anywhere in the sub routine then the code jumps down into my error handler if not, it will hit the line Exit sub beforehand and not go in.

The On Error Resume Next means exactly that, if there is an error, carry on to the next line of code. In my example I trap and handle the error on the next line.

If you are trying to figure out where it happens, you need to put brek points in your code and step through it.

As an educated guess, I'd say the code is trying to insert a record into a database table with a primary Key value that is already in use rather than an update.

Edited 3 Years Ago by G_Waddell

This question has already been answered. Start a new discussion instead.