MS-Access / Getting Started

Visual Basic Editor Window

When you open a module in Design view, Access 2010 opens the VBE and asks the editor to display your code. Open the Conrad Systems Contacts (Contacts.accdb) sample database if you haven't already, view the Modules list in the Navigation pane, and then either rightclick the modExamples object and click Design View on the shortcut menu or double-click the modExamples object to see the code for this module opened in the VBE. Notice that the VBE in Access 2010 uses the older menu and toolbar technology from releases of Access before Access 2007, not the ribbon used in the main Access window.

Particularly if you have opened the VBE previously and moved some windows around. In the upper-left corner of the figure, you can see the Visual Basic Project Explorer window docked in the workspace. (Click Project Explorer on the View menu or press Ctrl+R to see this window if it's not visible.) In this window, you can discover all module objects and form and report class modules saved in the database. You can double-click any module to open it in the Code window, which you can see maximized in the upper-right corner.

Docked in the lower-left corner is the Properties window. (Click Properties Window on the View menu or press F4 to see this window if it's not visible.) When you have a form or report that has a Visual Basic module open in Design view in Access, you can click that object in the Project Explorer to see all its properties. If you modify a property in the Properties window, you're changing it in Access. To open a form or report that is not open, you can select it in the Project Explorer and then click Object on the View menu.

In the lower-right corner, you can see the Locals window docked. (Click Locals Window on the View menu to see this window if it's not visible.) As you will see later, this window allows you to instantly see the values of any active variables or objects when you pause execution in a procedure. In the lower center, you can see the Immediate window docked. (Click Immediate Window on the View menu or press Ctrl+G to see this window if it's not visible.) It's called the Immediate window because you can type any valid Visual Basic statement and press Enter to execute the statement immediately. You can also use a special "what is" command character (?) to find out the value of an expression or variable. For example, you can type ?5*20 and press Enter, and Visual Basic responds with the answer on the following line: 100.

You can undock any window by grabbing its title bar and dragging it away from its docked position on the edge toward the center of the screen. You can also undock a window by right-clicking anywhere in the window and clearing the Dockable property. As you will see later, you can set the Dockable property of any window by clicking Options on the Tools menu. When a window is set as Dockable but not docked along an edge, it becomes a pop-up window that floats on top of other windows-similar to the way an Access form works when its Pop Up property is set to Yes. When you make any window not Dockable, it shares the space occupied by the Code window.

You cannot set the Code window as Dockable. The Code window always appears in the part of the workspace that is not occupied by docked windows. You can maximize the Code window to fill this remaining space. You can also click the Restore button for this window and open multiple overlaid Code windows for different modules within the Code window space.

At the top of the Code window, just below the toolbar, you can see two list boxes:

  • Object list box
    When you're editing a form or report class module, open this list on the left to select the form or the report, a section on the form or the report, or any control on the form or the report that can generate an event. The Procedure list box then shows the available event procedures for the selected object. Select General to view the Declarations section of the module, where you can set options or declare variables shared by multiple procedures. In a form or a report class module, General is also where you'll see any procedures you have coded that do not respond to events. When you're editing a standard module object, this list displays only the General option. In a class module object, you can choose General or Class.
  • Procedure list box
    Open this list on the right to select a procedure in the module and display that procedure in the Code window. When you're editing a form or report module, this list shows the available event procedures for the selected object and displays in bold type the event procedures that you have coded and attached to the form or the report. When you're editing a module object, the list displays in alphabetic order all the procedures you coded in the module. In a class module when you have selected Class in the Object list box, you can choose the special Initialize or Terminate procedures for the class.

Dragged the divider bar at the top of the scroll bar on the right of the Code window downward to open two edit windows. We clicked in the lower window and then clicked ShowTables in the Procedure list box. You might find a split window very handy when you're tracing calls from one procedure to another. The Procedure list box always shows you the name of the procedure that currently has the focus. In the Code window, you can use the arrow keys to move horizontally and vertically. When you enter a new line of code and press Enter, Visual Basic optionally verifies the syntax of the line and warns you of any problems it finds.

If you want to create a new procedure in a module, you can type either a Function statement, a Sub statement, or a Property statement on any blank line above or below an existing procedure and then press Enter; click anywhere in the module and click the arrow to the right of the Insert button on the toolbar and then click Procedure; or click Procedure on the Insert menu. Visual Basic creates a new procedure for you (it does not embed the new procedure in the procedure you were editing) and inserts an End Function, End Sub, or End Property statement. When you create a new procedure using the Insert button or the Insert menu, Visual Basic opens a dialog box where you can enter the name of the new procedure, select the type of the procedure (Sub, Function, or Property), and select the scope of the procedure (Public or Private). To help you organize your procedures, Visual Basic inserts the new procedure in alphabetical order within the existing procedures.

Remember
If you type a Function, Sub, or Property statement in the middle of an existing procedure, Visual Basic accepts the statement if it's syntactically correct, but your project won't compile because you cannot place a Function, Sub, or Property procedure inside another Function, Sub, or Property procedure.

If you are working in a form or report module, you can select an object in the object list box and then open the Procedure list box to see all the available events for that object. An event name displayed in bold type means you have created a procedure to handle that event. Select an event whose name isn't displayed in bold type to create a procedure to handle that event.

Visual Basic provides many options that you can set to customize how you work with modules. Click Options on the Tools menu, and then click the Editor tab to see the settings for these options.

On the Editor tab, some important options to consider are Auto Syntax Check, to check the syntax of lines of code as you enter them; and Require Variable Declaration, which forces you to declare all your variables. (Require Variable Declaration is not selected by default- you'll see later why it's important to select it.) If you want to see required and optional parameters as you type complex function calls, select the Auto List Members check box. Auto Quick Info provides drop-down lists where appropriate built-in constants are available to complete parameters in function or subroutine calls. When you're debugging code, Auto Data Tips lets you discover the current value of a variable by pausing your mouse pointer on any usage of the variable in your code.

Drag-And-Drop Text Editing allows you to highlight code and drag it to a new location. Default To Full Module View shows all your code for multiple procedures in a module in a single scrollable view. If you clear that check box, you will see only one procedure at a time and must page up or down or select a different procedure in the Procedure list box to move to a different part of the module. When you're in full module view, selecting the Procedure Separator check box asks Visual Basic to draw a line between procedures to make it easy to see where one procedure ends and another begins.

Selecting the Auto Indent check box asks Visual Basic to leave you at the same indent as the previous line of code when you press the Enter key to insert a new line. In the sample databases with indents to make it easy to see related lines of code within a loop or an If...Then...Else construct. You can set the Tab Width to any value from 1 through 32. This setting tells Visual Basic how many spaces you want to indent when you press the Tab key while writing code.

On the Editor Format tab of the Options dialog box, you can set custom colors for various types of code elements and also choose a display font. We recommend using a monospaced font such as Courier New for all code editing.

On the General tab, you can set some important options that dictate how Visual Basic acts as you enter new code and as you debug your code. You can ignore all the settings under Form Grid Settings because they apply to forms designed in Visual Basic, not Access.

If your code has halted, in many cases you can enter new code or correct problems in code before continuing to test. Some changes you make, however, will force Visual Basic to reset rather than let you continue to run from the halted point. If you select the Notify Before State Loss check box, Visual Basic will warn you before allowing you to make code changes that would cause it to reset.

In the Error Trapping section, you can select one of three ways to tell Visual Basic how to deal with errors. As you'll discover later in this tutorial, you can write statements in your code to attempt to catch errors. If you think you have a problem in your error-trapping code, you can select Break On All Errors. With this setting, Visual Basic ignores all error trapping and halts execution on any error. If you have written class modules that can be called from other modules, to catch an untrapped error that occurs within a class module, choose Break In Class Module to halt on the statement within the class module that failed. (We recommend this setting for most testing.) If you choose Break On Unhandled Errors, and an untrapped error occurs within a class module, Visual Basic halts at the statement that invoked the class module.

The last two important options on this tab are Compile On Demand and Background Compile. With the Compile On Demand check box selected, Visual Basic compiles any previously uncompiled new code whenever you run that code directly or run a procedure that calls that code. Background Compile lets Visual Basic use spare CPU cycles to compile new code as you are working in other areas.

Finally, on the Docking tab, you can specify whether the Immediate window, Locals window, Watch window, Project Explorer, Properties window, or Object Browser can be docked. We will take a look at the Immediate window and Watch window in the next section. You can use the Object Browser to discover all the supported properties and methods of any object or function defined in Access, Visual Basic, or your database application.

[Previous] [Contents] [Next]