Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error-trapping question 3

Status
Not open for further replies.

psemianonymous

Programmer
Dec 2, 2002
1,877
US
I posted this to comp.databases.ms-access a while ago and have yet to receive any useful replies. You can look at the thread:

on Google Groups: (updated every 9 hours only, otherwise perfect)

on dbforums.com: (missing two posts, uses popup windows, updated often)


Basically, when I set the error trapping to:

On Error GoTo Sub_Error

then later set the error trapping to

On Error Resume Next

instead of working, "On Error Resume Next" apparently *resets* error-trapping to the Access-default behavior. This is funky and consistently reproducible for me. The newsgroup thread has more details.

thanks,


Pete
 
I really don't know, but one thought:

You have alredy generated an error, which you do not clear before you jump to the exit part and state resume next.

You should, when using resume next, place it before any line that might generate an error you wan't to trap. In your example, I would have done the following:

on error goto Sub_Error
' bla bla bla coding
on error resume next
rs.FindFirst 'This Will Error
if err.number<>0 then ' an error occured, perhaps #91
err.clear
else
rs.close
set rs=nothing
endif
Sub_Exit:
Exit Sub
Sub_Error:
GoTo Sub_Exit
End Sub

Now, the Goto Sub_Exit, is a bit dangerous, one should rather use Resume Sub_Exit, because that also clears the error object.

Roy-Vidar
 
Hi foolio12,

This is working as advertised.

After you have had an error and entered your error handling code, the error handler for the procedure is active (and remains active until a Resume statement is executed). Procedures with active error handlers cannot handle errors; this prevents (possibly never-ending) recursion. As any one procedure can only have one enabled error handler, it follows that active error handlers can not, themselves, enable other error handlers within the same procedure.

Any error which occurs in an active error handler is passed back up the calling chain until a procedure is found with an enabled error handler which is NOT active, and it ALWAYS finds one, the ultimate error handler which stops the code, being built in. This is what happens on the second error in your code.

To terminate the error handling process and stop an error handler being active you must Resume mainline code, after which you must re-enable error handling (if you want to).

In your case, you need to change the code to

Code:
Public Sub CloseRecordset()
On Error GoTo Sub_Error
Dim rs As Recordset

    rs.FindFirst &quot;This Will Error&quot;

Sub_Exit:

On Error Resume Next
Code:
 ' Only works when NOT handling an earlier error
Code:
    rs.Close   '<----&quot;Object variable or with block not set&quot;
    Set rs = Nothing

Sub_Error:
Code:
    Resume
Code:
 Sub_Exit
Code:
 ' Finish handling error
Code:
End Sub

Enjoy,
Tony
 
foolio,
You need to change the line
GoTo Sub_Exit
to
Resume Sub_Exit

This is because the error handler is not reset when you go to the exit loop, so it can't handle the next error when it is reached.

I've just copied your exact code & made the change & it works fine.

hth

Ben



----------------------------------------------
Ben O'Hara

&quot;Where are all the stupid people from...
...And how'd they get so dumb?&quot;
NoFX-The Decline
----------------------------------------------
 
Thanks all. I never understood the difference between GoTo and Resume--now I do. Thanks.


Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top