ACC: Tips for Debugging Visual Basic for Applications Code

ID: Q142999

The information in this article applies to:

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article lists tips that can help you debug your Visual Basic for Applications code. This article assumes that your code compiles correctly, but does not perform as you expect.

Debugging is a process that you use to find and resolve errors, or bugs, in your Visual Basic for Applications code. There are three types of errors that you may encounter: compile-time errors, run-time errors, and logic errors. Logic errors occur when the application doesn't perform as intended and produces incorrect results. Microsoft Access provides debugging tools that enable you to step through your code, one line at a time, to examine or monitor the values of expressions and variables, and to trace procedure calls.

The general procedure for debugging Visual Basic for Applications code is to break larger procedures into smaller sub-procedures to verify that the individual steps in each sub-procedure work correctly, and then to combine the sub-procedures one by one until the code works correctly.

MORE INFORMATION

There are several different methods that you can use to debug your Visual Basic for Applications code. The main ones are as follows.

Using the Debug Window

You can use the Immediate pane in the Debug Window to run individual lines of Visual Basic for Applications code or to check the values of variables. In the Debug window, you can test and debug Function and Sub procedures, check the value of a field, a control, a property setting, a variable, or an expression; and display the result of an expression when the code is running. For an example of how to use the Debug window, follow thesesteps:

1. Open the module that contains your function in Design view.

2. On the View menu, click Debug Window.

3. Run your function in the Immediate pane of the Debug Window by typing

   the following line and pressing ENTER:

      ?<FunctionName>()

   where <FunctionName> is the name of your function. Be sure to place
   all arguments expected by your function inside the parentheses.

Use a Breakpoint to Suspend Execution of Code

When you suspend execution of Visual Basic for Applications code, the code is still running, but pauses between running statements. To make Visual Basic for Applications pause execution of your code, you can set a breakpoint. For example, if the first part of your code runs correctly, but other parts do not, follow these sample steps to find the part that does not run correctly:

1. To set a breakpoint right after the part of your code that you know

   works correctly, move the insertion point to the first line of code
   where you want to set a breakpoint.

2. Click Toggle Breakpoint on the Debug menu (or the Run menu in version
   7.0.). The line appears in bold type, indicating that there is a
   breakpoint set at that line.

3. Run the function in the Debug window. When code execution reaches
   the breakpoint, processing is suspended and the breakpoint line is
   selected. You can then check the value of variables in the code.

   For example, to check the value of a variable called MyName, type the
   following in the Debug window, and then press ENTER:

      ?MyName

    The value of MyName appears in the Debug window. If the value is
    correct, check other variables. If the value is incorrect, check the
    previous lines of code to see where this variable is assigned an
    incorrect value.

Using the Debug.Print Statement

You can view the results of an expression in the Debug window by entering the Print method of the Debug object, followed by the expression. You can display the Debug window anywhere in Microsoft Access 7.0 or 97 by pressing CTRL+G. You can use a question mark (?) as shorthand for the Print method in the Debug window.

Good places to position Debug.Print statements include the following:

Set a Watch Expression in Visual Basic for Applications Code

A watch expression is an expression that you monitor in the Debug window. You can observe the values of the watch expressions that you select in the Watch pane. You can also perform an Instant Watch to see the value of an expression that hasn't been specified as a Watch expression. To add a Watch expression to the Watch pane, follow these steps:

1. Open the Debug window by pressing CTRL+G.

2. On the Debug menu (or the Tools menu in version 7.0), click Add Watch.

3. In the Expression box, type a variable, property, function call, or

   other valid expression.

4. In the Context box, set the scope of the expression that you are
   watching by selecting the Procedure name and the Module name.

5. In the Watch Type box, click the option to determine how you want to
   evaluate the expression.

In Microsoft Access 97, you can change the value of a variable on which you set a watch. You can also restrict the scope used to watch variables defined for a specific procedure or a specific module, or globally in the code.

When the execution of your code is suspended, you can click Quick Watch on the Debug menu in Microsoft Access 97 to check the value of an expression that hasn't been defined as a Watch expression. You can also select an expression in your code and click the Quick Watch for it. (In Microsoft Access 7.0, you can use the Instant Watch command. You can also click the Instant Watch button on the Visual Basic toolbar to create a Watch expression from text that you've selected in the Module window.)

Using the Calls Dialog Box to Trace Nested Procedures

The Calls dialog box displays a list of all active procedure calls. These calls are the procedures in an application that are started but not completed. You can use the Calls dialog box to trace the operation of an application as it runs a series of procedures. You can view the Calls from the Debug window by clicking the Build button.

The Calls dialog box lists all the active procedure calls in a series of nested procedure calls. It places the earliest active procedure call at the bottom of the list and adds subsequent procedure calls to the top.

You can use the Show button in the Calls dialog box to display the statement that calls the next procedure listed in the Calls dialog box. If you choose the current procedure in the Calls dialog box and then click Show, Visual Basic for Applications displays the current statement at which execution was suspended.

Using the Locals Pane

The Locals pane in the Debug window has three columns: Expression, Value, and Type (or Context in version 7.0). The Expression column begins with the current module (for a standard module), or the current instance of a class (for a class module). The expression column is organized as a hierarchical tree of information starting from the current module to display all of the module-level variables in the current module. The Value column shows the values of the current module objects. You can change the value of a module-level variable in the Debug window in order to test the behavior of your module. The Type column shows the type of the current module-level object.

Inspecting a selected variable's value in the Locals pane can be very helpful in debugging your module, as can changing a variable's value in the Locals pane Value column to observe what effect it has on other parts of your module.

REFERENCES

For more information about the Debug window, search the Help Index for "Debug window."

For more information about debugging Visual Basic for Applications code, search the Help Index for "debugging code."

Additional query words:

Keywords          : kbprg PgmErr kbfaq
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbinfo

Last Reviewed: November 22, 1998