Link to home
Start Free TrialLog in
Avatar of Rob4077
Rob4077Flag 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
ASKER CERTIFIED SOLUTION
Avatar of John Mc Hale
John Mc Hale
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


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
Woops.. I didn't read the Q.  Paddy is on target.
Avatar of 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

Wow!  I don't know how I missed all those posts.  sorry all
Avatar of 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