MS Access

Creating a Subform

A subform is a form within a form. The primary form is called the main form, and the form within the form is called the subform, as shown in figure. Subforms are especially useful when you want to show data from tables or queries with a one-to-many relationship.

For example, a Customer form might have a subform that displays each customer's Orders. Subforms are a great way to display information from a one-to-many table relationship.

Subforms are great for working with data in multiple tables with one-to-many relationships. Here the subform displays all the tours made by each employee.

Select the table or query and fields that you want to include on your subform.

You need to define which fields link the main form and subform.

The main form and subform 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 a particular customer, the subform displays only orders for that customer.

  1. Open the frmEmployees form in Design view.

    Usually you will want to have the assist you when you add a subform.

  2. Make sure the Control Wizards button on the Toolbox is pressed in.

    The Control Wizard is on whenever its button is depressed.

    If the Toolbox isn't displayed, click the Toolbox button on the Form Design toolbar.

    Before you add a subform, make sure that you have enough room for it on the main form.

  3. Resize the main-form window as needed, so that you have enough room to add the subform.

    You're ready to add the subform! Here's how:

  4. Click the Subform/Subreport button on the Toolbox.

    The mouse pointer changes to a indicating that you can click and drag the subform onto the main form.

  5. Place the pointer just below the DOB field. Click and drag the pointer down and to the right, until the subform covers most of the bottom half of the main form.

    The appears and asks if you want to use an existing form for your subform or if you want to build a new form, using tables or queries. In this exercise we will have the Wizard build us a new form using tables and queries as our subform.

  6. Click Next.

    The next step of the Wizard appears. Here you have to select the table or query and fields that you want to display in your subform, as shown in figure. We want our subform to display the tours that each employee has sold, so we will select the qryCustomerTours query as the source for our subform.

  7. Select Query: qryCustomerTours from the Tables/Queries combo box.

    Now you need to select the fields you want to appear in the subform. You must select the related field used to join the main form and subform. This related field must appear on both the main form (where it is called the parent field) and on the subform (where it is called the child field). In this exercise we will use the EmployeeID field to link the two forms.

    It's very important that the underlying tables or queries of the main form and subform have a related field and that the related field appears somewhere on both forms. See Creating Relationship Between Tables in Creating Relational Databases if you're having trouble understanding this concept.

  8. Select the EmployeeID field and click the button to add the field to the Selected Fields list.

    Now that you've added the most important field that will link the two forms, you can add the remaining fields that you want to appear on the subform.

  9. Add the Date, TourID, CustomerID, and Number of Tickets fields to the Selected Fields list.

    If you're this far, you should be a pro at adding and removing fields using the Wizard. Just make sure that you add the fields in the order specified in Steps 8 and 9.

  10. Click Next.

    The next step in the Wizard, as shown in figure, is to define the fields that link the main form and the subform. The Subform Wizard is often smart enough to recognize the field and use it to link the two forms as it is in this exercise. If not, you will have to click the Define my own option and select the two related fields.

  11. Click Next and then click Finish.

    Access creates the subform and adds it to the main form.

  12. Save your changes to the frmEmployees form and display it in Form view.

    The frmEmployees form displays information on each employee and the new subform displays the tours that employee booked.

Subforms created with the Subform Wizard are usually a little rough around the edges and will require a little clean-up work on your part. We'll learn how to modify and work with a subform in the next lesson.

To create a subform:

  1. display the form in design view.

  2. click the toolbox button on the toolbar, if necessary.

  3. make sure the control wizards button on the toolbox is selected.

  4. resize the form if necessary.

  5. click the subform/subreport button on the toolbox.

  6. in the form window, click and drag where you want the subform to appear.

  7. click next.

  8. specify the table or query you want to use for the subform and select the fields you want to appear in the subform.

  9. click next.

  10. if necessary, specify the parent and child fields that link the main form and subform.

  11. click finish.