Home Access 2002 Forms Forms and Subforms   |   Search MS Office A-Z   |   Search Web Pages/ Design A-Z

Forms and Subforms

The subform is an extremely useful device that is surprisingly easy to create.

The Northwind.mdb sample database has a form that uses not one but two subforms. Launch Access and click Help, Sample Databases, and Northwind Sample Database. In the Database window, click Forms and double-click Customer Orders.

At the top of the form is the customer’s name and country. The first datasheet below is a subform that shows order details for that customer. The form itself tells you how the second datasheet works. Each time you move to a different record in the top datasheet, the bottom datasheet changes to display the specifics of that order.

At the bottom of the form, click the Next Record button a few times and briefly review the data for each customer. Each time you move to a new customer record, the top datasheet changes to display orders for that particular customer, and the bottom datasheet changes to show details for the selected order from that customer.


 Review Relationships

The key to understanding how subforms work is grasping the relationships among the various tables. In the Database window, click Tools and Relationships. The Customers table has a one-to-many relationship with the Orders table through the CustomerID field. In other words, for each customer there are many orders, and a single order never goes to more than one customer.

At the same time, the Orders table has a one-to-many relationship with the Order Details table through the OrderID field. Each order can have many separate order detail records, while any particular detail record applies only to one order.

In the Customer Orders form, we see this hierarchy of relationships at work: One customer (at the top of the form) has many orders (in the top datasheet), and each order (a single record in the top datasheet) has many details (in the bottom datasheet).

Close the Northwind database. Let’s quickly make our own form/subform (as such forms are often called) that will show order details for each order.


 Create A Form

Navigate to the Northwind.mdb database in the Samples subfolder of your Microsoft Office folder. Copy Northwind.mdb to the Clipboard and paste it in another folder. Rename the file CopyOfNorthwind.mdb, which you can now open and use without worrying about changing the original file.

In the Database window (at full size), click Tables, double-click Orders, click the arrow button next to the New Object button, and click Form. After choosing Design View, click OK. Maximize the window. Click Save from the File menu and name the form "Orders SC." Move your pointer to the bottom of the Detail section. When the pointer becomes a double-arrow crossbar, drag it down so the section is 3 inches long. Using the same technique, widen the form to 6 inches.

From the field list, drag the OrderID field to the Detail section and drop it 1 inch from the left and two rows of dots from the top.

 Add The Subform

At this point, only the form and the Database window should be open. Click Window and Tile Vertically. In the Database window, click Tables and select Order Details. Drag your selection from the Database window to the Detail section of the form; drop it 0.5 inches from the left and 1 inch from the top and then release the mouse button. The SubForm Wizard will then open.

In the first dialog box, you’ll see that Access has already figured out what you want to do: Show order details for each record in the Orders table using OrderID as the field with matching data. Click Next. Change the name of the subform to "Order Details SC” and click Finish.


 Review The Form & Subform

Maximize the form and click the View button. Access has added the subform (even though it’s cut in half—we’ll fix that in a minute).

Using the navigational buttons at the bottom of the form itself, click the Next Record button a few times. As you move from order to order, you can see that the OrderID at the top matches those in the datasheet.

Notice that the subform has its own set of navigational buttons. Because most or all orders have only a few details, all of them are in view, and the extra set of buttons is largely superfluous. But the navigational buttons in the subform would prove useful if there were many detailed records for each order.


 Subform Details

Now let’s do some work on the subform. Note the unnecessary label: Order Details SC. Click the View button, select the control (it’s the small label above the subform), and press DELETE.

Right-click inside the middle of the subform control, click Properties, and choose the Format tab. (The title of the property sheet should be Subform/Subreport.) Edit the Width to 4.5 inches (see Figure 1). Next, click the View button and close the property sheet. You can now readjust column widths with your mouse so that all columns are in view (see Figure 2).

Close the form. With both forms selected, click Yes in the dialog box. In the Database window, click Forms and open Order Details SC. As you can see, when you create a form/subform, the subform itself becomes a form in its own right, with full status in the Database window.

You can now exit CopyOfNorthwind.mdb without saving your changes and exit Access.