|
-
Dec 2nd, 2002, 11:58 PM
#1
Thread Starter
Frenzied Member
Question using On Error...
I want to know if I can do this because it does not seem to work right.
I start out with using an On Error Goto ER1 and then after that part I want to do an On Error Resume Next in the same sub. But the resume next part does not seem to take affect. Like this:
On Error GoTo ER1
<some code goes here...>
ER1:
<more code here>
On Error Resume Next
<and more code here...>
But the On Error Resume Next does not work it seems. I can't change the On Error in the same sub?
Thanks!
-
Dec 3rd, 2002, 12:14 AM
#2
Fanatic Member
You need to turn off error trapping before you can change it.
Insert a
on the line before the On Error Resume Next to reset the on error routine
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
-
Dec 3rd, 2002, 03:38 AM
#3
Well ...
Originally posted by WarrenW
I want to know if I can do this because it does not seem to work right.
I start out with using an On Error Goto ER1 and then after that part I want to do an On Error Resume Next in the same sub. But the resume next part does not seem to take affect. Like this:
On Error GoTo ER1
<some code goes here...>
ER1:
<more code here>
On Error Resume Next
<and more code here...>
But the On Error Resume Next does not work it seems. I can't change the On Error in the same sub?
Thanks!
Just remove the On Error words from the statement and simply use "Resume Next"
For e.g.
VB Code:
Private Sub DisplayNumbers()
On Error Goto ER1
'Some code1
'Some code2
'Some code3
'Error Occured
'Some code4
'Some code5
ER1:
If Err.Number <> 0 Then
MsgBox "An error occured"
Resume Next
End If
End Sub
In this case, after the error occurs, the error handler will show a message box showing "An error occured", and then will resume execution from Some Code4.
A few other things to note: I have found it better to put the error handler at the top of the procedure instead of at the bottom. This works great with add-ins that have a facility of inserting procedure headers. You can simply create a header with the error handler code, and then tell the add-in to insert proc headers in all procs, which gives you error handlers in all procedures!
The second, and more significant advantage is that if you have an error handler at the bottom, you will need an Exit Function/Exit Sub as the last statement before the error handler code. Alternatively you will have to check if the Err.Number is non-zero. In the absence of any of these steps, the code execution will continue right through your procedure code into the error handler. The result is the error handling code will execute always. Since for normal execution the error number is assumed to be zero, you will end up getting the "An error occured" messagebox all the time. Therefore you should either use Exit Function/Exit Sub before the error handler code, or check the error number inside the error handler code.
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|