Bringing up the Immediate window during runtime is no problem. You can try to pause an application programmatically with either a Stop statement or a breakpoint. When placed in code, both methods will halt the execution of a piece of code and bring up the module editor, with the code line containing the Stop statement or breakpoint highlighted. You can have as many breakpoints and Stop statements in your code as you want.
Figure A.6 shows a Stop statement and a breakpoint set in the ap_AppInit() function, which is in the GlobalUtilities module.
Note
You can't place Stop statements or breakpoints in comments, variable declarations (the Dim statement), blank lines, line labels, or line numbers.
To place a Stop statement in a code line, simply type the command.
Caution
Stop statements, if saved with the module, remain in the code until you remove them. Remember to remove all Stop statements from your code and forms modules before distributing your application for production. To find all occurrences of Stop statements, follow these steps:
1. |
Open a module. |
2. |
From the Edit menu, choose Replace. |
3. |
Type Stop in the Find What text box. (Leave the Replace With text box blank.) |
4. |
Select Current Project in the Search section. |
5. |
Click the Find Next button. |
6. |
Click the Replace button if the text found is a legitimate Stop statement. If the text is part of another line of code, such as comments or a text string, skip this step. |
7. |
Tip
Although not required, if you turn on “Find Whole Word Only” in the Replace dialog, you'll be less likely to get bogus occurrences of the string.
The alternative to a Stop statement is a breakpoint. Breakpoints are useful because when the database is closed, the breakpoints go away. As a result, you don't have to worry about breakpoints left in your code when you distribute the application.
To set a breakpoint, first highlight the line on which you want to stop code execution. Then you can toggle (set or unset) a breakpoint in one of a number of ways:
If the breakpoint didn't exist before, you'll see it at this time, highlighted in whatever color you set the Breakpoint Text color code to be. (The default color is dark red for the background, white for the foreground.) You'll also see a red dot if the margin indicator bar is on. To unset a breakpoint, follow the same steps that were performed to set it. (For information on how to set the breakpoint text color, refer to the earlier section “Setting the Correct Module Options for Maximum Debugging Power.”)
To remove all breakpoints in an application, open the Debug menu and choose Clear All Breakpoints while in the VBE.
Tip
Inserting a breakpoint in your code can really help you track down an error in your code when a variable is returning a Null value when it shouldn't be. You could place a breakpoint just after the point in the code where the variable is being assigned, and examine the environment with some of the other commands listed later in the section “Watching Expressions During Program Execution.”
The Assert method off the Debug object stops execution of an application based on criteria. The syntax would look like this:
Assert.Debug booleanexpression
The assertion stops execution with the last statement executed, the Debug.Assert line.
18.220.160.216