|< Day Day Up >|
No matter how thorough you are with error handling, there will probably still be mistakes lurking somewhere in your code. Fortunately, the VBE offers a few tools for helping you debug (find) these mistakes so you can resolve them.
Using Run and Break Mode
After writing a procedure, you need to test it before actually setting it free upon your application, and you do this testing in the VBE. Your first step is to simply run the procedure and see what happens within the controlled VBE environment.
There are a number of ways to execute a procedure, but the quickest and easiest is to position the cursor in the procedure and press F5 or choose Run, Run Sub/UserForm. If it works, great.
If a runtime error occurs, and the problem isn't immediately apparent, you have a bit more work ahead. To expose the clues you need to find the problem, you need to temporarily suspend the procedure within the controls of the development environment (the VBE). This temporary suspension is called break mode. You can switch from run mode to break mode by doing any of the following:
After execution is suspended, you have the opportunity to modify the code. Afterward, you can test your modifications by pressing F5 again. Sometimes, you'll want to continue without restarting. To do so, choose Run, Reset project or click the Reset toolbar button.
Watch expressions are an advanced developer's topic that aren't covered in this book. For details, search the VBA help file for "watch expression."
Often, the only way to find a problem is to sift through the code, line by line. When this is the case, press F8 or choose Debug, Step Into to highlight the first line of your procedure, as shown in Figure 4.8. Press F8 to advance to the next statement. You can continue pressing F8 to execute the procedure one statement at a time.
Figure 4.8. The arrow and highlighting indicate the next statement that will be executed in break mode.
At any time during this single-step process, you can get information about a variable by hovering the mouse pointer over the reference right in the code. The VBE will display the reference's current value. You can review code at any point in the process, including review references that have been resolved and those that you've yet to reach.
There are other steps you can take while single-stepping through your code:
Setting a breakpoint is another good way to control where run mode stops. Position the cursor in the statement where you want execution to stop. Then, choose Debug, Toggle Breakpoint or press F9 to set a breakpoint. Doing so places a marker in the margin of the module, as shown in Figure 4.9.
Figure 4.9. Set a breakpoint to better control execution.
Run the procedure as you normally would. You'll find that execution stops at the breakpoint. You can then single-step from there or use any of the other available debugging tools to determine what's going wrong.
To remove a breakpoint, place your cursor on the line of code with the breakpoint and press F9 again. Or, you can just click in the margin of the module to set or remove a breakpoint.
|< Day Day Up >|