Cách Tạo Main Sub Form Trong Access 2010

     
Access for olympiaplus.vn 365 Access 2021 Access 2019 Access năm 2016 Access 2013 Access 2010 Access 2007 More...Less

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 form. For example, you might want to 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, and Access provides several ways to lớn help you create subforms quickly.Bạn đã xem: biện pháp tạo main sub form trong access 2010

What vị you want khổng lồ do?

Learn about subforms

A subform is a form that is inserted in another form. The primary khung is called the main form, and the form that is enclosed in size is called the subform. A form/subform combination is sometimes referred to lớn as a hierarchical form, 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 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 lớn the value in the matching field or fields of many records in the related table. For example, you can create a form 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 more than one order.


*

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

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

The main form & subform in this kind of khung are linked so that the subform displays only records that are related lớn the current record in the main form. For example, when the main khung displays Nancy Freehafer"s information, the subform displays only her orders. If the form và subform 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

Subform control

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

Source Object property

The property of the subform 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 size whose mặc định View property is set lớn Datasheet. In these cases, the subform is sometimes referred khổng lồ as a datasheet or subdatasheet instead of as a subform.

Link Child Fields property

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

Link Master Fields property

The property of the subform control that specifies which field or fields on the main form liên kết the main size to the subform.

Notes: 

For best results, establish table relationships before following the procedures in this article. This enables Access to lớn automatically create the liên kết between subforms và 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 size as its source object, it contains the fields that you place on the form, và it can be viewed as a single form, continuous form, or datasheet. One advantage of basing a subform on a form object is that you can showroom calculated fields lớn the subform, such as * .

đứng top of Page

Create or địa chỉ cửa hàng a subform

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

Scenario

Recommended procedure

You want Access khổng lồ create both a main form và a subform, và to link the subform khổng lồ the main form.

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

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

địa chỉ cửa hàng one or more subforms to lớn an existing khung by using the Subform Wizard

You want to lớn use an existing size as the main form, & you want to showroom one or more existing forms lớn that form as subforms.

Create a subform by dragging one size onto another

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

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

On the Create tab, in the Forms group, click Form Wizard

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

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

On the same page of the wizard, in the Tables/Queries drop-down list, select another table or query from the list. 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 set up the relationships correctly before you started the wizard, the wizard asks How bởi vì 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, lớn create the Employees form, we will click 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 subform.

Note: If the wizard does not ask How do 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 địa chỉ a subform khổng lồ the form. You might want to click Cancel and examine your table relationships before you continue.

At the bottom of the wizard page, select form with subform(s), & then click Next.

On the What layout would you lượt thích for your subform? page, click the layout option that you want, và then click Next. Both layout styles arrange the subform data in rows and columns, but a tabular layout is more customizable. You can địa chỉ color, graphics, & other formatting elements to lớn a tabular subform, 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 form, and then click Next. If you chose Tabular on the previous page, the formatting style you choose will also be applied to 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, & labels the subform based on the title that you type for the subform.

Specify whether you want to 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, and then click Finish.

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

vị trí cao nhất of Page

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

Use this procedure to địa chỉ one or more subforms lớn an existing form. For each subform, you can choose to lớn have Access create a new size or use an existing form as the subform.

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

Xem thêm: Cách Tính Trợ Cấp Thôi Việc Trước 2009 Khi Nghỉ Việc? Những Người Đi Làm Trước Năm 2009 Có Quyền Lợi Gì

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

Click on the form where you want khổng lồ place the subform.

Follow the directions in the wizard.

When you click Finish, Access adds a subform control to your form. If you chose to have Access create a new khung for the subform instead of using an existing form, Access creates the new khung object and adds it khổng lồ 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 form, and you want to địa chỉ cửa hàng one or more existing forms to that khung as subforms.

In the Navigation Pane, right-click the form that you want lớn use as the main form, and then click Layout View.

Drag the size that you want khổng lồ use as the subform from the Navigation Pane onto the main form.

Access adds a subform control to the main form and binds the control to lớn the form that you dragged from the Navigation Pane. Access also tries to links the subform lớn the main form, based on the relationships that have been defined in your database.

Repeat this step to địa chỉ cửa hàng any additional subforms to the main form.

To verify that the linking was successful, on the Home tab, in the Views group, click View, click Form View, và 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 kiểm tra does not work, Access was unable lớn determine how to links the subform lớn the main form, & the Link Child Fields and Link Master Fields properties of the subform control are blank. You must mix these properties manually by doing the following:

Right-click the main form in the Navigation Pane, and then click Design View.

Click the subform control one time to lớn select it.

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

In the Property Sheet, click the Data tab.

Click the Build button

*

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

The Subform Field Linker dialog box appears.

In the Master Fields and Child Fields drop-down lists, select the fields that you want to link the forms with, and then click OK. If you are not sure which fields to use, click Suggest to lớn have Access try to determine the linking fields.

Tip: If you bởi not see the field that you want to lớn use to link the forms, you might need to lớn edit the record source of the master size or child form to help make sure that the linking field is in it. For example, if the size 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 size view, và then verify that the form works as expected.

Open a subform in a new window in kiến thiết view

If you want lớn make kiến thiết changes to lớn a subform while you are working on its main khung in kiến thiết view, you can xuất hiện the subform in its own window:

Click the subform to lớn select it.

On the Design tab, in the Tools group, click Subform in New Window.

đứng top of Page

Change the default view of a subform

When you địa chỉ a subform lớn a form, the subform/subreport control displays the subform according to lớn the subform’s Default View property. This property can be set khổng lồ 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 set the Default View property of a subform khổng lồ Datasheet, then the subform will display as a datasheet on the main form.

To mix the Default View property of a subform:

Close any mở cửa objects.

In the Navigation Pane, right-click the subform & then click Design View.

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

In the drop-down menu at the đứng đầu of the Property Sheet, make sure Form is selected.

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

Note: If the view you want lớn use is not in the list, make sure the “Allow…View” property for the view is set lớn Yes. For example, if you want khổng lồ specify Datasheet as the mặc định view for the form, make sure that the Allow Datasheet View property is set khổng lồ Yes.

Save và close the subform, and then open the main size to check the results.

đứng đầu of Page

Add related data lớn a form 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 thiết kế view và you drag a table or query from the Navigation Pane to the form, Access creates a subform/subreport control that displays the data in that object. The object’s mặc định View property determines how the data is displayed. Usually, this is set lớn 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: Cách Trị Đau Lưng Tại Nhà Mà Bạn Nên Biết, Sở Y Tế Tỉnh Hậu Giang

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.