All developers need to learn debugging. And when you're debugging, you can use several tools to track down problems within the logic of the written code. The Visual Basic Editor has a number of debugging tools usually found only in advanced development environments, such as Microsoft Visual C++.
When Visual Basic runs your code, you can break execution at a specific line of code and evaluate the current state of variables. When you press F5 to run your code or select a menu item or toolbar button that calls a specific macro, Visual Basic is in run mode because it's running code. If you add a breakpoint at a specific line of code, Visual Basic changes to break mode. While in break mode, you can do one of three things: stop execution, continue execution, or step through your code line by line. Stepping through code allows you to evaluate variables used in your code and see the exact path of code execution that Visual Basic is following.
The F9 key is a keyboard shortcut to clicking Toggle Breakpoint on the Debug menu in the Visual Basic Editor. The line is now highlighted in dark red, indicating that when Visual Basic runs this line of code, it will enter break mode. You remove this breakpoint by placing the cursor within the line of code and pressing F9 again or by clicking the dot in the left margin next to the line of code.
Visual Basic will start code execution by entering the ParseFileName procedure and will break at the first line within the procedure. In break mode, Visual Basic highlights the next line it'll run.
The F8 key is a shortcut to clicking Step Into on the Debug menu. As you'll see below, stepping through code by pressing F8 allows you to examine the values of variables and see the exact path Visual Basic code execution will take. You can press F5 anytime to continue code execution until the next breakpoint (if there is one). If you want to stop code execution while in break mode, click the Reset button (two buttons to the right of Continue) or click Reset on the Run menu in the Visual Basic Editor.
TIP
If you don't add a breakpoint anywhere in your code, you can start code execution in break mode by placing the cursor in the procedure you want to run and pressing F8. Visual Basic automatically enters break mode and highlights the first line of the procedure containing the cursor.
As you step through your code, there are several ways you can track a variable's value. The easiest way is to place the cursor over the variable in question when Visual Basic is in break mode. The Visual Basic Editor will display a tip window with the current value of the variable, object property, or function. The Data Tips window is similar to the ToolTip displayed when the cursor is over a toolbar button.
The Visual Basic Editor provides three other windows in which you can display the current value of variables. They are the Watch, Locals, and Immediate windows.
You can select a variable in a code module and drag it to the Watch window, where its contents are automatically updated each time the value changes during code execution.
The "out of context" message in the Value column in the Watch window indicates that the corresponding variable hasn't been used yet within the currently running module. Once a variable has been referenced within the currently running module, the Value column displays the current value assigned to that variable. With a variable that hasn't been declared yet (either by assigning a value to it or by explicitly creating it with the Dim statement), the third column, Type, displays the notation "Empty." When Visual Basic encounters a statement that declares the variable, it displays the appropriate data type (such as Integer, String, Single, or Long).
The fourth column in the Watch window indicates context, which defines the scope of the variables. The variables sChar and i are declared at the procedure level, within the GetPath function. The context of each variable in the Watch window is listed as Module1.GetPath. Module1 represents the name of the code module containing the function GetPath.
As you step through your code, the values of the variables sChar and i are updated in the Watch window. The Watch window allows you to monitor the values of as many variables as you choose. The program sets the value of i in the first line of the For…Next loop and the value of sChar in the next line.
Although the Locals window is very similar to the Watch window, you don't need to add the variables to be watched. By default, the Locals window automatically displays the values of all declared variables in the current procedure.
The program displays the values of all variables used in the GetPath function in the Locals window, including sFileName, which was passed to the GetPath function, and the value of GetPath itself. If there were any module-level variables, you could click the plus sign (+) beside the Module1 item in the Expression list to display their values.
The third window that the Visual Basic Editor provides as a debugging tool is the Immediate window. It's more versatile than the Data Tips, Watch, or Locals window. You can either explicitly "print" a value of a variable to the Immediate window, or you can type or paste a line of code into the window, press ENTER to run the code, and observe the results.
Debug.Print sChar |
The Debug.Print method accepts one argument, which is the variable or value you want to print to the Immediate window. In this case, you'll print the value of sChar to the Immediate window.
TIP
In the Print statement in the preceding example, you can replace the word "Print" with a question mark (?) so the line appears as ?i. The question mark is a shortcut to the word "Print" in the Immediate window.