|
Relational database allows you to separate data in objects so that these objects can hold different pieces of information and make data available to other objects that need it. Sooner or later, users constantly need information held by one form while they are working on another form. There are various ways you can solve such a problem. One solution is to "embed" one form or report into another form or report.
A subform is a form that you position inside of another form. In order to "include" one form into another form, both objects must have a relationship, as we have learned in the past. The form that is hosting the other form is the parent. The form that is added to the parent is called the child form. The parent form must have a primary key that "links" or relates it to a foreign key in the child form. This foreign key of the child object must be of the same data type as the primary key on the parent object.
|
|
Microsoft Access >= 2000 Automatic Subforms
|
|
|
Microsoft Access 2000 and later versions allow you to quickly create a form that has a subform already. Before using this feature, you should make sure that the table you want to use has a sub datasheet, which can be verified by the + button on the left of its records:
As you may know already, a sub datasheet exists if a relationship had previously been created between two tables. Using this feature, to create a form that is automatically equipped with a subform, after selecting the table on the Database Window, on the Database toolbar, you can click the arrow of the New Object button and select
AutoForm.
|
|
Practical Learning: Automatically creating a Form With a Subform
|
|
- If you are using Microsoft Access 97, skip this exercise.
- Open the Book Collection1 database
- From the Tables section of the Database Window, double-click the Categories table to open it in Datasheet View

- After viewing it, close it but make sure it is sill selected in the Tables section of the Database window
- On the Database toolbar, click the arrow of the New Object button and select AutoForm:


- To close and save the new form, click its Close button

- When prompted whether you want to save the objects, click Yes
As done with all the other forms we have used so far, there are two main techniques you can use to create a subform. Keeping the tradition of its many wizards, Microsoft Access provides the means of automatically creating a subform. There is no particular design you need to follow for a subform. A subform is primarily a form. The only true rule you must follow is that one of its fields must have a field, usually its primary key, that will allow it to be related to a field on the form in which it would be added. If you are not using the Form Wizard, you can still design a form and make it a subform.
You can also create a form equipped with a subform, using the Form Wizard. To do this, in the first page of the wizard, select the parent form. Select the necessary fields for the form (report). In the Tables/Queries combo box, select another table or query that holds the fields for the child form (report). Continue with the wizard. The wizard will verify that the tables or queries share a relationship. If they do not, you would receive an error or the result of the form (report) would be unpredictable.
|
Practical Learning: Automatically Creating an Auto-Subform
|
|
- Open the Bethesda Car Rental1 database
- On the main menu, click Insert -> Form
- On the New Form dialog box, click Form Wizard and click OK
- In the Tables/Queries combo box, select CarCategories
- Click the Select All button
- In the Tables/Queries combo box, select Table: Cars
- Double-click the following fields to select them: Make, Model, CarYear, NbrOfDoors, and TransmissionID
- Click Next

- On the second page, make sure the Form With Subform radio button is selected. Otherwise, select it

- On the 3rd page, make sure the Datasheet radio button is selected. Otherwise, click it. Click Next
- On the 4th page, click the Standard style and click Next
- Change the name of (main) form to CarsByCategory
- Change the name of the sbfCars

- Click Finish
- After viewing the form, switch it to Design View
- Move the controls around and change their position to appear as follows:

- Save, preview, and close the form
|
|