Cách tạo main sub form trong access 2010

When you are working with relational data (related data that is stored in separate tables), you often need lớn view multiple tables or queries on the same form. For example, you might want lớn 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, và Access provides several ways lớn help you create subforms quickly.quý khách hàng đã xem: Cách chế tác main sub size trong access 2010

What vì you want to do?

Learn about subforms

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

You watching: Cách tạo main sub form trong access 2010

Subforms are especially effective sầu when you want to lớn show data from tables or queries that have sầu 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 khổng lồ the value in the matching field or fields of many records in the related table. For example, you can create a size that displays employee data, and contains a subsize 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 form and subsize in this kind of size are linked so that the subsize 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 khung and subkhung 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 lớn know what is occurring behind the scenes if you need lớn make modifications later.

Term

Definition

Subkhung control

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

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 & columns, much lượt thích a spreadsheet. The subsize control displays a datasheet when its source object is a table or query, or when its source object is a size whose Default View property is mix khổng lồ Datasheet. In these cases, the subform is sometimes referred to lớn as a datasheet or subdatasheet instead of as a subsize.

Link Child Fields property

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

Link Master Fields property

The property of the subform control that specifies which field or fields on the main form links the main form lớn the subsize.

Notes: 

For best results, establish table relationships before following the procedures in this article. This enables Access lớn automatically create the liên kết between subforms & main forms. To view, add, or modify relationships between the tables in your database, on the Database Tools tab, in the Show/Hide group, click Relationships.

When a subform control has a khung as its source object, it contains the fields that you place on the size, & it can be viewed as a single size, continuous khung, or datasheet. One advantage of basing a subsize on a form object is that you can add calculated fields khổng lồ the subform, such as * .

Top of Page

Create or add a subform

Use the following table khổng lồ determine which procedure is most appropriate for your situation.

Scenario

Recommended procedure

You want Access to create both a main size & a subsize, & lớn links the subsize to the main size.

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

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

Add one or more subforms to an existing size by using the Subkhung Wizard

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

Create a subsize by dragging one size onlớn another

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

This procedure creates a new size & subsize 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 subsize.

On the Create tab, in the Forms group, cliông xã Form Wizard

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

Double-click the fields that you want to lớn include from this table or query.

On the same page of the wizard, in the Tables/Queries drop-down danh mục, select another table or query from the các mục. 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 phối up the relationships correctly before you started the wizard, the wizard asks How bởi 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 size, we will cliông xã by Employees. The wizard displays a small diagram of a khung. The page should resemble the following illustration:


*

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

Note: If the wizard does not ask How vày you want lớn 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 lớn the form. You might want khổng lồ cliông chồng Cancel và examine your table relationships before you continue.

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

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

On the next page of the wizard, select a formatting style for the size, & then clichồng Next. If you chose Tabular on the previous page, the formatting style you choose will also be applied to lớn the subform.

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, and labels the subkhung based on the title that you type for the subform.

Specify whether you want 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 kiến thiết, and then click Finish.

Access creates two forms — one for the main form that contains the subform control, và one for the subsize itself.

Top of Page

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

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

On the Design tab, in the Controls group, click the down-arrow lớn display the Controls gallery, and ensure that Use Control Wizards is selected.

See more: Download Photo Crop Editor V, Download Ifoxsoft Photo Crop Editor 2

On the Design tab, in the Controls group, clichồng the Subform/Subreport button.

Cliông chồng on the khung where you want to lớn place the subsize.

Follow the directions in the wizard.

When you cliông xã Finish, Access adds a subkhung control to lớn your size. If you chose khổng lồ have Access create a new khung for the subsize instead of using an existing size, 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 khổng lồ use an existing form as a main khung, & you want to add one or more existing forms khổng lồ that khung as subforms.

In the Navigation Pane, right-cliông chồng the size that you want to use as the main khung, và then clichồng Layout View.

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

Access adds a subsize control khổng lồ the main form và binds the control to lớn the size that you dragged from the Navigation Pane. Access also tries to lớn link the subkhung lớn the main khung, based on the relationships that have been defined in your database.

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

To verify that the linking was successful, on the Home tab, in the Views group, click View, click Form View, & 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 demo does not work, Access was unable lớn determine how to liên kết the subkhung khổng lồ the main size, and the Link Child Fields and Link Master Fields properties of the subkhung control are blank. You must phối these properties manually by doing the following:

Right-click the main size in the Navigation Pane, and then click 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, click the Data tab.

Cliông xã the Build button

*

next khổng lồ the Link Child Fields property box.

The Subkhung Field Linker dialog box appears.

In the Master Fields and Child Fields drop-down lists, select the fields that you want lớn liên kết the forms with, and then cliông chồng OK. If you are not sure which fields to use, cliông xã Suggest khổng lồ have Access try khổng lồ determine the linking fields.

Tip: If you vày not see the field that you want lớn use lớn links the forms, you might need to edit the record source of the master khung or child form 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 size, switch khổng lồ Form view, và then verify that the size works as expected.

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

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

Cliông xã the subkhung lớn select it.

On the Design tab, in the Tools group, clichồng Subsize in New Window.

Top of Page

Change the mặc định view of a subform

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

Single Form

Continuous Forms

Datasheet

Split Form

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

To mix the Default View property of a subform:

Cchiến bại any open objects.

In the Navigation Pane, right-clichồng the subform & then clichồng Design View.

If the Property Sheet is not already displayed, press F4 to 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 to lớn the view you want to lớn use.

Note: If the view you want to use is not in the danh mục, make sure the “Allow…View” property for the view is phối lớn Yes. For example, if you want khổng lồ specify Datasheet as the mặc định view for the khung, make sure that the Allow Datasheet View property is mix khổng lồ Yes.

Save & cchiến bại the subkhung, và then open the main khung lớn check the results.

Top of Page

Add related data lớn a size without creating a subform

It is not always necessary to create a separate khung object khổng lồ 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 lớn the size, 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 mix the Default View property of a table or query lớn Single Form, Split Form, or Continuous Forms, giving you more flexibility in displaying related data on forms.

See more: 5 Lý Do Hóng Cực Mạnh Gia Đình Nhà Croods (Lồng Tiếng), Gia Đình Croods: Kỷ Nguyên Mới (Lồng Tiếng)

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.


Chuyên mục: Chia sẻ