When you are working with relational data (related data that is stored in separate tables), you often need to lớn view multiple tables or queries on the same size. For example, you might want to see customer data from one table & information about customer orders from another table at the same time. Subforms are a convenient tool for doing this, & Access provides several ways lớn help you create subforms quickly.quý khách hàng đã xem: Cách tạo thành main sub form trong access 2010

What vị you want to lớn do?

Learn about subforms

A subkhung is a size that is inserted in another form. The primary khung is called the main size, & the size that is enclosed in size is called the subform. A form/subform combination is sometimes referred lớn as a hierarchical size, a master/detail form, or a parent/child form.

Bạn đang xem: Cách tạo main sub form trong access 2010

Subforms are especially effective when you want to lớn 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 khung 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 sầu more than one order.


*

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

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

The main khung và subkhung in this kind of khung are linked so that the subkhung displays only records that are related khổng lồ the current record in the main size. For example, when the main form displays Nancy Freehafer"s information, the subform displays only her orders. If the form & subform were unlinked, the subsize 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 lớn know what is occurring behind the scenes if you need to make modifications later.

Term

Definition

Subsize control

The control that embeds a form inlớn a form. You can think of the subform control as a "view" of another object in your database, whether it is another size, a table, or a query. The subkhung control provides properties which allow you khổng lồ link the data displayed in the control lớn the data on the main form.

Source Object property

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

Datasheet

A simple display of data in rows and columns, much lượt thích a spreadsheet. The subform control displays a datasheet when its source object is a table or query, or when its source object is a khung whose Default View property is set khổng lồ Datasheet. In these cases, the subsize is sometimes referred to as a datasheet or subdatasheet instead of as a subsize.

Link Child Fields property

The property of the subform control that specifies which field or fields in the subkhung liên kết the subform lớn the main khung.

Link Master Fields property

The property of the subform control that specifies which field or fields on the main khung links the main khung to the subkhung.

Notes: 

For best results, establish table relationships before following the procedures in this article. This enables Access khổng lồ automatically create the links between subforms and main forms. To view, add, or modify relationships between the tables in your database, on the Database Tools tab, in the Show/Hide group, cliông xã Relationships.

When a subkhung control has a size as its source object, it contains the fields that you place on the form, and it can be viewed as a single khung, continuous khung, or datasheet. One advantage of basing a subform on a form object is that you can add calculated fields to the subkhung, such as * .

Top of Page

Create or add a subform

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

Scenario

Recommended procedure

You want Access lớn create both a main size & a subsize, và to links the subsize khổng lồ the main khung.

Create a khung that contains a subsize by using the Form Wizard

You want to lớn use an existing form as the main khung, but you want Access lớn create a new subsize và add it khổng lồ the main form.

Add one or more subforms lớn an existing form by using the Subform Wizard

You want khổng lồ use an existing size as the main size, và you want khổng lồ add one or more existing forms to that size as subforms.

Create a subsize by dragging one khung onto lớn another

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

This procedure creates a new size & subform combination by using the Form Wizard. This is also the quickest way khổng lồ get started if you have not already created the forms that you want lớn use as the main khung or the subform.

On the Create tab, in the Forms group, cliông chồng Form Wizard

On the first page of the wizard, in the Tables/Queries drop-down danh mục, select a table or query. For this example, to lớn create an Employees khung that displays orders for each employee in a subsize, we will select Table: Employees (the "one" side of the one-to-many relationship).

Double-cliông chồng the fields that you want khổng lồ include from this table or query.

On the same page of the wizard, in the Tables/Queries drop-down các mục, select another table or query from the menu. For this example, we will select the Orders table (the "many" side of the one-to-many relationship).

Double-cliông chồng the fields that you want to lớn include from this table or query.

When you clichồng Next, assuming that you set up the relationships correctly before you started the wizard, the wizard asks How vì you want to lớn 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, lớn create the Employees khung, we will cliông chồng 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 size diagram represents the subkhung.

Note: If the wizard does not ask How vì chưng 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 subkhung to the size. You might want lớn cliông xã Cancel và examine your table relationships before you continue.

At the bottom of the wizard page, select Form with subform(s), và then clichồng Next.

On the What layout would you lượt thích for your subform? page, cliông xã the layout option that you want, & then cliông xã Next. Both layout styles arrange the subform data in rows & columns, but a tabular layout is more customizable. You can add color, graphics, & other formatting elements lớn a tabular subform, whereas a datasheet is more compact, like the datasheet view of a table.

On the next page of the wizard, select a formatting style for the khung, and then clichồng Next. If you chose Tabular on the previous page, the formatting style you choose will also be applied khổng lồ the subsize.

On the last page of the wizard, type the titles that you want for the forms. Access names the forms based on the titles that you type, & labels the subform based on the title that you type for the subkhung.

Specify whether you want to lớn open the form in Form view, so that you can view or enter information, or in Design view, so that you can modify its design, & then cliông chồng Finish.

Access creates two forms — one for the main size that contains the subsize control, & one for the subkhung itself.

Top of Page

Add one or more subforms lớn an existing khung by using the SubForm Wizard

Use this procedure lớn add one or more subforms lớn an existing form. For each subsize, you can choose to have sầu Access create a new size or use an existing khung as the subsize.

On the Design tab, in the Controls group, cliông xã the down-arrow khổng lồ display the Controls gallery, & ensure that Use Control Wizards is selected.

On the Design tab, in the Controls group, click the Subform/Subreport button.

Cliông chồng on the form where you want to place the subform.

Follow the directions in the wizard.

When you cliông chồng Finish, Access adds a subsize control to your khung. If you chose to lớn have sầu Access create a new size for the subkhung instead of using an existing khung, Access creates the new size object và adds it lớn the Navigation Pane.

Create a subkhung by dragging one size onto another

Use this procedure if you want to use an existing form as a main size, & you want khổng lồ add one or more existing forms to that form as subforms.

In the Navigation Pane, right-cliông xã the form that you want to lớn use as the main form, and then clichồng Layout View.

Drag the size that you want to lớn use as the subform from the Navigation Pane onkhổng lồ the main khung.

Access adds a subkhung control lớn the main khung & binds the control lớn the khung that you dragged from the Navigation Pane. Access also tries to lớn links the subsize to lớn the main size, based on the relationships that have sầu been defined in your database.

Repeat this step to add any additional subforms lớn the main form.

To verify that the linking was successful, on the Home tab, in the Views group, cliông chồng View, clichồng Form View, & then use the main form"s record selector lớn advance through several records. If the subform filters itself correctly for each employee, then the procedure is complete.

If the previous chạy thử does not work, Access was unable to determine how khổng lồ liên kết the subform khổng lồ the main khung, và the Link Child FieldsLink Master Fields properties of the subform control are blank. You must set these properties manually by doing the following:

Right-cliông chồng the main size in the Navigation Pane, & then cliông chồng Design View.

Clichồng the subform control one time lớn select it.

If the Property Sheet task pane is not displayed, press F4 lớn display it.

In the Property Sheet, clichồng the Data tab.

Click the Build button

*

next to the Link Child Fields property box.

The Subform Field Linker dialog box appears.

In the Master Fields & Child Fields drop-down lists, select the fields that you want lớn links the forms with, và then clichồng OK. If you are not sure which fields to use, cliông xã Suggest to have Access try to lớn determine the linking fields.

Tip: If you vị not see the field that you want to use to lớn liên kết the forms, you might need to edit the record source of the master size or child khung to lớn 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.

Save the main form, switch to lớn Form view, and then verify that the form works as expected.

xuất hiện a subform in a new window in Design view

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

Cliông chồng the subkhung to select it.

On the Design tab, in the Tools group, cliông xã Subkhung in New Window.

Top of Page

Change the default view of a subform

When you add a subsize khổng lồ a khung, the subform/subreport control displays the subform according lớn the subform’s Default View property. This property can be mix to lớn the following values:

Single Form

Continuous Forms

Datasheet

Split Form

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

To phối the Default View property of a subform:

Cđại bại any open objects.

In the Navigation Pane, right-cliông chồng the subsize và then click Design View.

If the Property Sheet is not already displayed, press F4 khổng lồ display it.

In the drop-down list at the top of the Property Sheet, make sure Form is selected.

On the Format tab of the Property Sheet, set the Default View property khổng lồ the view you want to lớn use.

Note: If the view you want lớn use is not in the menu, make sure the “Allow…View” property for the view is mix to lớn 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 mix lớn Yes.

Save sầu and cthua the subform, and then open the main form to check the results.

Top of Page

Add related data khổng lồ a khung without creating a subform

It is not always necessary to lớn create a separate form object to lớn display related data. For example, if you are working on a size in Layout view or Design view and you drag a table or query from the Navigation Pane lớn the khung, 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 phối to Datasheet view, but you can also mix the Default View property of a table or query to lớn Single Form, Split Form, or Continuous Forms, giving you more flexibility in displaying related data on forms.

Xem thêm: Hướng Dẫn Cách Đăng Ký Học Trên Hocmai Vn, 5 Bước Để Bắt Đầu Học

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 recommover using a query for this procedure instead of a table.