Using the Immediate Window in Break Mode

When the procedure execution is suspended, the Code window appears in break mode. This is a good time to activate the Immediate window and type VBA instructions to find out, for instance, the name of the open form or the value of a certain control. You can also use the Immediate window to change the contents of variables in order to correct values that may be causing errors. By now, you should be an expert when it comes to working in the Immediate window. Figure 9-6 shows the suspended ListEndDates function procedure and the Immediate window with the questions that were asked of Visual Basic while in break mode.

Figure 9-6: When code execution is suspended you can find answers to many questions by entering appropriate statements in the Immediate window.

In break mode, you can hold the mouse pointer over any variable in a running procedure to see the variable's value. For example, in the ListEndDates function procedure shown in Figure 9-7, the breakpoint has been set on the

Part I

statement just before the End Select keywords. When Visual Basic encounters this statement, the Code window appears in break mode. Because Visual Basic has already executed the statement that stores the value of the variable intOffset, you can quickly find out the value of this variable by resting the mouse pointer over its name. The name of the variable and its current value appear in a frame. To show the values of several variables used in a procedure at once, you should use the Locals window, which is discussed later in this chapter.

'days till ending date intOffset - Abs((8 ■ Weekday(Now)) Mod 7) 'start 5 wetks prior lo current week ending date ' (7 days " 5 weeks ~ 35 days before next ending I 1 and stiow t1 dates |ListEndDates = Format[((Nuw(| + intOffset) - 35) 7 * row. -MlvTOD/YYYYlritoii-:...-> - 2] End Select

Figure 9-7: In break mode, you can find out the value of a variable by resting the mouse pointer on that variable.

Working in a Code Window in Break Mode

While in break mode, you can change code, add new statements, execute the procedure one line at a time, skip lines, set the next statement, use the Immediate window, and more. When Visual Basic is in break mode, all of the options on the Debug menu are available. You can enter break mode by pressing Ctrl + Break or F8, or setting a breakpoint. In break mode, if you change a certain line of code, VBA will prompt you to reset the project by displaying the message, "This action will reset your project, proceed anyway?" Click OK to stop the program's execution and proceed editing your code, or Cancel to delete the new changes and continue running the code from the point where it was suspended. For example, change the variable declaration. As you press F5 to resume code execution, VBA will prompt you to reset your project.

0 0

Post a comment