Capture Run-time error 3022 (PK duplicate values).

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,297
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Is there any way of capturing a Run-time error 3022?

The code I'm using is:
Code:
    Set rst = dbs.OpenRecordset("Contact_List_" & sType, dbOpenDynaset, dbAppendOnly)    
    For x = 3 To lLastRow
        rst.AddNew
        rst.Fields(0).Value = Forms("frmCalendar").clndr.Value
        For lngColumn = 0 To UBound(y)
            rst.Fields(lngColumn + 1).Value = xlc.Offset(0, y(lngColumn) - 1).Value
        Next lngColumn
        rst.Update
        Set xlc = xlc.Offset(1, 0)
    Next x

and it's falling over on the rst.Update part. I've added a On Error GoTo ERR_HANDLE at the top of the procedure, and this code at the end:
Code:
EXIT_PROC:
    On Error GoTo 0
    Exit Sub


ERR_HANDLE:
    Select Case Err.Number
        Case 3022
            DebugError "Duplicate record: " & x, "ImportFromExcel"
            Err.Clear
            Resume Next
        Case Else
            'DisplayError Err.Description, "mdlFunctions_Range.FindCell", True
            Resume EXIT_PROC
    End Select
    
End Sub
but it still stops on the update event.

Do I need to check first if the new record will be a duplicate, or is there any way of capturing it when it occurs?

Thanks for any help offered as usual. :)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I've figured it out and it was one of those real DOH! moments.

Earlier in the code I have this:
Code:
'Establish an EXCEL application object
    On Error Resume Next
    Set xlx = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    Err.Clear
    On Error GoTo 0

May help if I reset the On Error GoTo correctly. :)
 
Upvote 0

Forum statistics

Threads
1,215,938
Messages
6,127,777
Members
449,406
Latest member
Pavesib

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top