Anatomy of the VBA Editor

You can access the VBA Editor in several ways. From anywhere in Microsoft Access, press Alt+F11 or choose from several places on Access's new Ribbon: Create, Macros, Modules, Database Tools, or Visual Basic. You can also open the VBA Editor by double-clicking a module name in the navigation pane or from any form or report. From the Properties dialog box, click the Events tab, select the event that you're interested in, click the Ellipses button (...), and choose Code Builder. When you first view the VBA Editor, you might be a little overwhelmed by the number of components on the screen. Take a look at the VBA Editor within a user-created module, as shown in Figure 4-1.

The VBA Editor has the following components:

□ Three types of modules: Form or report modules, class modules, and standard modules. Each type of component has its own icon. The Project Explorer in Figure 4-1 contains a class module, a source form, and a standard module. The VBA project carries the same name as the current database. If the Project Explorer isn't visible when you display the VBA Editor, press Ctrl+R to display it.

^Microsoft Visual Basic - Chapier4VBAEditor - [DefcugAssertGoS hopping! (Code)] File Edit View Insert Debug Run Tools Add-Ins Window Help is4s'Hjji cs ® i *7 i ► «« a n;ia-a?*i®i

Type a question for help _ fi1 X

H ^ Chapter4VRAEditor (Chapter4VBAEditor)

B ^ Modules

BreakPoinisHowMuchCanWeSpend «££ CallStackDemo «It DateFunctionsl

DebugAssertGoShopping 1 DebugPrintfunWithStringsandNumbers «ü RunToCursorCoffeTime

SteppingThroughCodeGoShopping2 WatchGoShoppingSuits

Properties - DebugAssertGoShoppingl

| DebugAssertGoShopping Module Alphabetic | Categorized ]

Properties - DebugAssertGoShoppingl

| DebugAssertGoShopping Module Alphabetic | Categorized ]

Option Compare Database

Private blnünderBudget As Boolean

Const curBudget - 1QQ0_

"Debug Assert Section Private Sub GoShopping() Dim intSuits As Integer Dim curSuitPrice As Currency Dim curTotalPrice As Currency Dim i As Integer curSuitPrice = 100

intSuits = InputBox("Enter the desired number of suits"

For i = 1 To intSuits curTotalPrice = curTotalPrice + curSuitPrice If curTotalPrice > curBudget Then blnünderBudget — False

Else blnünderBudget = True End If

Debug.Assert blnünderBudget

Next End Sub

Figure 4-1

□ The Properties window: Typically shown in the bottom-left corner of the VBA Editor, the Properties window lists all properties for the currently selected object. The object could be a module or a class module. The Properties window is quite helpful for working with user forms in Visual Basic, but you probably won't use it very often when writing VBA code in Access. However, it is a handy way to see all the properties for an object, so go ahead and check it out. Click the drop-down list and scroll through the alphabetical listing of the properties for that object.

□ The Code window: This is where you actually write your code. By default, the Code window displays all subs and functions within the current module. You can change the display of the Code window and limit it to only the currently selected procedure by selecting Tools O Options and, in the Window Settings frame of the Editor tab, clearing the checkbox, Default to Full Module View. Click OK to save your changes. The Code window has several components of its own, including the Object list on the upper left and the Procedure list on the upper right.

□ The Object list box: Enables you to choose from a variety of objects. When you're writing code inside a standard module, the list box contains only the (General) option. When you're writing code in a class module associated with a form or report, the Object list box contains an entry for every object (text box, combo box, label, and so on) within that form or report.

□ The Procedure list box: Displays different items depending on the type of module you're viewing. When viewing a class module associated with a form or report, the Procedure list box contains an entry for every event associated with the selected object. For example, if you choose a combo box on your form, the Procedure list box contains entries for events such as the Click, BeforeUpdate, AfterUpdate, and LostFocus events, among others.

If you're viewing a standard module, the list box contains an entry for every sub or function in your module, even the ones that you write or rename. Using the drop-down list is a quick way to select the specific procedure you need to edit. You can even add a procedure by clicking on its name.

If you have a module with lots of objects and procedures, scrolling through the Code window to find the desired procedure can be a time-consuming task, so simply select the object and then click the Procedure drop-down box to choose and jump to any available procedure you want in the current module or class module. Subs and functions are listed alphabetically, although your code may not keep them that way. You can also use the Procedure drop-down list to jump directly to the General Declaration section.

In addition to these visible components, there are a number of components you can display to help you write your code and work with the Access 2007 objects. Most of these components are available under the VBA Editor's View menu.

Your Database and VBA Project—Better Together

You might wonder about the correlation between a VBA project and your database. Quite simply, the database with forms and reports is what you see, and the VBA project contains the instructions to make it work. Although you won't see a separate file, there is a VBA project for every database created in Access. The objects in the Project Explorer shown in Figure 4-1 are present no matter where the code is used in your database. Whether you are writing code behind a form or report or in a module, you will see the same objects listed in the Project Explorer.

Was this article helpful?

0 0

Post a comment