Previous Page

Connecting Your Application's Objects

Simply getting the individual objects in your application up and running isn't enough. You need to connect the dotstie the objects in your application together into a coherent system that's designed for the particular tasks your users are trying to accomplish. You connect the dots by running macros or event procedures in response to the events that occur on the forms or reports in your application.

The following illustrations show how you can tie separate objects together into a custom system for entering orders. In the illustrated application, the Orders form is the startup form. When you start the application, the Orders form opens automatically. People who take orders can do all their tasks with this one form.

Perform Bulk Updates with Action Queries, Not Code

In database applications, you often need to automate bulk updates of recordsin order to delete and archive inactive customer records, for example. If you're an experienced application developer, you may have found that operations like this can require writing many lines of code. But in a Microsoft Access application, you can usually manipulate sets of records more easily with action queries than with code. The action queriesmake-table, update, append, and deleteare often the most efficient way to change data. Running a predefined delete query, for example, is a much more efficient way to remove a lot of records than looping through records in a Visual Basic procedure. You can define the query graphically in Design view, and then run it from the Database window. You can also write a macro that uses only two actionsSetWarnings and OpenQueryto run the query. Then you can attach the macro to a menu command or a command button, or specify it as the event property setting for any event that occurs on a form.

See Also   For information on how to create action queries that manipulate sets of records, search the Help index for "action queries."

Referring to Objects and Their Values

The preceding section showed that you can tie objects together not only by using one object to open another, but also by passing data from one object to the next. For example, when you click the Print Invoice button on the toolbar in the Orders sample application, you want the order displayed in the Orders form to be selected in the PrintInvoiceDialog form.

How do you do it? Using a macro or an event procedure, you identify the value you want to pass from the open objectusually the value of a control on a form. Because you may have many forms open at one time in your application, Microsoft Access requires a specific syntax structure to identify the control that contains the value you want.

Tip   When you name the tables, fields, and other objects in your database, keep in mind that you'll use these names to refer to the objects elsewhere in your application. Although descriptive names for objects with spaces are easier to recognize than more compact names, they can be difficult to use in expressions, SQL statements, and Visual Basic code. If you are creating a database that uses these advanced features, you may want to use short, consistent names that don't contain spaces and are easier to remember and typefor example, field names such as LastName and Phone.

To refer to an object or a value, you start with an object or a collection of objects and identify each element in turn. A collection groups objects, such as the forms or controls in the current database, as shown in the following illustration.

To refer to an element of a collection, such as the Forms collection, use the ! operator. To refer to the Orders form, for example, use the following expression:

Forms!Orders

Each form contains a collection of controls. To refer to the OrderID control on the Orders form, for example, use the following expression:

Forms!Orders!OrderID

You refer to a control to get, set, or pass its value.

To refer to a property, use the . (dot) operator before the property name. You use this operator before properties, methods, actions, and collections. For example, to refer to the Visible property of the Orders form, use the following expression:

Forms!Orders.Visible

To refer to the OrderID control's Visible property on the Orders form, use the following expression:

Forms!Orders!OrderID.Visible

Tip   If you want help referring to an object or property, use the Expression Builder. With the Expression Builder, you can simply select the object you want from a list, and the Expression Builder writes the reference for you with all the operators in the correct places. To display the Expression Builder, right-click where you want to enter the expression, and then click Build on the shortcut menu.

See Also   For information on objects and collections, see Chapter 5, "Working with Objects and Collections."

Providing Navigation to Tasks and Objects

As an application developer, you determine how people navigate through your applications and complete their tasks. Navigating in a Microsoft Access application usually means moving from control to control within a form or switching between forms.

Navigation within a form   Within a form, navigation from control to control should follow the natural flow of the taskyour users shouldn't have to bounce from the top of the form to the bottom and then back to the top again in order to complete one task. Group controls logically so that users can focus on one area of the form at a time. Define how a user moves from control to control on a form with the keyboard by setting the AutoTab, TabStop, and TabIndex properties. If controls are used to find or filter records, place them in the form header or footer to show they're separate from the other fields in the current record.

Navigation from one form to another form and completing tasks   Nothing says you must use one approach over another to navigate from form to form or to complete a task; however, it's a good idea to provide your users with ways to navigate that are common to other Windows-based applications. It's also a good idea to be consistent within your applicationto do similar tasks on different forms in similar ways. Here are some common navigation devices used in Windows-based applications:

 Command buttons   You can use a command button on a form to carry out a task (for example, saving the current record) or to open another form used for a related task. Command buttons have the advantage of being highly visible. For most common actions, you can create buttons on your forms by using the Command Button Wizard, which creates an event procedure for you. If you want to change or add to what the button does, you can edit the event procedure the wizard creates.

See Also   For information on creating a button with a wizard, search the Help index for "Command Button Wizard."

 Hyperlinks   As an alternative to command buttons that use macros or event procedures for navigation, you can use hyperlinks to navigate to other forms or objects in your database. To create a hyperlink, you set the HyperlinkAddress and HyperlinkSubAddress properties of a label, image control, or command button. Once you set these properties, users can click the text, picture, or button to navigate to the specified object. You can also use hyperlinks to open documents and other files on a local hard drive, a local area network, an internal Web (intranet), or the Internet. If you use hyperlinks to navigate to objects within a Microsoft Access database or to other Microsoft Office documents, your users can use the Web toolbar to navigate between documents and objects they've previously opened with hyperlinks.

See Also   For information on creating a hyperlink, search the Help index for "hyperlinks."

 Menu commands   You can put your own menus and commands on a form or report's menu bar. Commands on menus occupy less room than command buttons on forms, but they're also less visibleusers must open the menu to see them. You can also put common commands on shortcut menus, which users access by right-clicking a form or a control on a form.

See Also For more information on creating a menu bar, see "Creating New Menu Bars, Toolbars, and Shortcut Menus" later in this chapter.

 Toolbar buttons   You'll often want to create buttons that provide shortcuts to the commands on your menu bar. You can add custom toolbar buttons to your toolbars, as shown in the following illustration.

See Also   For information on creating a toolbar and custom buttons, see "Creating New Menu Bars, Toolbars, and Shortcut Menus" later in this chapter.

 Key assignments   You can associate a macro with a key combination so that Microsoft Access runs the macro whenever users press that key combination in your application. This approach is even less visible than menu commandsyour users must know the key assignment in order to use it. However, it has the advantage of being available throughout your application, not just on a particular form. If there's an action your users may want to perform anywhere in your application (for example, printing the current record), you can give them a consistent way to do it by using a key assignment.

See Also   For information on making key assignments, see "Making Key Assignments" later in this chapter.

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender