Link to home
Start Free TrialLog in
Avatar of velcrow
velcrow

asked on

vba code breaks on second DAO error within the same routine despite error handling

I'm looping through a dao Recordset and making updates to the records.  Some updates violate keys and/or data validation rules causing an error to occur at the Update method of the recordset.  The routine does have appropriate error handling and the first time a data validation error occurs it works great, but the second time the error handling gets completely ignored and the execution breaks and displays the error in the default VBA runtime error dialog.

Here is some sample code to duplicate the error.  Create a 'Table1' with one field 'uniqueText' and add three records to the table and then try to update the records with:

Sub test2()
    On Error GoTo errHandler
   
    Dim db As DAO.Database
    Dim rs As Recordset
    Set db = CurrentDb()
    Set rs = CurrentDb.OpenRecordset("Table1")
     
    Do While Not rs.EOF
        rs.Edit
        rs("uniqueText") = "test"
        rs.Update
skip:
        rs.MoveNext
    Loop
   
Exit Sub
errHandler:
    MsgBox (Err.Description)
    GoTo skip

End Sub
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

what error are you getting?
Avatar of velcrow
velcrow

ASKER

the error is the description of how the validation rule was violated
what validation?  be more specific..
Avatar of velcrow

ASKER

Any validation / index / key that is violated when the recordset updates, but that is irrelevant to the question.  The problem is not the error but the fact that it is not being handled by the error handler but rather causes execution to break.
so, what do you want to happen? the codes to continue running ?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America 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
or this one


Sub test2()
    On Error GoTo errHandler

    Dim db As DAO.Database
    Dim rs As Recordset
    Set db = CurrentDb()
    Set rs = CurrentDb.OpenRecordset("Table1")
     
    Do While Not rs.EOF
        rs.Edit
        rs("uniqueText") = "test"
        rs.Update

        rs.MoveNext
    Loop
errExit:
Exit Sub
errHandler:
    MsgBox (Err.Description)
    Err.Clear
    Resume Next
 

End Sub
<The problem is not the error but the fact that it is not being handled by the error handler but rather causes execution to break.>

Like capricorn1, I am not following the logic of your error handler...

I would use capricon1's code syntax and add this as the error handler:

errExit:
    Exit Sub
errHandler:
    If Err.Number = 3022 Then
        MsgBox "No Dupes allowed"   'Or whatever you want to do if this error is encountered...
    Else
        MsgBox (Err.Description)
    End If
    Err.Clear
    Resume errExit

You can split the points if the combination of both of these suggestions solves your issue

JeffCoachman
Avatar of velcrow

ASKER

capricorn1 and boag2000,

I did not mean to distract you with ‘how to handle the error’, as that was not the problem, and the sample code was only something I quickly threw together so somebody could duplicate the problem.  

The problem was that after the first DAO error, any subsequent DAO errors would not go to the error handling code as specified by ’On Error GoTo errHandler’ but instead would cause and unhandled error and halt execution of the code with a visual basic runtime error dialog.

As it turns out, the solution was very simple and I don’t know how it escaped me for so long.

capricorn1,
your code gave me the solution which was to simply replace ‘goto’ with ‘resume’ in the error handler.   I don’t know why, but apparently ‘goto’ does not have the same effect as ‘resume’ when handling DAO errors.  I guess this wasn’t a 500 point question after all, but I’ll award 500 points to you.  Thanks!


As a follow up question, do either of you know how to make access ignore validation errors thrown by the ‘Before Change’ data macro when running an append query and append all the records anyway, except those that throw an error?

For example, under normal circumstances when running an append query that creates duplicate records based only a primary key, access warns you of the records that will not be appended but will still append all those that it can.  I would like this same behavior even when validation is done through the ‘Before Change’ data macro, but instead it’s all or nothing; if one record violates the ‘Before Change’ event, then none of the records get appended.