Article ID: 210143 - Last Review: October 11, 2006 - Revision: 3.2 ACC2000: Tips for Debugging Visual Basic for Applications CodeThis article was previously published under Q210143 Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp). On This PageSUMMARY
This article shows you tips that can help you debug your Visual Basic for
Applications code, particularly when your code compiles
correctly, but does not perform as you expect.
Debugging is the 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 can encounter: compile-time errors, run-time errors, and logic errors. Logic errors occur when the application does not 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. All of these aid in finding and eliminating logic errors. The general procedure for debugging Visual Basic for Applications code is to break larger procedures into smaller subprocedures to verify that the individual steps in each subprocedure work correctly, and then to combine the subprocedures one by one until the code works correctly. MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. There are several different methods that you can use to debug your Visual Basic for Applications code. The main ones are these: Use the Immediate WindowYou can use the Immediate window to run individual lines of Visual Basic for Applications code or to check the values of variables. In the Immediate window, you can:
To use the Immediate window, follow these steps:
If you are testing a Sub procedure, type SubNamearg1, ..., argN, where SubName is the name of your procedure and arg1 ... argN are any arguments it requires. Use a Breakpoint to Suspend Execution of CodeIf the first part of your code runs correctly but other parts do not, you may want to suspend execution of Visual Basic for Applications code, so that the procedure is still running but pauses at selected statements. You can do this by setting breakpoints. To set a breakpoint, follow these steps:
Use the Debug.Print StatementYou can display the value of an expression in the Immediate window by using the Print method of the Debug object, followed by the expression. This lets you see a list of values that your variables take on as the procedure executes.Good places to position Debug.Print statements include the following:
Set a Watch Expression in Visual Basic for Applications CodeA Watch expression is an one that you monitor in the Watch window You can also use Quick Watch to see the value of an expression that has not previously been specified as a Watch expression.To add a Watch expression to the Watch window, follow these steps:
View the Call Stack to Trace Nested ProceduresThe Call Stack dialog box displays a list of all active procedure calls. These are the procedures in an application that have started but not completed. You can use the Call Stack dialog box to trace the operation of an application as it runs a series of procedures. You can view the Call Stack from the Immediate window by clicking Call Stack on the View menu. The earliest active procedure call is placed at the bottom of the list and subsequent procedure calls are added to the top.You can use the Show button in the Call Stack dialog box to display the statement in one procedure that has called the procedure listed above it. If you choose the current procedure in the Call Stack dialog box and then click Show, the Visual Basic Editor displays the statement at which execution was suspended. To see the calls in the PyrNum() example above, follow these steps:
Use the Locals WindowThe Locals window is similar to the Watch window and normally displays all variables and objects in the current procedure. It has three columns: Expression, Value, and Type.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 Immediate window in order to test the behavior of your module. The Type column shows the data type of the current module-level object. Inspecting a selected variable's value in the Locals window can be very helpful in debugging your module, as can changing a variable's value in the Locals window Value column to observe what effect it has on other parts of your module. REFERENCESFor more information about the Immediate window, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type use the Immediate window of the visual basic editor while working with visual basic in the Office Assistant or the Answer Wizard, and then click Search to view the topic. For more information about breakpoints, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type use a breakpoint to suspend running of visual basic code in the Office Assistant or the Answer Wizard, and then click Search to view the topic. For more information about watch expressions, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type set a watch expression in visual basic code in the Office Assistant or the Answer Wizard, and then click Search to view the topic. For more information about debugging VB code, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type debug visual basic code in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
| Article Translations
|
Back to the top
