ACC: Tips for Debugging Access Basic Code

ID: Q108438


The information in this article applies to:


SUMMARY

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

This article contains tips to help you debug your Access Basic code. This article assumes that your code compiles correctly, but does not perform as you expect.


MORE INFORMATION

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

Using the Immediate Window

You can use the Immediate window to execute individual lines of Access Basic code, or to check the values of variables. Therefore, you can use the Immediate window to:
The following steps demonstrate how to use the Immediate window to debug your Access Basic functions:
  1. Open the module containing your function in Design view.


  2. From the View menu, choose Immediate Window.


  3. Run your function in the Immediate window by typing "?<FunctionName>()" (without the quotation marks) and then pressing ENTER. Substitute the name of your function for <FunctionName>, and be sure to place all arguments expected by your function inside the parentheses.


For more information on the Immediate window, search for "immediate window" then "Immediate Window" using the Microsoft Access Help menu.

Using Breakpoints with the Immediate Window

A breakpoint suspends execution of the code, allowing you to check the values of variables. You can use the Immediate window with breakpoints to pinpoint code problems quickly.

For example, if the first part of your code runs correctly, but other parts do not, you can follow the sample steps below to find the malfunctioning part:
  1. Set a breakpoint right after the part that you know works correctly. To set a breakpoint:

    1. Move your cursor to the line of code where you want to set the breakpoint.


    2. Click the Breakpoint button (the five-fingered hand) on the toolbar. Or, press F9, or choose Toggle Breakpoint from the Run menu. The line will appear in bold type, indicating that there is a breakpoint set at that line.




  2. Run the function in the Immediate window (see "Using The Immediate Window" above). 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 Immediate window and then press ENTER:

    ?MyName

    The value of MyName is displayed in the Immediate 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 use the Debug.Print statement in your code to display the values of your variables in the Immediate window. Good places to include Debug.Print statements include:

Stepping Through the Code

Stepping through your code means executing the code one line at a time. This is sometimes referred to as tracing. Access Basic has two ways of stepping through code: single stepping, and procedure stepping.

Single stepping executes each step of a called procedure, stopping at each line in the procedure. Procedure stepping executes a called procedure all at once, then returns to the next line in the current procedure.

To step through your code:
  1. Set a breakpoint in your code at the line where you want to begin stepping. (See "Using Breakpoints with the Immediate Window", above). To step through an entire function, set the breakpoint on the first line of the function.


  2. Start the function. When program execution reaches the breakpoint, the line with the breakpoint will be selected in the function's module.


  3. To single step, click the Single Step button (single foot) on the toolbar. Or, press F8, or choose Single Step from the Run menu. The selected line of code will be executed, and then the next line of code will be selected. You can continue through your code one line at at time in this fashion.


If your code calls other functions or procedures you will single step through them as well. If you do not want to single step through these other procedures, click the Procedure Step button (double foot) on the toolbar. Or, press SHIFT+F8, or choose Procedure Step from the Run menu.

Note that as you step through your code, you can check the values of variables in the Immediate window (see "Using the Immediate Window", above).

If you want to continue running your code normally (without single or procedure stepping), click the Run button (exclamation point) on the toolbar. Or, press F5, or choose Continue from the Run menu.


REFERENCES

Microsoft Access "Introduction to Programming," version 1.x, Chapter 4, "Debugging Your Access Basic Code"

Microsoft Access "Building Applications," version 2.0, Chapter 9, "Debugging"

Additional query words: kbtshoot trouble shoot


Keywords          : kbprg PgmErr 
Version           : 1.0 1.1 2.0
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: March 30, 1999