Avatar of Rob4077
Rob4077
Flag for Australia asked on

On Error GoTo MyErrorRoutine except when error number is 3022

I have a routine that does a fair amount of work on a table. I need a general error trap in case it encounters an unexpected error, however in just one small section of code I need to have it ignore a .Update command if the error code is 3022 (duplicate key) and just abandon the .Update. Can anyone tell me how to do it?

eg

with MyTable
    .Addnew
    !Value = "My Value"
    !Value2 = "My Other Value"
    .Update    'only in this step of the code, abandon the update if the record has already been added
End With
Microsoft Access

Avatar of undefined
Last Comment
Rob4077

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
John Mc Hale

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
John Mc Hale

BTW:

Depeding on your 'production' code, you might also need an 'Undo' statement; e.g.
On Error Goto MyErrorRoutine

With MyTable
.AddNew
!Value="My Value"
!Value2="My Other Value"
.Update
MyResume:
End With

MyOtherPlace:
Exit Sub

MyErrorRoutine:
If Err = 3022 Then
  MyTable.Undo
  Resume MyResume
Else
  ' handle error normally
  MsgBox Err.Description
  Resume MyOtherPlace
End If
End Sub
flavo



On Error Goto MyErrorRoutine

with MyTable
    .Addnew
    !Value = "My Value"
    !Value2 = "My Other Value"
    .Update    'only in this step of the code, abandon the update if the record has already been added
End With

MyExitRoutine:
'// Add any clean up
Exit Sub

MyErrorRoutine:
If Err.number = 3022 then
   Resume Next
else
  '// Standard Error message / log / what ever
 
  goto MyExitRoutine:
end if

Dave
flavo

Woops.. I didn't read the Q.  Paddy is on target.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
mbizup

Hi Rob4077,
At the top of your code:

On Error Goto EH

In your error handler:


EH:
If Err.Number = 3022 then Resume Next
'Code for error handling in other cases follows

mbizup

Wow!  I don't know how I missed all those posts.  sorry all
Rob4077

ASKER
Hi all,

I was hoping there was some way of trapping before going to the error routine thereby avoiding multiple error routines in one function but your comments make me realise there isn't. Thanks for your help.

Rob
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.