Pausing a Procedure

   

Pausing a procedure in midstream lets you see certain elements such as the current values of variables and properties. It also lets you execute program code one statement at a time so you can monitor the flow of a procedure.

When you pause a procedure, VBA enters break mode, which means it displays the code window, highlights the current statement (the one that VBA will execute next ) in yellow, and displays a yellow arrow in the Margin Indicator Bar that points to the current statement. See the done = False statement in Figure 15.3.

Figure 15.3. VBA displays the Debug window when you enter break mode.

graphics/15fig03.jpg

Entering Break Mode

VBA gives you no fewer than five ways to enter break mode:

  • From a runtime error dialog box.

  • By pressing F8 at the beginning of a procedure.

  • By pressing Esc or Ctrl+Break while a procedure is running.

  • By setting breakpoints.

  • By using a Stop statement.

Entering Break Mode from an Error Dialog Box

When a runtime error occurs, the dialog box that appears only tells you the error number and the error description (see Figure 15.2, shown earlier). It doesn't tell you where the error occurred. Instead of scouring your code for possible bugs , you should click the Debug button to enter break mode. This will take you directly to the line that caused the error so that you can investigate the problem immediately.

Entering Break Mode at the Beginning of a Procedure

If you're not sure where to look for the cause of an error, you can start the procedure in break mode. Place the insertion point anywhere inside the procedure and then select Run, Step Into (or press F8). VBA enters break mode and highlights the Sub statement.

tip

graphics/tip_icon.gif

Many of the menu commands that I discuss in this chapter have button equivalents on the Debug toolbar. If you don't see this toolbar onscreen, activate the View, Toolbars, Debug command.


Entering Break Mode by Pressing the Esc Key

If your procedure isn't producing an error but appears to be behaving strangely, you can enter break mode by pressing Esc (or by selecting Run, Break) while the procedure is running. VBA pauses on whatever statement it was about to execute.

Alternatively, you can press Ctrl+Break to display the dialog box shown in Figure 15.4. Click Debug to put VBA into break mode.

Figure 15.4. This dialog box appears if you press Ctrl+Break while a procedure is running.

graphics/15fig04.jpg

Setting a Breakpoint

If you know approximately where an error or logic flaw is occurring, you can enter break mode at a specific statement in the procedure. This is called a breakpoint .

To set up a breakpoint, follow these steps:

  1. Activate the module containing the procedure you want to run.

  2. Move the insertion point to the statement where you want to enter break mode. VBA will run every line of code up to, but not including, this statement.

  3. Set the breakpoint by selecting Debug, Toggle Breakpoint (or by pressing F9; you can also click inside the Margin Indicator Bar, which is the vertical gray bar to the left of the code). As shown in Figure 15.5, VBA highlights the entire line in red and adds a breakpoint indicator in the Margin Indicator Bar.

    Figure 15.5. When you set a breakpoint, VBA highlights the entire line in red.

    graphics/15fig05.jpg

graphics/note_icon.gif

The command that sets a breakpoint is a toggle, so you can remove a breakpoint by placing the insertion point on the same line and running the command again.

To remove all the breakpoints in the module, select Debug, Clear All Breakpoints or press Ctrl+Shift+F9.


Entering Break Mode Using a Stop Statement

When developing your projects, you'll often test the robustness of a procedure by sending it various test values or by trying it out under different conditions. In many cases, you'll want to enter break mode to make sure things look okay. You could set breakpoints at specific statements, but you lose them if you close the file. For something a little more permanent, you can include a Stop statement in a procedure. VBA automatically enters break mode whenever it encounters a Stop statement.

Figure 15.6 shows the BackUpToFloppy procedure with a Stop statement inserted just before the statement that runs the SaveCopyAs method.

Figure 15.6. You can insert Stop statements to enter break mode at specific procedure locations.

graphics/15fig06.jpg

Exiting Break Mode

To exit break mode, you can use either of the following methods :

  • Resume normal program execution by selecting Run, Continue (or by pressing F5).

  • End the procedure by selecting Run, Reset.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net