Using Project Explorer

Project Explorer provides a list of all the modules contained in the current database (which is whatever database happens to be open in Access at the moment). The Toggle Folders button in the Project Explorer toolbar determines how the module names are displayed. When the Toggle Folders button is turned on, module names are shown in two separate folders as follows.

As in all programs, you can point to any button in Access to see its name. The Toggle Folders button is the third one from the left in the Project Explorer's toolbar.

i Microsoft Office Access Class Objects: Lists the names of all class modules in the current database. The name of the class module is the same as the form or report name, preceded by Form_ or Report_.

i Modules: Lists the names of all standard modules in the current database.

If either folder has a plus (+) sign next to its name, you can click that + to view objects within the folder. Conversely, clicking the minus (-) sign next to either folder name collapses the folder and hides its contents.

To open a module in the VBA editor, just double-click its name in Project Explorer. Each module that you open will open within its own Code window (described a little later in the section, "Using the Code window").

For class modules, Project Explorer also provides quick access to the form or report to which the module is attached. Just right-click any class module name and choose View Object. The form or report opens in Design view in Access. The VBA editor might then be covered by the Access window. However, the editor is still open, so you can get back to it by clicking its taskbar button.

The buttons to the left of the Toggle Folders button — View Code and View Object — also provide a means of switching between a class module and the object to which it's attached. Press Alt+F11 to switch back and forth between the Access and VBA editor program windows.

Using the Properties window

The Properties window in the VBA editor can be quite perplexing because it displays the properties of whatever object is currently selected in Access. If nothing is currently selected in Access, the Properties window might show nothing. That's often the case when you're working with standard modules because standard modules aren't tied to any particular object or event.

To illustrate how things tie together, Figure 2-3 shows a portion of a form, in Design view, in Access. One button on the form is currently selected. In the VBA editor window, which also appears in Figure 2-3, the properties for that selected button appear in the VBA editor Properties window.

In that same figure, you see an example of how Project Explorer might look in a database that already contains some modules. The modules whose names begin with the word Form_ are all class modules that are attached to forms in that database. The names Module1 and My Standard Module refer to standard modules in that same database.

Perhaps the most important thing to remember about Project Explorer and the Properties window is that they are optional, and you really don't need them taking up space in your VBA editor when you're not using them. Most of the time, you probably won't use them. So feel free to close those panes and forget about them if they just get in the way and confuse matters for you.

Figure 2-3:

A sample Properties window and Project Explorer.

Figure 2-3:

A sample Properties window and Project Explorer.

Selected object VBA Properties window VBA Project Explorer on an Access shows properties of lists all modules in form selected Access object. the current database.

Using the Immediate window

The Immediate window in the Visual Basic editor allows you to run code at any time, right on the spot. It's sometimes referred to as the debug window because it's mainly used for testing and debugging (removing errors from) code. If the Immediate window isn't open in the Visual Basic editor, you can bring it out of hiding anytime by choosing ViewOImmediate Window from the editor's menu bar.

When the Immediate window is open, you can anchor it to the bottom of the Visual Basic editor by dragging its title bar to the bottom of the window. Optionally, you can make the Immediate window free-floating by dragging its title bar up and away from the bottom of the Visual Basic editor's program window. You can also dock and undock the Immediate window by right-clicking within the Immediate window and choosing Dockable.

The Immediate window allows you to test expressions, run VBA procedures you've created, and more. You'll see practical examples throughout this book. But just to get your feet wet, test this simple expression in the Immediate window. Just bear in mind that an Access expression is any formula. For example, the simplest expression in the world is probably 1 + 1, which (as just about everyone knows) results in 2.

To test an expression in the Immediate window, do the following:

1. Click inside the Immediate window.

You need your cursor in that pane.

2. Type a question mark (?) followed by a space and the expression you want to test; then press Enter.

For example, click in the Immediate window and then type ? 1+1.

The Immediate window immediately shows you the result — 2 — as in Figure 2-4.

Figure 2-4:

Testing a simple expression in the Immediate window.

You might think of the ? mark character at the start of the line as asking the Immediate window "What is?" For example, if you think of ? 1+1 as meaning "What is one plus one?", then it stands to reason that the Immediate window would return 2. After all, 1 + 1 is 2!

When you start actually writing VBA code, you'll use the Immediate window to test and debug your code. For now, just know that the Immediate window is another optional pane in the Visual Basic editor that you can show and hide on an as-needed basis.

Using the Code window

The VBA editor's Code window is where you write, edit, and view VBA code. The Code window is similar to a word processor or text editor in that it supports all the standard Windows text-editing techniques. For example, you can type text or use the Backspace and Delete keys to delete text. And just like in



? 1+1




Testing a simple expression in the Immediate window.

Word, press the Tab key to indent text, select text by dragging the mouse pointer through it, and copy and paste text (to and from the Code window). In short, the Code window is a text editor.

Like all panes in the Visual Basic editor, the Code window can be docked or undocked. Choosing one view or the other is just a matter of personal preference and won't affect how you write and edit VBA code. You can easily switch between docked and undocked views.

When the Code window is undocked, it has its own title bar and can be moved and sized independently. To dock an undocked Code window, click the Code window's Maximize button (as shown in Figure 2-5).

When the Code window is docked, it fills the available space in the VBA editor window, and its Minimize, Restore, and Close buttons appear near the upper-right corner of the VBA editor's program window. Clicking the Code window's Restore Window button (also shown in Figure 2-5) undocks the Code window and allows it to float freely.

- sIS

-Ins Window Help

Type a question for help

if a? 1 # 1 Ln 4. Col 1

'J VBA Oummiev i:orm NumWord Demo Form (Code)


Optiori Compare database

'J VBA Oummiev i:orm NumWord Demo Form (Code)



Optiori Compare database

Private SUB ClicfcHereButtQri ÇlïdfcO I He. Cohvertedljuitiber .Visible = True

He . Cohvertedljuitiber .Value = NumTJörd fLIuitibe End Sub

Undocked Code window

Undocked Code window

Figure 2-5:

Code window Restore Window and Maximize buttons.

Add-lns Window Help



- Restore Window

Option Compare Database

— Docked Code window

He . ConvertedHui[iber . Visible - True He . ConvertedLIuiriber.Välue r1 NuiflWord (LIui[iber] End.Sub

As I mention earlier, the Code window is really a small word processor or text editor. But word processors tend to be oriented around paragraphs of text, whereas the Code window is built for typing individual lines of code. Unlike a word processor — where you don't press Enter until you get to the end of a paragraph — in the Code window, you press Enter at the end of each line you type.

When you type a line of VBA code and press Enter, the VBE compiles that line of code. For now, you can think of compiling as testing the line of code to see whether it will work. If you just type some line at random in the Code window — or even if you try to type a legitimate line of VBA code but make a mistake — you'll see a compile error message, as in Figure 2-6.

Figure 2-6:

Compile error in the Code window.

Figure 2-6:

Compile error in the Code window.

I talk about ways of dealing with compile errors when I really get into writing code in Chapter 3. For now, just realize that if you type anything other than a valid line of VBA code into the Code window, you'll see a compile error message as soon as you press Enter. So you don't want to waste your time trying to type text at random into the Code window.

Was this article helpful?

0 0

Post a comment