Debug your Visual Basic code

ShowAdd a breakpoint to suspend execution of Microsoft Visual Basic code

When you suspend execution of Microsoft Visual Basic (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code, the code is still running but is paused between running statements (statement: A syntactically complete unit that expresses one specific kind of operation, declaration, or definition. A statement is usually on one line in a procedure or Declarations section, but two or more can be on a line separated by a colon (:).). While your code is suspended, you can do debugging (debugging: The process of finding and fixing errors in a Visual Basic procedure or a macro. Debugging usually involves executing specific portions of the procedure or macro and analyzing them at various points.) tasks such as checking current variable (variable: A named storage location capable of containing data that can be modified during program execution. Each variable has a name that uniquely identifies it within its level of scope. A data type can be specified or not.) values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint (breakpoint: A line of code in a Function or Sub procedure at which Microsoft Visual Basic automatically suspends execution.).

  1. In the Visual Basic Editor (Microsoft Visual Basic Editor: An environment in which you can edit macros that you've recorded and write new macros and Visual Basic for Applications programs.), 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 (Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) 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 (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code, the code is still running but is paused between running statements (statement: A syntactically complete unit that expresses one specific kind of operation, declaration, or definition. A statement is usually on one line in a procedure or Declarations section, but two or more can be on a line separated by a colon (:).). While your code is suspended, you can do debugging (debugging: The process of finding and fixing errors in a Visual Basic procedure or a macro. Debugging usually involves executing specific portions of the procedure or macro and analyzing them at various points.) tasks such as checking current variable (variable: A named storage location capable of containing data that can be modified during program execution. Each variable has a name that uniquely identifies it within its level of scope. A data type can be specified or not.) values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint (breakpoint: A line of code in a Function or Sub procedure at which Microsoft Visual Basic automatically suspends execution.).

  1. In the Visual Basic Editor (Microsoft Visual Basic Editor: An environment in which you can edit macros that you've recorded and write new macros and Visual Basic for Applications programs.), 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.

  1. 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 (call tree: All modules that might be called by any procedure in the module in which code is currently running.), 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 (Microsoft Visual Basic Editor: An environment in which you can edit macros that you've recorded and write new macros and Visual Basic for Applications programs.) Code window (Code window: A window in the Microsoft Visual Basic Editor in which you write, display, and edit Visual Basic code. You can open as many Code windows as you have modules.), 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 (Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code.

ShowHow?

When you suspend execution of Microsoft Visual Basic (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code, the code is still running but is paused between running statements (statement: A syntactically complete unit that expresses one specific kind of operation, declaration, or definition. A statement is usually on one line in a procedure or Declarations section, but two or more can be on a line separated by a colon (:).). While your code is suspended, you can do debugging (debugging: The process of finding and fixing errors in a Visual Basic procedure or a macro. Debugging usually involves executing specific portions of the procedure or macro and analyzing them at various points.) tasks such as checking current variable (variable: A named storage location capable of containing data that can be modified during program execution. Each variable has a name that uniquely identifies it within its level of scope. A data type can be specified or not.) values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint (breakpoint: A line of code in a Function or Sub procedure at which Microsoft Visual Basic automatically suspends execution.).

  1. In the Visual Basic Editor (Microsoft Visual Basic Editor: An environment in which you can edit macros that you've recorded and write new macros and Visual Basic for Applications programs.), 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.

  1. Select the expression (expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.) whose value you want to see.
  2. 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 (Watch window: A window in the Visual Basic Editor used for debugging Visual Basic procedures. You can specify the variables from the procedure you want to inspect, and the Watch window displays the value of the variable as the procedure runs.) of the Microsoft Visual Basic Editor (Microsoft Visual Basic Editor: An environment in which you can edit macros that you've recorded and write new macros and Visual Basic for Applications programs.).

ShowTrace Visual Basic procedure calls while debugging Visual Basic code

When you suspend execution of Visual Basic (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code while debugging (debugging: The process of finding and fixing errors in a Visual Basic procedure or a macro. Debugging usually involves executing specific portions of the procedure or macro and analyzing them at various points.) 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 (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) code, the code is still running but is paused between running statements (statement: A syntactically complete unit that expresses one specific kind of operation, declaration, or definition. A statement is usually on one line in a procedure or Declarations section, but two or more can be on a line separated by a colon (:).). While your code is suspended, you can do debugging (debugging: The process of finding and fixing errors in a Visual Basic procedure or a macro. Debugging usually involves executing specific portions of the procedure or macro and analyzing them at various points.) tasks such as checking current variable (variable: A named storage location capable of containing data that can be modified during program execution. Each variable has a name that uniquely identifies it within its level of scope. A data type can be specified or not.) values and running each line of code one at a time. To make Microsoft Visual Basic pause your code, you can set a breakpoint (breakpoint: A line of code in a Function or Sub procedure at which Microsoft Visual Basic automatically suspends execution.).

  1. In the Visual Basic Editor (Microsoft Visual Basic Editor: An environment in which you can edit macros that you've recorded and write new macros and Visual Basic for Applications programs.), 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.

  1. 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 (Code window: A window in the Microsoft Visual Basic Editor in which you write, display, and edit Visual Basic code. You can open as many Code windows as you have modules.).
  2. On the Debug menu, click Add Watch.
  3. If you already selected an expression (expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.) 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 (variable: A named storage location capable of containing data that can be modified during program execution. Each variable has a name that uniquely identifies it within its level of scope. A data type can be specified or not.), a property (property: A named attribute of a control, a field, or an object that you set to define one of the object's characteristics (such as size, color, or screen location) or an aspect of its behavior (such as whether the object is hidden).), 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 (Watch window: A window in the Visual Basic Editor used for debugging Visual Basic procedures. You can specify the variables from the procedure you want to inspect, and the Watch window displays the value of the variable as the procedure runs.).

  1. To select the range for which the expression will be evaluated, select a module (module: A collection of declarations, statements, and procedures stored together as one named unit. There are two types of modules: standard modules and class modules.) 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.

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

ShowDelete a watch expression

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

ShowDisable syntax checking

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.

 
 
Applies to:
Access 2003