Online Book Reader

Home Category

Access Cookbook - Ken Getz [7]

By Root 1795 0

Figure P-2. Use the Tools → Options dialog from within VBA to turn on the Require Variable Declaration option

If you are asked to create a new function or subroutine, the simplest way to do so is to use Insert → Procedure. For example, if the solution instructs you to enter this new procedure:

Function SomeFunction(intX as Integer, varY as Variant)

you can use Insert → Procedure to help you create the function.

Click OK in the Add Procedure dialog, as shown in Figure P-3. Access will create the new procedure and place the cursor in it. For the example in Step 3, you must also supply some function parameters, so you'll need to move back up to the first line and enter intX as Integer, varY as Variant between the two parentheses.

Figure P-3. The Add Procedure dialog helps you create a new function or subroutine

How Do I Import an Object?


In this book's solutions, you'll often be asked to import an object from one of the sample databases. Follow these steps:

With your database open on the Access desktop, select the database window by pressing F11. (If you're in the VBA editor, first press Alt+F11 to get back to Access.)

Choose File → Get External Data → Import, or right-click on the database window and choose Import.

Find the database from which you want to import a module, and click Import.

In the Import Objects dialog, select all of the objects you'd like to import, moving from object type to object type. When you've selected all the objects you want to import, click OK.

If a solution instructs you to import a module from one of the sample databases that you've already imported (for a different solution), you can ignore the instruction. Any modules with matching names in the sample database contain the exact same code, so you needn't import it again.

How Do I Create an Event Macro?


Programming in Access often depends on having macros or VBA procedures reacting to events that occur as you interact with forms. You'll find that most of the solutions in this book use VBA code rather than macros, because code provides better control and safety. But occasionally a macro is the right tool for the job. To create a macro that will react to a user event, follow these steps:

Select the appropriate object (report, form, or control) and make sure the properties window is displayed.

Choose the Event properties page on the properties window, or just scroll down the list until you find the event property you need.

Click on the ellipsis (...) button to the right of the event name, as shown in Figure P-4. This is the Build button; it appears next to properties window items that have associated builders. In this case, clicking the Build button displays the Choose Builder dialog, shown in Figure P-5. Choose the Macro Builder item to create a new macro. (If you don't often use macros, in the Tools → Options dialog, on the Forms/Reports page, you can choose to "Always use event procedures". The Build button will immediately take you to the Visual Basic Editor.)

Figure P-4. Press the Build button to invoke the Choose Builder dialog

Figure P-5. The Choose Builder dialog: choose Macro Builder for macros and Code Builder for VBA

Give the macro a name, so Access can save it and place its name in the properties window. You can always delete it later if you change your mind. Give your new macro the name suggested in the solution, and fill in the rows as directed. When you're done, save the macro and put it away.

Once you're done, you'll see the name of the macro in the properties window, as shown in Figure P-6. Whenever the event occurs (the Change event, in this case), Access will run the associated macro (mcrHandleChange).

Figure P-6. The properties window with the selected macro assigned to the OnChange event property

If you want to call an existing macro from a given event property, click on the drop-down arrow next to the event name, rather than the Build button. Choose from the displayed list of available macros (including macros that exist as part of a macro group).

Return Main Page Previous Page Next Page

®Online Book Reader