Create a form that contains a subform (a one-to-many form)

When you are working with relational data (related data that is stored in separate tables) in Access, you often need to view multiple tables or queries on the same form. For example, you might want to see customer data from one table and information about customer orders from another table at the same time. Subforms are a convenient tool for doing this, and Access provides several ways to help you create subforms quickly.

What do you want to do?

Learn about subforms

A subform is a form that is inserted in another form. The primary form is called the main form, and the form that is enclosed in form is called the subform. A form/subform combination is sometimes referred to as a hierarchical form, a master/detail form, or a parent/child form.

Subforms are especially effective when you want to show data from tables or queries that have a one-to-many relationship. A one-to-many relationship is an association between two tables in which the primary key value of each record in the primary table corresponds to the value in the matching field or fields of many records in the related table. For example, you can create a form that displays employee data, and contains a subform that displays each employee's orders. The data in the Employees table is the "one" side of the relationship. The data in the Orders table is the "many" side of the relationship — each employee can have more than one order.

A form that contains a subform

1. The main form shows data from the "one" side of the relationship.

2. The subform shows data from the "many" side of the relationship.

The main form and subform in this kind of form are linked so that the subform displays only records that are related to the current record in the main form. For example, when the main form displays Nancy Freehafer's information, the subform displays only her orders. If the form and subform were unlinked, the subform would display all the orders, not just Nancy's.

The following table defines some of the terminology that is associated with subforms. Access will handle most of the details if you use the procedures in this article, but it is helpful to know what is occurring behind the scenes if you need to make modifications later.

The control that embeds a form into a form. You can think of the subform control as a "view" of another object in your database, whether it is another form, a table, or a query. The subform control provides properties which allow you to link the data displayed in the control to the data on the main form.

Source Object property

The property of the subform control that determines what object is displayed in the control.

A simple display of data in rows and columns, much like a spreadsheet. The subform control displays a datasheet when its source object is a table or query, or when its source object is a form whose Default View property is set to Datasheet. In these cases, the subform is sometimes referred to as a datasheet or subdatasheet instead of as a subform.

Link Child Fields property

The property of the subform control that specifies which field or fields in the subform link the subform to the main form.

Link Master Fields property

The property of the subform control that specifies which field or fields on the main form link the main form to the subform.

Create or add a subform

Use the following table to determine which procedure is most appropriate for your situation.

Recommended procedure

You want Access to create both a main form and a subform, and to link the subform to the main form.

You want to use an existing form as the main form, but you want Access to create a new subform and add it to the main form.

You want to use an existing form as the main form, and you want to add one or more existing forms to that form as subforms.

Create a form that contains a subform by using the Form Wizard

This procedure creates a new form and subform combination by using the Form Wizard. This is also the quickest way to get started if you have not already created the forms that you want to use as the main form or the subform.

  1. On the Create tab, in the Forms group, click Form Wizard.
  2. On the first page of the wizard, in the Tables/Queries drop-down list, select a table or query. For this example, to create an Employees form that displays orders for each employee in a subform, we will select Table: Employees (the "one" side of the one-to-many relationship).

Note: It does not matter which table or query you choose first.

The subform page of the Form Wizard

  • Double-click the fields that you want to include from this table or query.
  • On the same page of the wizard, in the Tables/Queries drop-down list, select another table or query from the list. For this example, we will select the Orders table (the "many" side of the one-to-many relationship).
  • Double-click the fields that you want to include from this table or query.
  • When you click Next, assuming that you set up the relationships correctly before you started the wizard, the wizard asks How do you want to view your data? — that is, by which table or query. Select the table on the "one" side of the one-to-many relationship. For this example, to create the Employees form, we will click by Employees. The wizard displays a small diagram of a form. The page should resemble the following illustration:

    The box in the lower portion of the form diagram represents the subform.

    Note: If the wizard does not ask How do you want to view your data?, that means that Access did not detect a one-to-many relationship between the tables or queries that you selected. The wizard will continue, but Access will not add a subform to the form. You might want to click Cancel and examine your table relationships before you continue.

    Add one or more subforms to an existing form by using the SubForm Wizard

    Use this procedure to add one or more subforms to an existing form. For each subform, you can choose to have Access create a new form or use an existing form as the subform.

    1. Right-click the existing form in the Navigation Pane, and then click Design View.
    2. On the Form Design tab, in the Controls group, click the down-arrow to display the Controls gallery, and ensure that Use Control Wizards is selected.
    3. On the Form Design tab, in the Controls group, click the Subform/Subreport button.
    4. Click on the form where you want to place the subform.
    5. Follow the directions in the wizard. When you click Finish, Access adds a subform control to your form. If you chose to have Access create a new form for the subform instead of using an existing form, Access creates the new form object and adds it to the Navigation Pane.

    Create a subform by dragging one form onto another

    Use this procedure if you want to use an existing form as a main form, and you want to add one or more existing forms to that form as subforms.

    1. In the Navigation Pane, right-click the form that you want to use as the main form, and then click Layout View.
    2. Drag the form that you want to use as the subform from the Navigation Pane onto the main form. Access adds a subform control to the main form and binds the control to the form that you dragged from the Navigation Pane. Access also tries to link the subform to the main form, based on the relationships that have been defined in your database.
    3. Repeat this step to add any additional subforms to the main form.
    4. To verify that the linking was successful, on the Home tab, in the Views group, click View, click Form View, and then use the main form's record selector to advance through several records. If the subform filters itself correctly for each employee, then the procedure is complete.

    If the previous test does not work, Access was unable to determine how to link the subform to the main form, and the Link Child Fields and Link Master Fields properties of the subform control are blank. You must set these properties manually by doing the following:

    1. Right-click the main form in the Navigation Pane, and then click Design View.
    2. Click the subform control one time to select it.
    3. If the Property Sheet task pane is not displayed, press F4 to display it.
    4. In the Property Sheet, click the Data tab.
    5. Click the Build button

    Tip: If you do not see the field that you want to use to link the forms, you might need to edit the record source of the master form or child form to help make sure that the linking field is in it. For example, if the form is based on a query, you should make sure that the linking field is present in the query results.

    Open a subform in a new window in Design view

    If you want to make design changes to a subform while you are working on its main form in Design view, you can open the subform in its own window:

    1. Click the subform to select it.
    2. On the Form Design tab, in the Tools group, click Subform in New Window.

    Change the default view of a subform

    When you add a subform to a form, the subform/subreport control displays the subform according to the subform’s Default View property. This property can be set to the following values:

    When you first create a subform, this property may be set to Continuous Forms or perhaps Single Form. However, if you set the Default View property of a subform to Datasheet, then the subform will display as a datasheet on the main form.

    To set the Default View property of a subform:

    1. Close any open objects.
    2. In the Navigation Pane, right-click the subform and then click Design View.
    3. If the Property Sheet is not already displayed, press F4 to display it.
    4. In the drop-down list at the top of the Property Sheet, make sure Form is selected.
    5. On the Format tab of the Property Sheet, set the Default View property to the view you want to use.

    Note: If the view you want to use is not in the list, make sure the “Allow…View” property for the view is set to Yes. For example, if you want to specify Datasheet as the default view for the form, make sure that the Allow Datasheet View property is set to Yes.

    Add related data to a form without creating a subform

    It is not always necessary to create a separate form object to display related data. For example, if you are working on a form in Layout view or Design view and you drag a table or query from the Navigation Pane to the form, Access creates a subform/subreport control that displays the data in that object. The object’s Default View property determines how the data is displayed. Usually, this is set to Datasheet view, but you can also set the Default View property of a table or query to Single Form, Split Form, or Continuous Forms, giving you more flexibility in displaying related data on forms.

    Note: Changing the Default View property for a table or query determines how it is displayed whenever it is opened, whether you open it from the Navigation Pane or view it in a subform/subreport control. Because changing the view settings for a table can sometimes cause confusion when opening the table from the Navigation Pane, we recommend using a query for this procedure instead of a table.