Implement VB.Net Style Error Trapping

As you may know, Microsoft is phasing out VBA and encouraging developers to start using VB.Net. Some VBA code can be imported in to VB.Net and run without any modifications, other code will require extensive modification. The On Error GoTo method of error trapping used in VBA is not supported in VB.Net. In VB.Net this has been replaced by Try-Catch-Finally blocks and looks something like this:

Try
'Processing code goes here
Catch ex as Exception
'Errors raised in the try block transfers control
'to the Catch block
Finally
'This code is always executed
End Try

You can implement this same type of style in your VBA code modules and ease the eventual transition to VB.Net by using this structure:

TRY1:
On Error GoTo CATCH1
'Processing code goes here
GoTo FINALLY1
CATCH1:
'Errors raised in the try block transfers control to the Catch block
FINALLY1:
'This code is always executed
'End Try1

TRY2:
On Error GoTo CATCH2
'Processing code goes here
GoTo FINALLY2
CATCH2:
'Errors raised in the try block transfers control to the Catch block
FINALLY2:
'This code is always executed
'End Try2

EnvisionCAD

Since 1996, EnvisionCAD has been a nationally recognized leader in the configuration, customization, implementation, training & support for CAD software solutions. Our individualized approach has benefited private engineering firms and government agencies alike. Basic or advanced, we can help you get the most from your CAD technology.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *