Lesson7.16.Creating a Subform


Lesson 7.16. Creating a Subform

Figure 7-28. Subforms are great for working with data in multiple tables with one-to-many relationships. Here the subform displays all the tours made by each employee.


Figure 7-29. Select the table or query and fields that you want to include on your subform.


Figure 7-30. You need to define which fields link the main form and subform.


A subform is a form within a form. The primary form is called the main form, and the form within the form is called the subform, as shown in Figure 7-28. Subforms are especially useful when you want to show data from tables or queries with a one-to-many relationship. For example, a Customer form might have a subform that displays each customer's Orders. Subforms are a great way to display information from a one-to-many table relationship.

The main form and subform are linked so that the subform displays only records that are related to the current record in the main form. For example, when the main form displays a particular customer, the subform displays only orders for that customer.

  1. Open the frmEmployees form in Design view.

    Usually you will want to have the assist you when you add a subform.

  2. Make sure the Control Wizards button on the Toolbox is pressed in.

    The Control Wizard is on whenever its button is depressed.


    Note: If the Toolbox isn't displayed, click the Toolbox button on the Form Design toolbar.

    Before you add a subform, make sure that you have enough room for it on the main form.

  3. Resize the main-form window as needed, so that you have enough room to add the subform.

    You're ready to add the subform! Here's how:

  4. Click the Subform/Subreport button on the Toolbox.

    The mouse pointer changes to a indicating that you can click and drag the subform onto the main form.

  5. Place the pointer just below the DOB field. Click and drag the pointer down and to the right, until the subform covers most of the bottom half of the main form.

    The appears and asks if you want to use an existing form for your subform or if you want to build a new form, using tables or queries. In this exercise we will have the Wizard build us a new form using tables and queries as our subform.

  6. Click Next.

    The next step of the Wizard appears. Here you have to select the table or query and fields that you want to display in your subform, as shown in Figure 7-29. We want our subform to display the tours that each employee has sold, so we will select the qryCustomerTours query as the source for our subform.

  7. Select Query: qryCustomerTours from the Tables/Queries combo box.

    Now you need to select the fields you want to appear in the subform. You must select the related field used to join the main form and subform. This related field must appear on both the main form (where it is called the parent field) and on the subform (where it is called the child field). In this exercise we will use the EmployeeID field to link the two forms.


    Note: It's very important that the underlying tables or queries of the main form and subform have a related field and that the related field appears somewhere on both forms. See Lesson 5.2 in Chapter 5 if you're having trouble understanding this concept.

  8. Select the EmployeeID field and click the button to add the field to the Selected Fields list.

    Now that you've added the most important field that will link the two forms, you can add the remaining fields that you want to appear on the subform.

  9. Add the Date, TourID, CustomerID, and Number of Tickets fields to the Selected Fields list.

    If you're this far, you should be a pro at adding and removing fields using the Wizard. Just make sure that you add the fields in the order specified in Steps 8 and 9.

  10. Click Next.

    The next step in the Wizard, as shown in Figure 7-30, is to define the fields that link the main form and the subform. The Subform Wizard is often smart enough to recognize the field and use it to link the two formsas it is in this exercise. If not, you will have to click the Define my own option and select the two related fields.

  11. Click Next and then click Finish.

    Access creates the subform and adds it to the main form.

  12. Save your changes to the frmEmployees form and display it in Form view.

    The frmEmployees form displays information on each employee and the new subform displays the tours that employee booked.

Subforms created with the Subform Wizard are usually a little rough around the edges and will require a little clean-up work on your part. We'll learn how to modify and work with a subform in the next lesson.

QUICK REFERENCE

TO CREATE A SUBFORM:

  1. DISPLAY THE FORM IN DESIGN VIEW.

  2. CLICK THE TOOLBOX BUTTON ON THE TOOLBAR, IF NECESSARY.

  3. MAKE SURE THE CONTROL WIZARDS BUTTON ON THE TOOLBOX IS SELECTED.

  4. RESIZE THE FORM IF NECESSARY.

  5. CLICK THE SUBFORM/SUBREPORT BUTTON ON THE TOOLBOX.

  6. IN THE FORM WINDOW, CLICK AND DRAG WHERE YOU WANT THE SUBFORM TO APPEAR.

  7. CLICK NEXT.

  8. SPECIFY THE TABLE OR QUERY YOU WANT TO USE FOR THE SUBFORM AND SELECT THE FIELDS YOU WANT TO APPEAR IN THE SUBFORM.

  9. CLICK NEXT.

  10. IF NECESSARY, SPECIFY THE PARENT AND CHILD FIELDS THAT LINK THE MAIN FORM AND SUBFORM.

  11. CLICK FINISH.




CustomGuide Inc - Access 2003 Personal Trainer
Access 2003 Personal Trainer (Personal Trainer (OReilly))
ISBN: 0596009372
EAN: 2147483647
Year: 2005
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net