The VBA environment in Access now includes the same editor as other Office products, as well as Visual Basic. Because the VBE is now in its own MDI space, you must go to the editor to set the coding environment options.
To change or view the settings of VBA environment, use the Options dialog for VBA. For instance, to change the color of a line of code that has a syntax error in it follow these steps:
1. |
Create a new database, or open an existing one. |
2. |
Press Alt-F11. The Visual Basic Editor opens. |
3. |
From the Tools menu, choose Options. The VBA Options dialog appears, with the following tabbed pages: Editor, Editor Format, General, and Docking. |
4. |
On the Editor Format page, select Syntax Error Text from the Code Colors list box (see Figure A.1). Figure A.1. The Editor Format page in the Options dialog contains a number of options used for debugging purposes. |
Setting the color of code items is one of many ways to set up the application environment for maximum debugging power. The advantage of using color in code is that you can tell what's happening with different parts of the code. Red, for example, denotes a syntax error.
Table A.1 lists the various commands and their default color settings. The Foreground and Background columns refer to the specific line of code discussed. VBA now uses color along the left side of the module editor, called a margin indicator, which helps point out various commands that have been placed in the module editor.
Text Area | Foreground | Background | Indicator |
---|---|---|---|
Normal | Automatic | Automatic | Automatic |
Selection | Automatic | Automatic | Automatic |
Syntax Error | Light Red | Automatic | Automatic |
Execution Point | Automatic | Dark Yellow | Dark Yellow |
Breakpoint | White | Dark Red | Dark Red |
Comment | Light Green | Automatic | Automatic |
Keyword | Dark Blue | Automatic | Automatic |
Identifier | Automatic | Automatic | Automatic |
Bookmark | Automatic | Automatic | Cyan |
Call Return | Automatic | Automatic | Light Green |
Tip
Notice that one of the colors is for Bookmark. Bookmarks are used for tagging lines of code that you want to remember later for one reason or another. This feature is very useful in big chunks of code. A square with rounded corners appears in the margin indicator bar along the left side of the current module window.
You can use the bookmarks selections off the Edit menu for Bookmark (toggle), Next Bookmark, Previous Bookmark, and Clear All Bookmarks. Bookmarks disappear when you close the database.
Other useful coding options are found on the different pages in the Options dialog. From the Editor page, you have these code settings:
Auto Syntax Check. When selected, this option makes Access generate an error with a message box while you type lines of code. The message box doesn't appear unless an error exists after you complete a line of code and press Enter. It's recommended that you leave this option set to its default (on).
Require Variable Declaration. When enabled, this option places the Option Explicit statement in the Declarations section of any new modules created. It doesn't affect previously created modules. It's recommended that you change this option from its default (off) to on.
Tip
If you turn on Require Variable Declaration and use Option Explicit in each of your modules, you'll save countless hours of searching for misspelled variables. For more information on explicit versus implicit variable declarations, see Chapter 2, “Coding in Access 2000 with VBA.”
Auto List Members. Selecting this option causes a list of possible options (that is, properties and methods) to appear when you're building a statement in code.
Auto Quick Info. With this option on, function syntax is displayed below the line of code on which you're now working. This will reflect the function, statement, or object you're now typing out. This also includes user-defined procedures.
Auto Data Tips. By setting this option to true, you will see the value of the variable you have the cursor over when in a break of program execution.
Tip
The preceding three options have the dumb name of IntelliSense, but are very hot. Auto Data Tips in particular are great because you don't have to highlight values and click Quick Watch. All you do is place the cursor over the variable name to examine it (see Figure A.2).
Auto Indent. Used for indenting code automatically, this option enhances readability for debugging and maintaining code. It's recommended that you leave this option set to its default (on).
The rest of the commands that would affect the debugging environment are on the General page (see Figure A.3):
Notify Before State Loss. This is where you can specify whether you want Access to tell you when your variables will be reset at the module level when a running project is halted.
Error Trapping. These three options let you choose when you want Access to break on errors that can occur in your code:
Compile on Demand. This option keeps Access from compiling the entire potential call tree when you start up a form. VBA compiles only as you call various functions. By default, this option is on.
Background Compile. By selecting this option, Access will compile any uncompiled code when Access's processes are idle.
Note
Remember to first choose Compile Project_Name from the Debug menu while in the module editor before distributing a system. This ensures that no compile errors are lurking in obscure forms. Also, because Access doesn't have to compile the code during runtime while in production, the application performs better.
You should also Save from the File menu to save the project after compiling.
Now that you've set up the environment the way you want in order to debug your application, look at some tools that will help you harness the power of debugging in Access.
13.59.177.14