Debugging Your Code

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

Breakpoints

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.

Set Breakpoints in Code

  1. In the ParseFileName procedure, place the cursor in the line MsgBox GetPath("C:\Temp\Test.txt") and press F9.
  2. 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.

    Click to view at full size.

  3. With a breakpoint set for the MsgBox statement, and with the cursor placed in the ParseFileName procedure, press F5.
  4. 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.

    Click to view at full size.

  5. Press F8 several times to step through the code.
  6. 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.

  1. Press F5 to finish running the macro. Click OK to close the message box.

The Data Tips Window

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.

Display Data Tips

  1. With the same breakpoint set, place the cursor in the ParseFileName procedure and press F5 to start running the code.
  2. Step through the code by pressing F8 until code execution reaches the If…Then condition block.
  3. Place the cursor over the variable sChar in the line If sChar = "\" Then. You'll see the Data Tips window showing the current value of sChar, which is the letter "t". The letter "t" is the last character in the string "C:\Temp\Test.txt" and is the first character to which sChar is assigned.
  4. Click to view at full size.

  5. Continue pressing F8 to step through the code, and each time the For…Next loop reaches the If…Then condition block, display the Data Tips window by placing the cursor over the variable sChar.
  6. Press F5 to finish running the macro. Click OK to close the message box.

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.

The Watch Window

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.

Add Watch Variables

  1. If you don't see the Watch window, click Watch Window on the View menu in the Visual Basic Editor.
  2. Double-click the variable sChar in the code module. (You can select any of the three occurrences of sChar.)
  3. Drag the selected variable name to the Watch window. You can also add variables to the Watch window by selecting the var-iable in code, clicking Add Watch on the Debug menu, and clicking OK.
  4. Select the variable i in the code module and drag it to the Watch window.
  5. Click to view at full size.

    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.

  6. Step through your code by placing the cursor within the ParseFileName procedure and pressing F8 several times.
  7. 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.

  8. Press F5 to finish running the macro. Click OK to close the message box.

The Locals Window

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.

Observe the Values of Local Variables

  1. If you don't see the Locals window, on the View menu in the Visual Basic Editor click Locals Window.
  2. Place the cursor in the ParseFileName procedure.
  3. Press F8 several times to step through your code again, until you reach the end of the first iteration through the For…Next loop in the GetPath function.
  4. Click to view at full size.

    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.

  5. Press F5 to finish running the macro. Click OK to close the message box.

The Immediate Window

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.

Print to the Immediate Window

  1. If you don't see the Immediate window, on the View menu in the Visual Basic Editor click Immediate Window or press CTRL+G.
  2. In the Code window, in the GetPath function, add the following line below the line that sets the value of the variable sChar (sChar = Mid$(sFileName, i, 1)):
  3. 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.

  4. Place the cursor in the ParseFileName procedure and press F8 several times until the procedure ends. The program prints the value of the variable sChar to the Immediate window during each iteration through the For…Next loop.
  5. Click to view at full size.

Execute a Line of Code in the Immediate Window

  1. Step through your code, starting within the ParseFileName procedure, until you reach the line setting the value of sChar in the GetPath function.
  2. Click in the bottom of the Immediate window, type Print i, and press ENTER. The value of i will be printed below the line you typed. You can also determine the value of any property of any object defined within the current scope of code execution.
  3. Exit your application.

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.