VBA Error Messages
By Richard Rost
12 hours ago
Show Real Error With Err.Description Does your error handler just say 'Error'? I'll show you how to use Err.Description to see what actually went wrong, and where to put Exit Sub so your handler doesn't run when the code succeeds. This clip is from Access Developer 15. Learn more at https://599cd.com/ACD15.
Related Video: https://youtu.be/tKygQcNH550
Keywords err.description, err description microsoft access, vba err description, access vba error messages, on error goto
More InformationTranscript Okay, here's how this works.
Let's say, let me get rid of this stuff here.
All right, let's say On Error GoTo MyError. All right, what is MyError? I got no idea. Well, come down here toward the bottom of the sub and type in MyError and then a colon. We just created our own GoTo point.
GoTo has been around forever. Way, way, way back in the old Color Computer Radio Shack BASIC, I used to use GoTo statements. And then, when your lines were numbered, you'd have, like, 20 GoTo 10 if you wanted a loop before they had cool loops like While, Do, and For loops and all that.
So what does this say? Well, basically, if an error is encountered, jump down here to this line right here. And what are we going to do? I'm going to message box "Error," just like that.
So let's hit the button. I want to set these windows up side by side real quick just so you can see, because I don't need this to be that big. I want to be able to see the code window at the same time.
All right, there we go. Perfect. I'm going to hit the button, and boom, error. All I get is "Error." It doesn't pop up the system-generated dialog box, but I get my own message box.
Hit OK, and then we're done. What happens is this DLookup statement hits. It generates that invalid use of Null error, jumps down to MyError, I get my message box error, and then it exits out because it continues on, and now we're at the end of the sub.
Now it would be nice to see what that error message is. So we'll go colon here and then add Err.Description. There's a couple different things you can put in here. We'll talk about these when I do my full debugging class. There's Err, there's HelpContext, there's all kinds of other stuff, but really, pretty much all you ever will use is Number and Description.
Number is that number that they pop up, like Error 424. Description is the one that actually tells you what the problem is.
Save that and run it now. Error: Invalid use of Null.
So let's say we deal with this one by putting an Nz around it. We deal with that error. Hit it now, boom, Error: Division by zero.
That's fine. Let's fix this one now. Let's put a 1 around it. Now let's try it. Save and then run.
Oh, okay, there's my X is 7. X is 7 divided by 1, so X came out as 7. That's fine. Hit OK, and then error, what? And then it's blank.
That's because no error was generated. So you have to make sure before this point here, you have to Exit Sub yourself. That's the end of the sub, in other words. You're saying, all right, everything worked fine. Exit Sub before we get to MyError handling. Intro Does your error handler just say 'Error'? I'll show you how to use Err.Description to see what actually went wrong, and where to put Exit Sub so your handler doesn't run when the code succeeds. This clip is from Access Developer 15. Learn more at https://599cd.com/ACD15.
Related Video: https://youtu.be/tKygQcNH550 Quiz Q1. What does "On Error GoTo MyError" do in a VBA procedure? A. It tells VBA to jump to the MyError label if an error occurs B. It automatically fixes all errors in the procedure C. It disables all error messages D. It restarts the procedure when an error occurs
Q2. How do you create the MyError destination for an error handler? A. Type "MyError:" on its own line in the procedure B. Create a new module named MyError C. Add MyError to the form properties D. Declare MyError as an Integer variable
Q3. What is a label such as "MyError:" used for in VBA? A. It marks a location that a GoTo statement can jump to B. It creates a new function C. It declares an error number D. It closes the current procedure
Q4. Which Err property provides the text explaining what went wrong? A. Err.Description B. Err.Text C. Err.Message D. Err.HelpFile
Q5. Which Err property commonly provides the numeric error code? A. Err.Number B. Err.Code C. Err.Value D. Err.ID
Q6. Why should you place "Exit Sub" before the error handler label when the procedure completes successfully? A. To prevent normal execution from continuing into the error handler code B. To clear all variables from memory C. To force VBA to display an error message D. To run the procedure a second time
Q7. What happens if execution reaches an error handler label even though no error occurred? A. The error handler code will still run B. VBA automatically generates a new error C. The procedure restarts from the beginning D. VBA skips directly to the next procedure
Q8. In the example, why was Nz used around a value that could be Null? A. To prevent an "Invalid use of Null" error B. To convert the value into a date C. To create an error handler label D. To prevent a division by zero error
Q9. In the example, what change prevented a division by zero error? A. Dividing by 1 instead of zero B. Replacing Err.Description with Err.Number C. Removing the On Error statement D. Renaming the error handler label
Answers: 1-A; 2-A; 3-A; 4-A; 5-A; 6-A; 7-A; 8-A; 9-A
DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks. Summary Today's video from Access Learning Zone explains how to create and use a basic VBA error handler in Microsoft Access.
When an error occurs in VBA, you can direct Access to jump to a specific location in your procedure rather than displaying the standard system error dialog box. To do this, I use an On Error GoTo statement followed by the name of an error-handling label.
The label can be any valid name you choose. Near the bottom of the Sub procedure, I create that label by typing its name followed by a colon. This gives VBA a destination to jump to whenever an error occurs.
The GoTo concept has existed since the earliest versions of BASIC. In older BASIC languages, programmers often used numbered lines and GoTo statements to control program flow. Modern VBA gives us better structured options such as If statements, For loops, Do loops, and While loops, but GoTo labels are still useful for centralized error handling.
Once VBA reaches an error, it immediately jumps to the error-handling label. At that point, I can display my own message instead of allowing Access to show its built-in error dialog. For example, I can use a message box that simply says "Error." This confirms that the error handler was triggered.
However, a generic error message is not very helpful. I usually want to know what actually caused the problem. The Err object contains information about the most recent error. Two of its most useful properties are Err.Number and Err.Description.
Err.Number provides the numeric error code, such as error 424. Err.Description provides the readable explanation of the problem, such as "Invalid use of Null" or "Division by zero."
For example, if a DLookup function returns Null and I try to use that value in a calculation, VBA can generate an "Invalid use of Null" error. I can correct that specific problem by using the Nz function to convert Null into a usable value.
After fixing the Null issue, another error may appear. For example, if the calculation attempts to divide by zero, VBA will generate a "Division by zero" error. I can resolve that problem by ensuring that the divisor has a valid nonzero value.
There is one important detail to remember when using error handlers. If the procedure completes successfully, VBA will continue executing the next line of code. Since the error-handling label is located near the bottom of the procedure, VBA can accidentally continue into the error handler even though no error occurred.
To prevent that, I must place an Exit Sub statement before the error-handling label. This tells VBA that the normal portion of the procedure has completed successfully and that it should leave the Sub without running the error-handling code.
A typical procedure therefore has three parts: the normal code, an Exit Sub statement, and the error-handling section at the bottom. If everything works, the procedure exits normally. If an error occurs, VBA jumps directly to the error handler, where I can display the error number, description, or any other useful information.
You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below. Live long and prosper, my friends. Topic List VBA On Error GoTo error handlers Creating a VBA error-handler label Displaying Err.Description in a message box Using Err.Number and Err.Description Preventing error handler fall-through with Exit Sub Article In VBA, you can create your own error-handling section so that a procedure responds to errors in a controlled way instead of showing the standard system error dialog box.
At the beginning of a procedure, you tell VBA that if an error occurs, it should jump to a named location elsewhere in that same procedure. This named location is called an error handler. You create it by placing a label near the bottom of the procedure. When VBA encounters an error, it stops normal execution and continues at that label.
The error handler can display a message, record the problem, clean up temporary objects, or take other appropriate action. For a simple example, it can show a message box informing the user that an error occurred. However, a generic message such as "Error" is usually not very helpful. It is better to include the built-in error description, which explains the specific problem VBA encountered.
VBA provides error information through the Err object. The two most commonly useful properties are the error number and the error description. The error number identifies the type of error, while the description provides readable text such as "Invalid use of Null" or "Division by zero." In most practical situations, displaying or logging the description is enough to identify what went wrong.
For example, if a lookup operation returns a Null value and your procedure tries to use that value in a calculation, VBA may generate an invalid-use-of-Null error. You can often prevent that specific error by handling Null values before performing the calculation. Similarly, if a calculation attempts to divide by zero, you can prevent that by checking the denominator or providing a safe fallback value.
Error handling is still useful even when you correct known problems. There may be unexpected issues caused by missing data, invalid user input, unavailable files, permissions, or other conditions you did not anticipate. A general error handler gives your procedure a controlled response when those problems occur.
One important detail is that the error handler label is still part of the same procedure. If the procedure completes normally and reaches that label, VBA will execute the error-handling code even though no error occurred. This can cause a blank or misleading error message.
To prevent that, place an explicit instruction before the error handler that ends the procedure when everything has completed successfully. In other words, normal processing should finish and exit before execution can reach the error-handling section. The error handler should run only when VBA jumps to it because an actual error occurred.
A well-structured procedure therefore follows a simple pattern. First, enable error handling. Next, perform the normal work. Then, explicitly exit when the work succeeds. Finally, place the error-handling section at the bottom, where it can display or record the error details and safely end the procedure. Primary Topics On Error GoTo, custom error-handler labels, GoTo behavior, Err.Description, Err.Number, Exit Sub placement, handling Null errors, division by zero Secondary Topics historical BASIC GoTo statements, DLookup
|