If you know exactly which statement caused an error, using a single breakpoint may be sufficient. More often, however, you can only guess the general vicinity of the statement that caused the error. Setting a breakpoint helps you get to that general area. Once there, you can step through your code and run it line by line to see the effect of each statement.
You can also step through each line of code in a called procedure—a procedure the current procedure calls—or step over the called-procedure code. (Stepping through statements is sometimes referred to as tracing.) If necessary, you can also back up and start execution at a previous line.
Stepping is the process of running one statement at a time. After running each statement, you can use the Debug window to see how it affected the variables and objects in the procedure.
Û To step through code one statement at a time
When the Step Into command is carried out, Visual Basic runs the current statement and then automatically advances execution to the next statement in the procedure and again suspends execution.
A line of code can contain two or more statements separated by a colon (:). You can step from one statement to the next even if they are on the same line. However, breakpoints apply only to the first statement in a line.
Note If your Microsoft Access workgroup has security enabled (if you logged on by using the Logon dialog box), you can step through code in a called procedure only if you have Read Design permission for the called procedure’s module. If you try to step into a called procedure and you don’t have Read Design permission for the called procedure’s module, Visual Basic runs the called procedure without stepping through the code one statement at a time.
Tip You can also step through code by placing the cursor within the procedure and pressing F8. If at any point you want to run the rest of the procedure without stepping through it, click the Go/Continue button on the toolbar or press F5.
If you don’t want to step through the lines of code in a procedure, you can step over the entire procedure.
Û To step over an entire procedure
The Step Over command is identical to the Step Into command except when the current statement contains a call to another procedure. The Step Into command steps into the procedure that was called and then lets you step through it line by line, but the Step Over command runs the called procedure as a unit and then steps into the next statement in the current procedure. For example, suppose the current statement calls the procedure GetQueryCriteria:
GetQueryCriteria strCriteria
If you use the Step Into command, the Module window displays the GetQueryCriteria procedure and the first statement in that procedure is the current statement. The Step Into command is the best choice if you want to analyze the code within GetQueryCriteria.
If you use the Step Over command, the Module window continues to display the current procedure while Visual Basic runs the GetQueryCriteria procedure. Execution then advances to the statement immediately after the call to GetQueryCriteria. Step Over is a better choice than Step Into if you want to stay within the current procedure and don’t need to analyze the code in the GetQueryCriteria procedure.
If you step into a procedure and then decide that you don’t want to step all the way through it, you can step out of the procedure.
Û To step out of a procedure
When the Step Out command is carried out, Visual Basic runs the rest of the procedure, including any nested procedures that it calls, in one step. Execution stops when it returns to the next line of code in the calling procedure. If the current procedure wasn’t called by another procedure, the Step Out command has the same effect as the Continue command; that is, the current procedure runs to completion.
You can freely alternate between the Step Into, Step Over, and Step Out commands. The command you choose depends on which portions of code you want to analyze at any given time.
When you suspect a certain section of code contains an error, you may want to skip over it. This doesn’t solve the problem, of course—eventually, you’ll have to debug that section. However, skipping lines of code enables you to move on and examine other parts of your code.
At other times, you may want to rerun code in the procedure that has already been executed. Perhaps you’ve modified that code or changed the value of a variable and want to see if these changes produce different results.
Visual Basic enables you to continue execution at a line of code other than the next one as long as that line is within the current procedure.
Û To set the next statement to be run
You can then continue execution by pressing F5 or you can step through the lines of code starting with the statement you set.
Important You can use the Set Next Statement command in a procedure only if you’ve suspended execution in that procedure.
You can also use the Run To Cursor command (Debug menu) to run your code until it reaches the line that you have selected. You may find this command useful if you want to stop execution before a large loop farther down in your code. Or you may want to use this command if you’re sure that a certain section of your code is functioning properly, and you want to continue troubleshooting later in the procedure. To run code until it comes to a specific line, right-click the line of code, and then click Run To Cursor on the shortcut menu, or select the line of code where you want to again suspend execution and press CTRL+F8.