MS-Access / Getting Started

Debugging

In this section, the following VBE debugging facilities:

  • Breakpoints
  • Immediate window
  • Locals window
  • Watch window

What Are Software Bugs?

Debugging can be one of the most challenging processes in software development, and unfortunately it's sometimes very costly. In a nutshell, debugging is the process by which programmers identify, find, and correct software errors.

There are three common types of bugs in software. Syntax errors are the most common form of software bugs. They are caused by misspellings in the program code and are most commonly recognized by the language's compiler. Syntax errors are generally easy to fix.

The next type of bug is called a runtime error. Runtime errors occur once the program is running and an illegal operation occurs. These errors generally occur because the programmer has not thought ahead of time to capture them (for example, File Not Found, Disk Not Ready, or Division by Zero). Runtime errors are most often easy to find and sometimes easy to fix.

The last common type of bug, and the most difficult to identify and fix, is known as the logic error. Logic errors are not easily identified, because they don't necessarily generate an error message. Logic errors are the result of erroneous logic implemented in the program code. Examples of logic errors include invalid mathematical calculations, the wrong variable used in an operation, or calling (executing) the wrong procedure.

Stepping Through Code

By now you should be fairly comfortable with the design-time and runtime environments. Moreover, you may have discovered the break mode environment. As a refresher, the next bulleted list reviews each type of Access VBA environment.

  • Design time is the mode by which you add controls to containers (such as forms) and write code to respond to events.
  • The runtime environment allows you to see your program running the same way a user would. During runtime you can see all your Visual Basic code, but you cannot modify it.
  • Break mode allows you to pause execution of your Visual Basic program (during runtime) to view, edit, and debug your program code.

The VBE allows you to step through your program code one line at a time. Known as stepping or stepping into, this process allows you to graphically see what line of code is currently executing as well as values of current variables in scope. Using function keys or menu items, you can navigate through program code with ease. For example, once in break mode, you can press the F8 key to skip to the next line.

During break mode, it is also possible to step over a procedure without having to graphically execute the procedure's statements one at a time. Known as procedure stepping or stepping over, this process can be accomplished during break mode by pressing Shift + F8 simultaneously.

Sometimes you might want to skip ahead in program code to a predetermined procedure or statement. The VBE provides this functionality through the use of breakpoints.

Breakpoints

Breakpoints can be inserted into your Visual Basic procedures during design time or break mode.

To create a breakpoint, simply click in the left margin of the Code window where you want program execution to pause. When your program's execution reaches the statement where a breakpoint has been placed, program execution pauses. To continue execution to the next breakpoint, simply press F5. To continue program execution one statement at a time, with or without a breakpoint, press the F8 key.

Tip: Breakpoints cannot be placed on empty lines in the Code window or on variable declarations.

There are occasions when you want to go back in time and re-execute a particular program statement without having to halt the entire program and re-run it. Believe it or not, the VBE provides a facility for traveling back in time while in break mode. To do so, simply click the yellow arrow in the left margin of the Code window and drag it to a previous program statement.

Immediate Window

During testing or debugging, it is not always desirable to change the values of variables and properties by modifying program code. A safer way of testing program code is through the use of the Immediate window. The Immediate window can be used during design time or break mode. Most popular in break mode, the Immediate window can be accessed by pressing Ctrl + G or through the View menu.

The Immediate window allows you to verify and change the values of properties or variables.

Tip: You can re-execute a statement in the Immediate window by moving the cursor to the statement's line and pressing Enter.

Locals Window

The Locals window, a friendly companion to any VBA programmer, provides valuable information about variables and control properties in current scope. Accessed from the View menu group, the Locals window not only supplies information on variables and properties, but also allows for the changing of control property values.

To change a property or variable's value using the Locals window, simply click the item in the Value column and type a new variable or property value.

Watch Window

In addition to breakpoints, the Watch window can aid you in troubleshooting or debugging program code. Accessed from the View menu item, the Watch window can track values of expressions and break when expressions are True or have been changed. In a nutshell, the Watch window keeps track of Watch expressions.

A basic Watch expression allows you to graphically track the value of an expression throughout the life of a program. Moreover, you can create a Watch expression that pauses program execution when an expression has been changed or is True.

For example, let's say you know a bug occurs in your program because the value of a variable is being set incorrectly. You know the value of the variable is changing, but you do not know where in the code it is being changed. Using a Watch expression, you can create an expression that pauses program execution whenever the value of the variable in question changes.

Though Watch expressions can be created from within the Watch window, it is much easier to create them by right-clicking a variable or property name in the Code window and choosing Add Watch.

The Add Watch dialog box provides many options for creating Watch expressions. Essentially, creating a Watch expression with the Add Watch dialog box is broken into three parts: Expression, Context, and Watch Type.

An expression is a variable, property, function call, calculation, or combination of valid expressions. By default, the expression's value is the name of the variable or property you're trying to watch. The context is the scope of the variable or property being watched. There are three values displayed:

  • Procedure. Defines the procedure where the expression is evaluated.
  • Module. Defines the module where the variable or property resides.
  • Project. Displays the name of the current project.

The Watch Type determines how Visual Basic responds to the expression:

  • Watch Expression. Displays the Watch expression and expression value in the Watch window.
  • Break When Value Is True. Visual Basic breaks program execution when the value of the Watch expression is True.
  • Break When Value Changes. Visual Basic breaks program execution when the value of the expression changes.
[Contents] [Next]