This isn't any substantial piece of code, but it points out a potential problem I see experienced programmers making on these forums. Also, I must give Bonnie West some credit for pointing this out to me and forcing me to develop a clear understanding of it all.
Let me start by outlining how the three On Error... statements work, which isn't well documented in the MSDN.
On Error Resume Next - Always clears the ERR object upon execution, but leaves results in the ERR object even after end/exit from a procedure call.
On Error Goto LineLabel - Always clears the ERR object upon execution, and the ERR object is also cleared upon end/exit from a procedure call, regardless of whether Resume, Resume LineLabel, or Resume Next is used.
On Error Goto 0 - Always clears the ERR object upon execution.
It's the difference between On Error Resume Next and On Error Goto LineLabel that is often unappreciated. To illustrate, I've set up the following example. Just paste it into Form1's code and execute:
In case you don't want to execute it, the message box reports "True", which is misleading. The SomeTestWithOern_TrueIfError didn't technically have any error. That's the point I'm trying to make.
And now, this can be fixed with the addition of a single line, an "On Error Goto 0" at the end of AnotherTestWithOren_TrueIfErro, as follows:
This time, the message box reports "False"!!! In fact, I put the "On Error Goto 0" in both test functions, just as good programming practice. Also, just to make sure I always "turn off" my "On Error Resume Next" statements, I've adopted the convention of indenting between them.
Regards,
Elroy
EDIT1: Just as an FYI, even my recommendation isn't a perfect fix because the ERR object is truly a global object. Clearing ERR anywhere clears it everywhere.
Let me start by outlining how the three On Error... statements work, which isn't well documented in the MSDN.
On Error Resume Next - Always clears the ERR object upon execution, but leaves results in the ERR object even after end/exit from a procedure call.
On Error Goto LineLabel - Always clears the ERR object upon execution, and the ERR object is also cleared upon end/exit from a procedure call, regardless of whether Resume, Resume LineLabel, or Resume Next is used.
On Error Goto 0 - Always clears the ERR object upon execution.
It's the difference between On Error Resume Next and On Error Goto LineLabel that is often unappreciated. To illustrate, I've set up the following example. Just paste it into Form1's code and execute:
Code:
Option Explicit
Private Sub Form_Load()
MsgBox SomeTestWithOern_TrueIfError
Unload Me
End Sub
Private Function SomeTestWithOern_TrueIfError() As Boolean
Dim i As Long
Dim b As Boolean
'
On Error Resume Next
i = 1 / 1 ' Does NOT cause error.
'
' Just some other function maybe used herein.
' In this example, nothing is done with the return, but it could be.
b = AnotherTestWithOren_TrueIfError
'
' And now we return, thinking that we've only tested our i = 1/1 line for an error.
SomeTestWithOern_TrueIfError = Err.Number <> 0
End Function
Private Function AnotherTestWithOren_TrueIfError() As Boolean
Dim i As Long
'
On Error Resume Next
i = 1 / 0 ' Causes error.
AnotherTestWithOren_TrueIfError = Err.Number <> 0
End Function
And now, this can be fixed with the addition of a single line, an "On Error Goto 0" at the end of AnotherTestWithOren_TrueIfErro, as follows:
Code:
Option Explicit
Private Sub Form_Load()
MsgBox SomeTestWithOern_TrueIfError
Unload Me
End Sub
Private Function SomeTestWithOern_TrueIfError() As Boolean
Dim i As Long
Dim b As Boolean
'
On Error Resume Next
i = 1 / 1 ' Does NOT cause error.
'
' Just some other function maybe used herein.
' In this example, nothing is done with the return, but it could be.
b = AnotherTestWithOren_TrueIfError
'
' And now we return, thinking that we've only tested our i = 1/1 line for an error.
SomeTestWithOern_TrueIfError = Err.Number <> 0
On Error GoTo 0
End Function
Private Function AnotherTestWithOren_TrueIfError() As Boolean
Dim i As Long
'
On Error Resume Next
i = 1 / 0 ' Causes error.
AnotherTestWithOren_TrueIfError = Err.Number <> 0
On Error GoTo 0
End Function
Regards,
Elroy
EDIT1: Just as an FYI, even my recommendation isn't a perfect fix because the ERR object is truly a global object. Clearing ERR anywhere clears it everywhere.