Connecting Your Application’s Objects

Simply getting the individual objects in your application up and running isn’t enough. You need to connect the dots—tie 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 records—in 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 queries—make-table, update, append, and delete—are 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 actions—SetWarnings and OpenQuery—to 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 object—usually 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 type—for 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 task—your 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 application—to do similar tasks on different forms in similar ways. Here are some common navigation devices used in Windows-based applications: