You write VBA functions and procedures in the VBA editor. To open the VBA editor, click the Module shortcut of the Database window, and then double-click the name of the module you want to display. To open a new VBA module, click the New button; the VBA editor window appears with default Options set, as shown in Figure 27.8. The VBA editor window incorporates a text editor, similar to Windows Notepad, in which you type your VBA code. VBA color-codes keywords and comments. By default, the VBA editor opens with all of its windows docked. Figure 27.8. The VBA editor window opens with the Project Explorer, Properties, and Immediate windows docked as shown here. The Toolbar of the Module Window Table 27.3 lists the purpose of each item in the toolbar of the Module window (refer to Figure 27.8) and the menu commands and key combinations that you can substitute for toolbar components. Buttons marked with an asterisk (*) in the Item column were changed by Access 2000. There are no Access 2003 changes to the editor other than the appearance of active toolbar buttons, elimination of the Office Assistant barrier to obtaining online help for VBA, and the addition of the Ask a Question text box. Table 27.3. VBA-Specific Elements of the VBA Editor's Toolbar and Code-Editing WindowButton | Item | Alternative Method | Purpose |
---|
| View Micro-Access* | View, Microsoft Access or press Alt+F11 | Displays the Access 2003 window. | | Insert Module | Insert, Module | Creates a new, empty module. Click the down arrow next to this button to create a new class module or to insert a new procedure or function. | | Find | Edit, Find or press Ctrl+F | Similar to the Find feature used in Table or Form view; allows you to search for a specific word or phrase in a module. | | Undo | Edit, Undo or press Ctrl+Z | Rescinds the last keyboard or mouse operation performed, if possible. | | Redo | Edit, Redo | Rescinds the last undo operation, if possible. | | Run Sub/* UserForm | Run, Run Sub/ User/Form or press F5 | Starts the execution of the current procedure, or continues executing a procedure after its execution has been halted by a break condition. If the code-editing window doesn't have thefocus, this button is called Run Macro and opens the Macros dialog in which to select the macro to run, if any. | | Break* | Run, Break or press Ctrl+Break | Halts execution of a procedure. | | Reset Shift+F5 | Run, Reset or press execution of a VBA procedure and reinitializes all variables to their default values. | Terminates | | Design mode | Run, Design Mode | Toggles design mode for UserForms. | | Project Explorer or | View, Project Explorer window . | Opens the Project Explorer press Ctrl+R | | Properties Window | View, Properties Window or press F4 | Opens the Properties window for the object selected in the Project Explorer. | | Object Browser | View, Object Browser or press F2 | Opens the Object Browser window. | | Toolbox | View, Toolbox | Shows the Toolbox for adding controls to UserForms. | | VBA Help | Help, Microsoft Visual Basic Help | Opens the Welcome topic of VBA online help. | N/A | Object List | None | Displays a list of objects in form or report modules. Only (General) appears for Access modules. | N/A | Procedure List | None | Displays a function or procedure in a module. Select the procedure or event name from the drop-down list. Procedures are listed in alphabetical order by name. | Module Shortcut Keys Additional shortcut keys and key combinations listed in Table 27.4 can help you as you write and edit VBA code. Only the most commonly used shortcut keys are listed in Table 27.4. Table 27.4. Primary Key Combinations for Entering and Editing VBA CodeKey Combination | Purpose |
---|
F3 | Finds next occurrence of a search string | Shift+F3 | Findsprevious occurrence of a search string | F9 | Setsor clears a breakpoint on the current line | Ctrl+Shift+F9 | Clears all breakpoints | Tab | Indents single or multiple lines of code by four (default value) characters | Shift+Tab | Outdents single or multiple lines of code by four characters | Ctrl+Y | Deletes the line on which the cursor is located | You can change the default indentation of four characters per tab stop by choosing Tools, Options. Click the Editor tab and then enter the desired number of characters in the Tab Width text box. The VBA Help System Microsoftprovides an extensive, multilevel Help system to help you learn and use VBA. The majority of the help topics for VBA are supplied by a generic VBA help file that's applicable to all flavors of VBA. If you place the cursor on a keyword or select a keyword and then press the F1 key, for example, a help window for the keyword appears (see Figure 27.9). If you click the "Example" hot spot under the name of the keyword, the window displays VBA sample code (see Figure 27.10). Figure 27.9. Placing the cursor on a keyword, such as Const, and pressing F1 opens the Microsoft Visual Basic Help window with the topic for the keyword. Figure 27.10. Clicking the Example link in the help window for a keyword opens a help window with VBA example code for the keyword. |