Calling Subroutine

It is possible to pass an error to its calling procedure.
If you want to pass an error back to the calling subroutine, you just need to remove the On Error statement at the top.

Public Sub ParentSub() 
    On Error GoTo ErrorHandler
    Call ChildSub()
    Exit Sub
ErrorHandler:
' handle the error here
End Sub

Public Sub ChildSub()
' if there is an error here, the error will be handled in ErrorHandler of ParentSub
End Sub

This handles the error in both places

Public Sub ParentSub() 
    On Error GoTo ErrorHandler
    Call ChildSub()
    Exit Sub
ErrorHandler:
' handle the error here
End Sub

Public Sub ChildSub()
    On Error GoTo ErrorHandler

    Exit Sub
ErrorHandler:
' handle the error here and pass it back to the ParentSub to handle it as well
    Err.Raise Err.Number
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext