If I use the global Err object as a return value and call Exit Function will my return value always come up with no error?
I don't know a lot about the underlying implementation of VBA, but I'm assuming assignment statements are by reference and not by value. If it is by value then the above should work.
I want to use an ErrObject as a return value instead of a Boolean. So I can tell if the function worked, where it went wrong if it did (ErrObject.Source), and I can decide what to do with the error. I don't know exactly how the ErrObject.Raise function works but if that sets a new ErrObject without actually causing a break then that could work.
1) don't have any error handler in the function at all. When that function is called and an error is raised, it will bubble back to the caller. Example:
Code:
Private Function Oops()
Oops = 1/0
End Function
Private Sub TryIt()
On Error Goto catch
Debug.Print Oops
Exit Sub
catch:
Debug.Print Err.Number & " " & Err.Description
End Sub
Though the error actually occurs in Oops, it will be passed back to the Doit's error handling.
Alternatively for cases where you need local error handling and pass only certain errors back:
Code:
Private Function Oops()
On Error Goto catch
Oops = 1/0
Exit Function
catch:
Select Case Err.Number
Case 11
Err.Raise 11
Case Else
Oops = 0
End Select
End Function
Private Sub TryIt()
On Error Goto catch
Debug.Print Oops
Exit Sub
catch:
Debug.Print Err.Number & " " & Err.Description
End Sub
In this case, the local error handler catch the error and if it finds to be error 11, raise it again, thus passing it back to the calling function.
As a third option, some people do this:
Code:
On Error Resume Next
Oops = 1/0
If Not Err = 0 Then
Oops = 0
End If
On Error Goto 0
In that case, we just force the operation, and check for failure, and if there's any, regardless of cause for the failure, set the value accordingly and pass that value back.