Debug your Visual Basic code

Microsoft Office Access 2003

When you suspend execution of Microsoft Visual Basic code, the code is still running but is paused between running statements. While your code is suspended, you can do debugging tasks such as checking current variable values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint.

  1. In the Visual Basic Editor, move the insertion point to a line of code that isn't already a breakpoint or declaration.
  2. Click Toggle Breakpoint Button image on the Debug toolbar.
  3. Do one of the following:

    To clear a breakpoint, move the insertion point to the line of code on which the breakpoint has been set, and then click Toggle Breakpoint Button image on the Debug toolbar.

    To resume running the code, click Run Sub/UserForm on the Run menu.

Note  You can also suspend code by adding a Stop statement to a procedure or by pressing CTRL+BREAK while the code is running.

ShowStep through Visual Basic code

Stepping through your Visual Basic code can help you identify where an error is occurring. You can see whether each line of code produces the results you expect.

  1. Suspend execution of the code.

    ShowHow?

    When you suspend execution of Microsoft Visual Basic code, the code is still running but is paused between running statements. While your code is suspended, you can do debugging tasks such as checking current variable values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint.

    1. In the Visual Basic Editor, move the insertion point to a line of code that isn't already a breakpoint or declaration.
    2. Click Toggle Breakpoint Button image on the Debug toolbar.
    3. Do one of the following:

      To clear a breakpoint, move the insertion point to the line of code on which the breakpoint has been set, and then click Toggle Breakpoint Button image on the Debug toolbar.

      To resume running the code, click Run Sub/UserForm on the Run menu.

    Note  You can also suspend code by adding a Stop statement to a procedure or by pressing CTRL+BREAK while the code is running.

    Microsoft Access displays the line of code where execution is suspended.

  2. Do one of the following:

    To step through each line of code and into the code in a procedure called by another procedure, click Step Into Button image on the Debug toolbar.

    To step through each line of code but run as a unit any procedure that is called, click Step Over Button image on the Debug toolbar.

    To run the code that precedes the current line of code, and then break so you can step through each line of code, click Run To Cursor on the Debug menu.

    To run the rest of the current procedure, and then return to the next line of code in the preceding procedure in the call tree, click Step Out Button image on the toolbar.

Note  You can use Step Into Button image without suspending code execution. In the Visual Basic Editor Code window, place your cursor in the procedure you want to step through, and click Step Into Button image on the toolbar.

You can alternate between these types of stepping. The type of stepping you do depends on which portions of code you want to analyze.

ShowPerform a quick watch while debugging Visual Basic code

  1. Suspend execution of your Visual Basic code.

    ShowHow?

    When you suspend execution of Microsoft Visual Basic code, the code is still running but is paused between running statements. While your code is suspended, you can do debugging tasks such as checking current variable values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint.

    1. In the Visual Basic Editor, move the insertion point to a line of code that isn't already a breakpoint or declaration.
    2. Click Toggle Breakpoint Button image on the Debug toolbar.
    3. Do one of the following:

      To clear a breakpoint, move the insertion point to the line of code on which the breakpoint has been set, and then click Toggle Breakpoint Button image on the Debug toolbar.

      To resume running the code, click Run Sub/UserForm on the Run menu.

    Note  You can also suspend code by adding a Stop statement to a procedure or by pressing CTRL+BREAK while the code is running.

    Microsoft Access displays the line of code where execution is suspended.

  2. Select the expression whose value you want to see.
  3. Click Quick Watch Button image on the Debug toolbar.

Microsoft Access displays the Quick Watch dialog box, where you can see the expression and the current value of the expression. Click Add in the dialog box to add the expression to the list of watch expressions in the Watch window of the Microsoft Visual Basic Editor.

ShowTrace Visual Basic procedure calls while debugging Visual Basic code

When you suspend execution of Visual Basic code while debugging the code, you can use the Calls dialog box to view a list of procedures that have been started but not completed.

  1. Suspend execution of your Visual Basic code.

    ShowHow?

    When you suspend execution of Microsoft Visual Basic code, the code is still running but is paused between running statements. While your code is suspended, you can do debugging tasks such as checking current variable values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint.

    1. In the Visual Basic Editor, move the insertion point to a line of code that isn't already a breakpoint or declaration.
    2. Click Toggle Breakpoint Button image on the Debug toolbar.
    3. Do one of the following:

      To clear a breakpoint, move the insertion point to the line of code on which the breakpoint has been set, and then click Toggle Breakpoint Button image on the Debug toolbar.

      To resume running the code, click Run Sub/UserForm on the Run menu.

    Note  You can also suspend code by adding a Stop statement to a procedure or by pressing CTRL+BREAK while the code is running.

    Microsoft Access displays the line of code where execution is suspended.

  2. Click Call Stack Button image on the Debug toolbar.

Microsoft Access displays the most recently called procedure at the top of the list, followed by the next most recent procedure call, and so on. To display the statement that calls the next procedure in the list, click Show.

ShowAdd, modify, or delete a watch expression in the Watch window of the Microsoft Visual Basic Editor

ShowAdd a watch expression

  1. Open a procedure in the Code window.
  2. On the Debug menu, click Add Watch.
  3. If you already selected an expression in the Code window, it is automatically displayed in the dialog box. If no expression is displayed, type the expression you want to evaluate. The expression can be a variable, a property, a function call, or any other valid expression.

    Instead of typing the expression, you can also select an expression in the Code window and drag it to the Watch window.

  4. To select the range for which the expression will be evaluated, select a module and procedure context under Context.

    Note  Select the narrowest scope that fits your needs. Selecting all procedures or all modules could slow down code execution.

  5. To define how the system responds to the watch expression, select an option under Watch Type.

    To display the value of the watch expression, click Watch Expression.

    To pause execution if the expression evaluates to True, click Break When Value Is True.

    To pause execution if the value of the expression changes, click Break When Value Changes.

When you run the code, the Watch window shows the value of the expression you set.

ShowModify a watch expression

  1. Select the expression in the Watch window.

  2. Click Edit Watch on the Debug menu.

ShowDelete a watch expression

  1. On the Debug menu, click Edit Watch.
  2. Click Delete in the Edit Watch dialog box.

ShowDisable syntax checking

Microsoft Visual Basic automatically checks the code you type in the Code window of the Microsoft Visual Basic Editor for syntax errors as you type it. You can disable this feature.

  1. On the Tools menu, click Options.
  2. Click the Editor tab.
  3. Under Code Settings, clear the Auto Syntax Check check box.

ShowIgnore error handling

When you add an On Error statement to a Visual Basic procedure, Visual Basic automatically branches to an error-handling routine when an error occurs. You may want to ignore a procedure's error-handling code in some circumstances— for example, while you're debugging the procedure.

  1. On the Tools menu, click Options.
  2. Click the General tab.
  3. Under Error Trapping, select Break On All Errors.

    When you check this option, you'll automatically enter Break mode when an error occurs, regardless of error handling.