Поделиться через


Get "Immediate" results when creating or debugging code

In my last blog, I talked about debugging with the Watch window where you can do things such as look at the value of  expressions at some point in the code execution and halt execution when a variable reaches a specific value. Other useful  tools to help with debugging are the Immediate window and the Locals window. 

The Immediate window is the most basic debugging area. Unlike the Watch (or Locals) window, the Immediate Window can be used  regardless of whether your code is running or not. The Immediate is essentially a mini-VB interpreter that allows you to:

  • Evaluate expressions that don't appear in your code (e.g. math equations).
  • Run a procedure.
  • View the value of current variables or expressions.
  • Change the value of variables while the code is in break mode and see the results of the change.
  • Quickly see the methods and properties of an object.

For example, for items that return a value, use a question mark followed by the expression.
? 15 + 22 / 4
then press Enter to see the value.
If your code is currently running and paused at a breakpoint, you can evaluate the current value of a variable:
? strGreeting

You can also use it to launch a subroutine or function with the parameters, if applicable:
? MsgBox("Do you want to see the error message?", vbCritical+vbYesNo)
the Immediate Window runs the function, which displays the text string in a message box.

If you want to run a subroutine that doesn’t return a value, do not include the ? and just enter the subroutine name.

To test VBA statements in the Immediate window:
1. In the Visual Basic Editor, on the View menu, click Immediate Window.
2. In the Immediate window, type ?15 + 22 / 4
3. Press ENTER. 20.5 is displayed.
4. Add the following procedure to the code window. This procedure loops through a string and prints each character to the  Immediate window using the Debug.Print statement:
Sub PrintChars()

Dim strTest As String
Dim strChar As String
Dim int1 as Integer

strTest = "Look at each character"

For int1 = 1 to Len(strTest)
   strChar = Mid(strTest, int1, 1)
   Debug.Print(strChar)
Next int1

End Sub

6. Place a breakpoint at the Debug statement by clicking the gray column to the left side of the statement.
7. In the Immediate window, type PrintChars. The code runs and then pauses at the Debug statement.
8. Type ?strChar in the Immediate window and press ENTER. The current setting of the variable (L) is displayed in the window. 

You can change the value of a variable while the code is in break mode. Just type the line of code in the Immediate window and press Enter. Then step through the code to see the effect of the change. For example, using the subroutine described above:

1. Set a break point on the Debug.Print statement and press F5 to run the procedure. If the Macros dialog box pops up, select the macro name and click Run.
2. With the code paused, press F8 a few times to print the first couple of letter in the text string (L o o, for example).
3. Now below the PrintChars line in the Immediate window, type: strTest = "New message." and press Enter.
4. Press F8 several times and notice that now the characters being displayed have changed to the new string, starting from the current position in the string (m e s s a g e).

You can also take advantage of Intellisense in the Immediate window. For example, type Application. in the window and a  drop-down box is displayed with the associated members of the object.

You may not be aware but instead of setting a breakpoint in your code, you can just highlight the code statement where you want to pause execution, click Run To Cursor on the Debug menu and program execution will immediately continue to the  point where you are at. Then you can step through the procedure pressing F8. I like to use this technique because if keeps me from cluttering up my code with breakpoints. When debugging, I will invariably forget that I placed multiple breakpoints in the code and when I run the procedure, execution will stop when I don't want it to. Run To Cursor will allow me to pause execution without cluttering up my code. Another way around this problem is just before setting a breakpoint at the current location, click the Debug menu and then click Clear All Breakpoints.

Also, the Debug.Print statement lets you write output to the Immediate window. Insert this command into sections of your code where you’d like to know the value of certain variables, but don't want to stop the program to get it. For instance, if you’re moving through a recordset and would like to know the values of a few fields as the processing occurs, you may have code like  the following before the other processing of the record occurs:

Debug.Print intCount & ": " & rst![ID] & ", " & rst![Name]
intCount = intCount + 1
It’s not as complete as stepping through each line, but will give you an indication of whether the code is running correctly 
before narrowing your search further.

Although the Immediate window is a powerful tool when creating and debugging code, it does have limitations. For example, you can only execute one statement at a time in the window. However, there is a way around this limitation. Some control structures such as For...Next loops, which require multiple statements, can be put on a single line and executed. For example, on a blank line in the Immediate window, type for i = 1 to 5 : ? i : Next i and press Enter. The statement runs and displays the values of "i."

The Locals window is good for looking at the value of active variables when the code is in break mode. You can also see the  value of properties and even some controls. The Locals windows displays the entire list of local variables and their current  values. For example, again looking at the subroutine in the last example:

If you place a break statement on the Debug statement and run the code, the code pauses and you can look in the Locals window and see the value of the variables strTest, strChar, and int1. You can then press F8 to step through the loop and see the values of the variables change.

A variable that represents an object appears in the Locals window with a plus sign (+) to the left of its name. You can click the plus sign to expand the variable, displaying the properties of the object and their current values. If a property of the object contains another object, that can be expanded as well. The same holds true for variables that contain arrays or user­defined types.

Like the Immediate window, sometimes you can change local variable values by selecting the variable you want to change and clicking the Value box. If VB lets you to change the variable, the Value field will let you edit it. Some things can't be changed though. For example, you can't change objects, such as the properties of a control, or certain Variant variables. In cases where you can't change the values of a variable in the Locals window, you can use the Immediate window to test the change.

Before finishing this blog, another tip that I uncovered looking through VB help involves moving between Visual Basic procedures. Simply highlight the property or procedure name in the code and press SHIFT+F2; you'll be transported to the highlighted property or procedure. If the subroutine or property is located outside the project, you'll be taken to the Object Browser, where you can find additional information. You can also move back to the original location by pressing CTRL+SHIFT+F2.