Rx_
Nothing In Moderation
- Local time
- Today, 05:30
- Joined
- Oct 22, 2009
- Messages
- 2,803
An example of the basic On Error Goto
Basically, give the error a name (handle) such as Err_Trap
Any name will do... OK Any Name?
In the old days, we had code testers, code reviews and such.
Now in the new Nike Age "just do it" we get an assignment and move on like a Gypsy. We write good code that works.
But, why not leave some Fortune Cookies for the next coder?
First a quick tutor about On Error - followed by the Fortune Cookie version
The On Error GoTo tells VBA to transfer execution to the line following the specified line label. Whenever an error occurs, code execution immediately goes to the line following the line label. None of the code between the error and the label is executed, including any loop control statements.
Fortune Cookie Version:
Private Sub DesignedToFail()
On Error GoTo Bar_Order_One_Bourbon_One_Scotch_and_One_Beer
Private Sub DesignedToFail()
On Error GoTo Wikipedia
Private Sub DesignedToFail()
On Error GoTo statement_is_frowned_upon_by_Ada_programmers
Private Sub DesignedToFail()
On Error GoTo Heaven ' Wait! Heaven is up and you NEVER NEVER use a GOTO to a upper line of code! Is this Proof nobody can goto Heaven?
Private Sub DesignedToFail()
On Error GoTo PointOfNoReturn
With that, the door is open for your submissions
Basically, give the error a name (handle) such as Err_Trap
Any name will do... OK Any Name?
In the old days, we had code testers, code reviews and such.
Now in the new Nike Age "just do it" we get an assignment and move on like a Gypsy. We write good code that works.
But, why not leave some Fortune Cookies for the next coder?
First a quick tutor about On Error - followed by the Fortune Cookie version
The On Error GoTo tells VBA to transfer execution to the line following the specified line label. Whenever an error occurs, code execution immediately goes to the line following the line label. None of the code between the error and the label is executed, including any loop control statements.
Code:
Private Sub DesignedToFail()
On Error GoTo Err_Trap
X =1
Y = 2
Z = 1+2 / 0
MsgBox "This will never display: " & Z, vbokonly, "My Existence is Futile"
Exit Sub
Err_Trap:
MsgBox "Your Code had an error " & Err.Description, vbokonly, "OK, Bonehead?"
End Sub
Fortune Cookie Version:
Private Sub DesignedToFail()
On Error GoTo Bar_Order_One_Bourbon_One_Scotch_and_One_Beer
Private Sub DesignedToFail()
On Error GoTo Wikipedia
Private Sub DesignedToFail()
On Error GoTo statement_is_frowned_upon_by_Ada_programmers
Private Sub DesignedToFail()
On Error GoTo Heaven ' Wait! Heaven is up and you NEVER NEVER use a GOTO to a upper line of code! Is this Proof nobody can goto Heaven?
Private Sub DesignedToFail()
On Error GoTo PointOfNoReturn
With that, the door is open for your submissions