 |
Sectional and Tabbed Forms |
|
|
|
A 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 or report while they are
working on another form or report. 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 (or a subreport) is a form (or a report)
that you position inside of another form (or another report). In order to
"include" one form (or report) into another form (or report), both objects
must have a relationship. The form (or report) that is hosting the other
form (or the other report) is the parent. The form (or report) that
is added to the parent is called the child form (or child report). The
parent object must have a primary key that "links" or relates it to a
foreign key in the child object.
|
Microsoft Access Automatic Subforms
|
|
Microsoft Access provides various techniques you can
use to create a subform. Probably the simplest technique consists of using
a wizard. This allows you to specify the table or query that is holding
the parent records, followed by the table or query that has the child
records. Of course, both lists must be able to communicate through the
primary key of the parent list and a foreign key in the child list.
To generate a form that contains a subform, start the
Form Wizard. In the first page of the wizard, in the Tables/Queries combo
box, select the parent object and, in the Available Fields list, select
the fields you want to display on the form. Then, in the Tables/Queries
combo box again, select the child list. In the Available Fields list,
select the fields that the subform should display. Continue with the
wizard. In the second page, you must be able to identify the relationship
that will control the link between both list. In other words, you must be
able to identify the primary key from the parent list and the foreign key
from the child list. Once this is clear, you can continue. In the third
page of the wizard, you will decide how you want the subform to display,
as a tabular list or as a datasheet. After making this decision, you can
continue. The fourth page of the wizard allows you to select a preset
design you want to apply to the form (and the subform). There is no
particular design you need to follow for a subform. After making the
selection, you can click Next and Finish.
|
Practical
Learning: Automatically Creating an Auto-Subform
|
|
- Start Microsoft Access
- Open the Video Collection3 database you started in in
Lesson 35.
If you did not create it, from the resources that accompany these
lessons, open the Video Collection3a database
- On the Ribbon, click Create
- In the Forms section, click More Forms -> Form Wizard
- In the Tables/Queries combo box, select Table: Directors
- Click the Select All button

- In the Tables/Queries combo box, select Table: Videos
- Double-click the following fields to select them: Title,
CopyrightYear, Length, and Rating

- Click Next
- In the second page, click by Directors (or make sure it is
selected)

Click Next
- In the 3rd page, click the Tabular radio button

- Click Next
- In the 4th page of the wizard, accept the name of (main) form as
Directors.
Change the name of the subform to sbfVideos

- Click Finish
- After viewing the form, close it
|
Sub-Forms and Sub-Report Design
|
|
The Form Wizard allows you to quickly create a
sub-form. This would be fine if you just want to review records associates
from one table to another. As effective as it can be, it can also provide
some functionality you do not need and do not want. Besides that, the Form
Wizard imposes some aspects you must simply accept. The alternative it to
design your own subform, and you have various options:
- You can create a form using the Form Wizard, then design a
separate subform and add it to the form
- You can create both the form and its subform using the Form
Wizard, then modify the design
- You can design both the form and the subform separately, then join
them
All these techniques indicate that you should be able
to configure the functionality of the subform as it relates to the form
that will host it. When designing a subform, you must also specify its
appearance. You have the options as datasheet, tabular, or just regular
form.
As mentioned in Lesson 8, a form can appear as a
datasheet like a table. This makes the form appear with columns and
records whose intersections are cells. You can also create a subform that
display as a datasheet.
To create a subform that would display as a datasheet,
start the form in Design View. In the Property Sheet, set its Default View
to Datasheet. When designing the form, you can position the controls
anywhere because their positions would not be obvious on the form. Also,
the sizes of the labels and fields would not show on the form. When
designing the form, you work in Design View. The Datasheet View appears
only if you change the view. Although you can use the different sections
of a form, for a Datasheet form, the sections would appear only in the
Design View, not in Datasheet View.
When adding controls or other objects to a subform (or
a form) that would display in Datasheet View. Only the controls you
display in the Detail section would appear when the subform comes up. The
objects in the Queries sections would not appear. You can use this feature
to your advantage. For example you can add unbound controls in a Form
Footer section to hold some values or perform some calculations, then
refer to those controls in the parent form.
|
Practical
Learning: Using a Datasheet Subform
|
|
- Open the Music Collection1 database
- On the Ribbon, click Create
- To create a new form, in the Forms section, click Form Design
- Access the Property Sheet for the form and set its Record Source
to AlbumTracks
- Save the form as sbfTracks
- Reduce the width of the form so its right border is at 23/8
- If the Field List is not displaying, on the Ribbon, click Design
and, in the Tools section, click Add Existing Fields
.
In the Field List, click TrackNumber
- Press and hold Shift
- Then click TrackLength
- Release Shift. This selects the TrackNumber, the TrackTitle, and
the TrackLength fields
- Click and drag the selected fields to the Detail section of the
form. There is no need to change the positions or locations of the
labels and text boxes
- Click the TrackNumber text box
- On the Ribbon, click Format and click the Right button

- Double-click the button at the intersection of both rulers
- In the Property Sheet, click Format and change the following
properties:
DefaultView: Datasheet Record Selectors: No
Navigation Buttons: No
- To switch the form to Datasheet View, right-click its title bar
and click Datasheet View
- Right-click the # column header and click Field Width...
- Click Best Fit
- Click the Track Title column header
- On the Ribbon, click Home
- In the Records section, click More Field Width
- Type 24.75
- Click OK
- On the table, right-click the Length column header and click Field
Width
- Type 7.85 and press Enter
- Save and close the subform
- In the Navigation Pane, under the Forms bar, double-click
MusicAlbums to open it

- After viewing it, switch it to Design View and click the Tracks
tab on the tab control
- In the Controls section of the Ribbon, click the More button and
make sure the Use Control Wizards option is selected
(or click it). To add the new subform, in the Controls section of
the Ribbon, click the Subform/Subreport button
- Click the body of the Tracks tab on the form

- In the first page of the Subform/Subreport Wizard, in the list of
forms, click sbfTracks and click Next
- In the second page of the wizard, make sure the Choose From A List
radio button is selected. Also, in the Select One Of These Links list
box, make sure the relationship is based on the AlbumID field.
Click Next
- Accept the suggested name of the subform as sbfTracks and click
Finish
- Click the label that was added to the subform and press Delete (to
remove the label). You may also need to move the subform and resize
the tab sheet

- Save and preview the form
- Close the form
A variant of the datasheet is the tabular form. As you
may know already, a tabular form displays its records in groups. Instead
of showing one record at a time, the form can show as many records as its
size allows:

This type of form is referred to as continuous because
the records are displayed continuously in the same view. If the number of
records is not too high, all of them would display. If there are more
records than the form's size can allow to view, some records would be
hidden. To view them, you can use the scroll bars.
To create a continuous form, you can use the Form
Wizard where, in the second page of the wizard, you would select the
Tabular option. To create a continuous form in Design View, set its
Default View to Continuous Forms:

Unlike the datasheet form, a continuous form can be
equipped with, and can display, other sections than the Detail section in
Form View. This means that you can include additional fields in the
Queries sections. Based on this, a typical design of a continuous form
consists of creating some labels in the Form Header section and
positioning their corresponding controls under them but in the Detail
section. The fields can be horizontally aligned and adjacent each other.
You should (strongly, if not must) avoid including Memo and OLE Objects
(pictures, linked documents, etc) in a continuous form because such fields
may take too much space. This would deceive the purpose of the continuous
form.
|
Practical
Learning: Designing a Subform or Subreport
|
|
- Open the Ceil Inn1 database you
started in Lesson 34
and continued in the previous lesson. If you did not create create it,
from the resources that accompany our lessons, open the Ceil Inn4
database
- On the Ribbon, click Create
- To create a new query, in the Queries section, click Query Design
- In the Tables tab of the Show Table dialog box, double-click
Customers, Occupancies, and Rooms
- On the Show Table dialog box, click Close
- In the lists of fields, from the Occupancies list, double-click
DateOccupied
- In the Rooms, double-click RoomNumber
- In the Occupancies list, double-click RateApplied, PhoneUse, and
InternetFee
- In the Customers list, double-click CustomerID

- Save the query as CustomerRoomUse and close it
- On the Ribbon, click Create
- To create a new form, in the Forms section, click Form Design
- Using the Property Sheet, set its Record Source to CustomerRoomUse
- Save the form as sbfCustomerRoomUse
- Change the following properties:
Default View: Continuous Forms
Record Selector: No Navigation Buttons: No
- Right-click the form and click Form Header/Footer
- In the Controls section of the Ribbon, click the Label

- Click just under the Form Header bar on the left side:

- Type Date Occupied and press Enter
- If the same way, add labels with the captions Room #,
Rate Applied, Phone Use, and
Internet Use
- Design and apply a font available to you. Here is an example:

- To display the Field List, on the Ribbon, click Add Existing
Fields
- From the Field List, drag DateOccupied and drop it somewhere in
the Detail section. Click its label to select it and press Delete to
remove it
- In the same way, add the other fields
- Align them next to each other under their corresponding labels.
Design them as you see fit under the Part label but in the Detail
section

- In the Controls section of the Ribbon, click the Text Box
and click under the Form Footer
- Using the Property Sheet, change its characteristics as follows:
Name: txtTotalRateApplied Control Source:
=Sum(RateApplied) Format: Fixed
- Add another text box to the form footer section, change its
characteristics as follows:
Name: txtTotalPhoneUse
Control Source: =Sum(PhoneUse) Format: Fixed
- Add another text box to the form footer section, change its
characteristics as follows:
Name: txtTotalInternetFee
Control Source: =Sum(InternetFee) Format: Fixed

- Click the Form Footer bar and, in the All tab of the Property
Sheet, set its Visible value to No
- Save, preview and close the sub-form
- In the Navigation Pane, under Forms, double-click Customers
- After viewing the form, switch it to Design View
- In the Navigation Pane, under Forms, drag sbfCustomerRoomUse and
drop it on the form under the Notes memo
- Add four text boxes under the subform and design them as follows:
 |
| Label's Caption |
Text Box Name |
Format |
Control Source |
| Phone Use: |
txtPhoneUse |
Fixed |
=[sbfCustomerRoomUse].[Form]![txtTotalPhoneUse] |
| Internet Use: |
txtInternetUse |
Fixed |
=[sbfCustomerRoomUse].[Form]![txtTotalInternetFee] |
| Rate Total: |
txtRateTotal |
Fixed |
=[sbfCustomerRoomUse].[Form]![txtTotalRateApplied] |
| Net Pay: |
txtNetPay |
Fixed |
=Nz([txtPhoneUse])+NZ([txtInternetUse])+Nz([txtRateTotal]) |
|
- Save and preview the form:
- Close the form
|
|