We now have examined the various ways procedures can be declared and called. You saw how to use optional parameters, as well as parameters that can be called
Finally, you used a procedure that incorporated objects from the ADOX library to create a table.
We are now going to
While we
In this chapter we examine what happens when you encounter an error. In the process, we will discuss the types of errors that can occur, the tools you have available to fix them, how to step through an error, and how to write code to handle errors.
If you have tried code examples from this book, you have probably run into something like this:
What is going on is that the VBA Editor is trying to help you out a bit.
As you leave a line, the editor looks at the code and
This is a very simple example of a
design-time error
. Examples of design-time errors are
undefined
The more difficult type of error to solve is the runtime error.
Once you have written a program well, nothing ever goes wrong. True or false? I don’t think it would come as a surprise to
Some errors occur during the design stage, which, as you have just seen, VBA catches as soon as you leave the erroneous line of code (provided it is a syntax error and not an error in logic). Some errors occur when the code actually runs. This type of error—a runtime error—is often an error in logic. For instance, the code may cause an array to go past the upper boundary setting (“out of bounds” error). You have no way of knowing that this is happening until the code runs.
What we are going to be talking about here is known in programming parlance as exceptions . An exception is something that causes an interruption to the normal flow of the program. We use the word “exception” because this problem is usually something that will occur infrequently.
As a programmer, you must anticipate the potential problems and provide code to help the program resolve the problem gracefully. Again, in programming parlance, we call this
exception handling.
Since VBA does not distinguish between errors and exceptions, I will use the
Let’s begin with a simple coding example where we will intentionally cause an error to happen:
Sub errorTest() Dim intNumerator As Integer Dim intDenominator As Integer Dim intResult As Integer intNumerator = InputBox("Please enter a numerator", "Numerator") intDenominator = InputBox("
Please
enter a denominator",_ "Denominator") intResult = intNumerator / intDenominator MsgBox "The result is " & intResult End Sub
As you can see, this is a pretty simple bit of coding that asks the
You can go ahead and give it a try. But I want to break a commandment of mathematics. When you start the code, you should see the first input box. Let’s enter the number 11 for the numerator:
After selecting OK, you are presented with the denominator input box:
Notice that 0 is entered as the denominator. Division by zero is a mathematical
Notice that this gives you an error number (11) and an error description (“Division by zero”). You also have the option of ending the program or, by clicking the Debug button, going to what VBA “thinks” is the offending piece of code. (I don’t mean to make the last
Do you really want your end user to see this error box? Wouldn’t you like a more
Let’s make some modifications to the code as indicated by the gray shading shown here:
Sub errorTest() Dim intNumerator As Integer Dim intDenominator As Integer Dim intResult As Integer On Error GoTo mytrap intNumerator = InputBox("Please enter a numerator", "Numerator") intDenominator = InputBox("Please enter a denominator", "Denominator") intResult = intNumerator / intDenominator MsgBox ("The result is " & intResult) Exit Sub mytrap: MsgBox "You cannot divide by zero" End Sub
Before we examine the code, let’s once again run it and force the “divide by zero” message. This time, instead of getting the error box shown earlier, you should see this new message box.
Defining what has
myTrap: MsgBox "You cannot divide by zero" End Sub
Notice that this line is identified with the label myTrap ending in a
In programming parlance, this whole process is called
error handling
, and in this case, myTrap is an
error handler
. It will only run when an error is
We have one other problem that we
One other small problem needs to be solved. We have error handling and an error handler, but when we dismiss the error message, the program ends. Wouldn’t it be far better to have a way to return the user to the input box to reenter the denominator?
Let’s add a couple of lines to our code as
Sub errorTest() Dim intNumerator As Integer Dim intDenominator As Integer Dim intResult As Double On Error GoTo mytrap intNumerator = InputBox("Please enter a numerator", "Numerator")
enterDenominator:
intDenominator = InputBox("Please enter a denominator", _ "Denominator") intResult = intNumerator / intDenominator MsgBox ("The result is " & intResult) Exit Sub mytrap: MsgBox ("You cannot divide by zero")
Resume enterDenominator
End Sub
Notice that in this case, we used the command Resume inside the error-handler code. Normally, Resume will return the user to the line where the error occurred. This is handy if you have some code inside the error handler that might fix the problem. However, in our case, it would return the user to the calculation. We need to go back an additional line to the point where the denominator is entered. In order to solve that, we put a line label just before the denominator entry point and then, in the error handler, instructed Resume to go back to that line label.
All of this has created a tidy little solution that is