Adding Subforms
So one-to-many relations exists between two tables, you can show the information of “that” and “much” on sides of the relations using a principal form and a subform. For example, the principal form for the relative tables customers and orders could show information on a customer (the “one” side), and the subform could enumerate all the orders which the customer placed (the “many” side). Suppose that you want to create a principal form which includes all the fields of a table with a subform which includes all the fields of another table. As long as there is only one-to-many relations between the tables already defined in the page of relations, the fastest manner to create the form and its subform is using the tool of form. Click on simply the primary table in the square of navigation, and then on the label of creation, in the group of forms, click the button of form. The tool of form creates and shows a form and a subform, each one which contains all the fields of its table of source.
If you want to create a main form and subform that include only
some of the fields in their underlying tables, you can use the Form
wizard. To do so:
-
On the Create tab, in the Forms group, click the Form Wizard
button. -
On the Form wizard’s first page, in the Tables/Queries list,
click the table on which you want to base the form. -
In the Available Fields list, double-click the fields you
want to include in the main form to move them to the Selected
Fields list. -
In the Tables/Queries list, click the table on which you
want to base the subform. -
In the Available Fields list, double-click the fields you
want to include in the subform, and then click Next.
Troubleshooting
If the relationship between the selected tables has not
been defined, Access displays a message. You can click OK to
display the Relationships page, where you can define the
relationship. You will then need to start the wizard
again.
-
On the wizard’s second page, with the primary table and Form
With Subform(s) selected, click Next. -
On the third page, select the layout you want, and then
click Next. -
On the last page, enter the titles you want for your forms,
and with Open The Form To View Or Enter Information selected,
click Finish.
The wizard creates and opens the form and subform. You can
then use normal techniques to modify the form created by the Form
wizard to suit your needs.
If you have already created a main form and you now
want to add a subform to it, you can add a Subform/Subreport
control to the form.
In this exercise, you’ll add a subform to an existing form
in Design view, and you’ll then modify its appearance in Layout
view.
Set Up
-
To give yourself space to work, expand the Detail section until it is about 3 inches
tall. -
On the Design tab, in the
Controls group, display the
Controls gallery, and at the
bottom of the gallery, click the Use
Control Wizards button.
You want this button to be active (orange). -
In the Controls gallery,
click the Subform/Subreport
button, and then drag a box below the Description label and text box controls
in the lower portion of the Detail section.
A white unbound control appears in the form, and the SubForm
wizard starts.
-
With Use existing Tables and
Queries selected, click Next. -
Display the Tables/Queries list, and click Table: Products. -
In the Available Fields
list, double-click the ProductName, CategoryID, QuantityPerUnit, UnitPrice, and UnitsInStock fields to add them to the
Selected Fields list. Then
click Next.
Because there is a relationship between the Products table
and the Categories table that is based on the CategoryID field,
the wizard selects Choose From A List and indicates the
relationship it will use.
Tip
If the wizard can’t figure out which fields are related,
it selects the Define My Own option and displays list boxes in
which you can specify which fields should be related.
-
With Choose from a list
selected, click Next, and then
click Finish to accept the
suggested name for the subform.
Access embeds the Products subform in the Categories form.
The subform control has its own Form Header, Detail, and Form
Footer sections, and can be scrolled independently of the main
form.
-
Above the subform, click the Products subform label, and press the
Delete key. Then switch to Form view.
In this view, the subform looks like a
datasheet.
Tip
This main form and subform are ideal for checking which
products are assigned to which categories and for looking up
information about the products in a category. But if you want to
create a form whose main purpose is data entry, you obviously
need to include all the fields in which the database user will
need to enter information.
-
Right-click the subform, point to Subform, and then
click Form.
In this view, the layout of the subform reflects its layout
in Design view.
-
Repeat step 9 to switch back to Datasheet view. Then, so
that you can modify the layout of the subform, switch to Layout
view. -
Click any cell in the subform, and on the Format tab, in the Font group, click the Font Size arrow, and click 9. -
Point to the right border of the Product Name field name, and when the
pointer changes to a two-headed arrow, double-click to adjust the
column to its widest entry. Then repeat this step for all the
other columns. -
Widen the subform so that Units in
Stock is visible, by dragging the subform’s right
border to the right.
You can now see all the fields of the
subform.
-
Switch to Form view, and scroll through several categories
by using the record navigation bar for the main form.
As each category appears at the top of the form, the
products in that category are listed in the datasheet in the
subform. -
Click the First Record
button to return to the first category (Bulbs). In the subform,
click Bulbs in the Category column to the right of the first
product (Magic Lily).
The arrow at the right end of the box indicates that this is
a combo box. -
Click the arrow to display the list of categories, and then
change the category to Cacti. -
Click the Next Record
navigation button to move to the Cacti category.
Magic Lily is now included in this category. -
Display the Category list
for the Magic Lily record, and
return it to the Bulbs
category.
We need to prevent people from changing a product’s
category. -
Switch to Design view, clicking Yes when prompted to save the form and
the subform. -
In the subform, click the CategoryID combo box control, and then
press Delete.
The CategoryID combo box and its label no longer appear on
the form. -
Save the form, switch back to Layout view, and then adjust
the width of the subform, allowing space for the scroll
bar. -
Switch to Form view, and scroll through the
categories.
The results are shown here.
Clean Up
Close the Categories form, saving your changes. You don’t need
the GardenCompany07 database for the next topic, so you can close
it.
|