Sub-forms

A sub-form is a form that has been inserted into another form, called the main form. Typically a sub-form will contain several entries for every record in the main form. For example, a form that has been created from a table of employees, in which each employee appears only once, may contain a sub-form based on a table of holidays. Obviously each employee will have several holiday dates, which can all be viewed together in the sub-form attached to the main form. Thus each page in the main form will display the details of a single employee and all the holiday dates associated with this employee.

It is clear from the above that a pre-condition of associating a main form and a sub-form is that you have two tables with at least one field in common. In our example, the Employees table and the Holidays table would both usually contain an ID field for each employee. If you can establish a relation between two tables based on a common field then you can create a sub-form within a main form.

Assuming you have already created a main form from a table, the subform is created as follows:

  1. Open up the main form (in our example, this would be Employees).

  2. Click on the Design button to go into Form Design mode rather than Browse mode.

  3. If more space is required, use your mouse to extend the main area of the form.

  4. Select Subform from the Insert menu.

  5. Use your mouse to position the displayed cross and then drag till you have created a suitably sized outline box (you can alter the size and position later if this is not right).

  6. On releasing the mouse button, the Insert Field Wizard is displayed.

  7. You can use an existing form or an existing table/query/relation to create the subform. In this case, click on Existing Tables/Queries/Relations and then click on Next.

  8. The subform can be laid out either vertically or horizontally: choose Fields arranged down subform (the effect is shown in the Preview box at the right) and then click on Next.

  9. On this page you choose the appropriate join and browse fields:

  1. Click Next to go to the next page, where you can select the fields in the Hidden Fields column that you want be shown in the subform. Click on the Show button to move these to the Visible Fields column. (In our example, these should include at least the field with information on holidays.)

  2. Click on Finish to complete the subform and return to Design mode.

Now that you have created the subform within the main form, you can view the results by clicking on the Browse button. There are two ways of doing this: for each entry in the main form view all the attached subform entries one by one by clicking on the subform browse arrows, or view all the subform entries together.

To choose which, do the following:

  1. Return to Design mode, select the subform, right-click and select Properties from the displayed menu.

  2. In the Form Properties box, select Single Form to view the subforms one by one for each main form, or Continuous Form to see all the subforms displayed together for each main form.

In our example, several holiday dates will be displayed for each employee.