Debugging Code

 < 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:

  • Inserting a breakpoint in your code.

  • Inserting a Stop statement where you want the code to stop.

  • Inserting a watch expression.

  • Pressing Ctrl+Break during execution (probably the least preferable method because you can't really control where the code stops).

  • Clicking the Debug button in the runtime error dialog box when it occurs.

  • Clicking the Break tool on the toolbar.

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.

NOTE

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."


Single-Stepping

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.

graphics/04fig08.jpg


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:

  • Press Shift+F8 (choose Debug, Step Over) to skip the next statement.

  • Press Ctrl+Shift+F8 (choose Debug, Step Out) to skip the procedure entirely. This isn't the same as resetting or breaking the process because Step Out returns control to a calling procedure (if applicable).

  • Press Ctrl+F8 (choose Debug, Run to Cursor) to execute the code from the current statement to the statement that contains the cursor. This command lets you process several statements automatically while controlling where execution stops.

Setting Breakpoints

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.

graphics/04fig09.jpg


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.

TIP

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 > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net