Share via


Visual Basic Concepts

Running Selected Portions of Your Application

If you can identify the statement that caused an error, a single breakpoint might help you locate the problem. More often, however, you know only the general area of the code that caused the error. A breakpoint helps you isolate that problem area. You can then use Step Into and Step Over to observe the effect of each statement. If necessary, you can also skip over statements or back up by starting execution at a new line.

Step Mode Description
Step Into Execute the current statement and break at the next line, even if it's in another procedure.
Step Over Execute the entire procedure called by the current line and break at the line following the current line.
Step Out Execute the remainder of the current procedure and break at the statement following the one that called the procedure.

Note   You must be in break mode to use these commands. They are not available at design time or run time.

Using Step Into

You can use Step Into to execute code one statement at a time. (This is also known as single stepping.) After stepping through each statement, you can see its effect by looking at your application's forms or the debugging windows.

To step through code one statement at a time

  • From the Debug menu, choose Step Into.

    -or-

    Click the Step Into button on the Debug toolbar. (To display the Debug toolbar, right-click on the Visual Basic toolbar and select the Debug option.)

    -or-

    Press F8.

When you use Step Into to step through code one statement at a time, Visual Basic temporarily switches to run time, executes the current statement, and advances to the next statement. Then it switches back to break mode.

Note   Visual Basic allows you to step into individual statements, even if they are on the same line. A line of code can contain two or more statements, separated by a colon (:). Visual Basic uses a rectangular outline to indicate which of the statements will execute next. Breakpoints apply only to the first statement of a multiple-statement line.

Using Step Over

Step Over is identical to Step Into, except when the current statement contains a call to a procedure. Unlike Step Into, which steps into the called procedure, Step Over executes it as a unit and then steps to the next statement in the current procedure. Suppose, for example, that the statement calls the procedure SetAlarmTime:

SetAlarmTime 11, 30, 0

If you choose Step Into, the Code window shows the SetAlarmTime procedure and sets the current statement to the beginning of that procedure. This is the better choice only if you want to analyze the code within SetAlarmTime.

If you use Step Over, the Code window continues to display the current procedure. Execution advances to the statement immediately after the call to SetAlarmTime, unless SetAlarmTime contains a breakpoint or a Stop statement. Use Step Over if you want to stay at the same level of code and don't need to analyze the SetAlarmTime procedure.

You can alternate freely between Step Into and Step Over. The command you use depends on which portions of code you want to analyze at any given time.

To use Step Over

  • From the Debug menu, choose Step Over.

    -or-

    Click the Step Over button on the Debug toolbar. (To display the Debug toolbar, right-click on the Visual Basic toolbar and select the Debug option.)

    -or-

    Press SHIFT+F8.

Using Step Out

Step Out is similar to Step Into and Step Over, except it advances past the remainder of the code in the current procedure. If the procedure was called from another procedure, it advances to the statement immediately following the one that called the procedure.

To use Step Out

  • From the Debug menu, choose Step Out.

    -or-

    Click the Step Out button on the Debug toolbar. (To display the Debug toolbar, right-click on the Visual Basic toolbar and select the Debug option.)

    -or-

    Press CTRL+SHIFT+F8.

Bypassing Sections of Code

When your application is in break mode, you can use the Run To Cursor command to select a statement further down in your code where you want execution to stop. This lets you "step over" uninteresting sections of code, such as large loops.

To use Run To Cursor

  1. Put your application in break mode.

  2. Place the cursor where you want to stop.

  3. Press CTRL+F8.

    -or-

    From the Debug menu, choose Run To Cursor.

Setting the Next Statement to Be Executed

While debugging or experimenting with an application, you can use the Set Next Statement command to skip a certain section of code — for instance, a section that contains a known bug — so you can continue tracing other problems. Or you may want to return to an earlier statement to test part of the application using different values for properties or variables.

With Visual Basic, you can set a different line of code to execute next, provided it falls within the same procedure. The effect is similar to using Step Into, except Step Into executes only the next line of code in the procedure. By setting the next statement to execute, you choose which line executes next.

To set the next statement to be executed

  1. In break mode, move the insertion point (cursor) to the line of code you want to execute next.

  2. From the Debug menu, choose Set Next Statement.

  3. To resume execution, from the Run menu, choose Continue.

    -or-

    From the Debug menu, choose Run To Cursor, Step Into, Step Over, or Step Out.

Showing the Next Statement to Be Executed

You can use Show Next Statement to place the cursor on the line that will execute next. This feature is convenient if you've been executing code in an error handler and aren't sure where execution will resume. Show Next Statement is available only in break mode.

To show the next statement to be executed

  1. While in break mode, from the Debug menu, choose Show NextStatement.

  2. To resume execution, from the Run menu, choose Continue.

    -or-

    From the Debug menu, choose Run To Cursor, Step Into, Step Over, or Step Out.