Tải bản đầy đủ (.pdf) (58 trang)

apress pro access 2010 development phần 2 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.37 MB, 58 trang )

CHAPTER 5  CREATING PIVOTTABLES
116

Figure 5-23. Collapsing each of the categories
This hierarchy of attributes functions much like the CheckedOut By Month field that you added to the
Row area. You can collapse or expand specific values to drill down into the data. Using this approach,
you can add any number of fields to the Column or Row areas
 Tip You added the MediaDescription field to the right of the CategoryDescription field. This caused the
media type to be a secondary level under category. If you had dragged it to the left the CategoryDescription,
then the media type would be the top-level in the hierarchy.
Creating a Field Hierarchy
When planning these multi-level groupings, you should try to use attributes that are logically related.
The date field is the perfect example. Months and weeks are logical subdivisions of a year as are days,
hours and minutes. So it is logical to drill down from year to month to day. However, categories and
media types are orthogonal attributes.
When using multiple fields in a Column or Row area you have to specify the order in which the fields
are used in the hierarchy. In this case the category was first so you can see how a summary for each
category and then drill down to see what media type were loaned from each category. By placing media
type as a sub-group under category, you cannot view a media type across all categories.
Someone may prefer to see how a specific media type was represented in each category. You cannot
accomplish both at the same time. When you have a situation like this where you need to summarize in
both directions, then you should put one attribute in the Row area and the other in the Column area.
A better design would be to move the CheckedOut By Month field to the Filter area and move the
MediaDescription field to the Row area. Make those adjustments in your PivotTable View and it should
look like Figure 5-24.

CHAPTER 5  CREATING PIVOTTABLES
117

Figure 5-24. Rearranging the fields in the PivotTable View
Notice that you can simultaneously see both category and media type summaries, as well as how the


other attribute was represented in the total. Click the dropdown button next to the CheckedOut By Month
field in the Filter area. The dialog box shown in Figure 5-25 will appear which you can use to filter the
view to include only specific months or days, for example.


Figure 5-25. Selecting the dates to include
Remember, the Filter field(s) define the set of records that are included in the PivotTable View. By
placing the CheckedOut By Month field here, you can control the time period that you want to analyze.
Date fields are often a good candidate for a Filter field. However, if you want to see an attribute tracked
over time, you will generally use the appropriate date field in Row area.
In the current database schema, there is a single-level categorization of items. For a large library you
would probably want multiple levels, such as the Dewey Decimal system. The Dewey Decimal system
defines three main levels of organization, which are referred to as classes, divisions, and sections. It is
called a decimal system because there are 10 classes that each has 10 divisions, which, in turn, has 10
sections each. Thus there are 100 divisions and 1000 sections.
If you were to implement a structure such as this, then this would be an excellent candidate for
using a multi-level field hierarchy in your PivotTable View. Look for other fields that logically fit into a
hierarchy. The Author and Title fields are another good example.
CHAPTER 5  CREATING PIVOTTABLES
118
Using the PivotChart View
Once you have defined a PivotTable View, you can easily turn this into a graphical presentation. Select
the PivotChart View button from the lower right corner or select the PivotChart View link from the View
button in the ribbon. The default settings will probably look something like Figure 5-26.


Figure 5-26. The initial PivotChart View
Configuring a PivotChart View
This is a fairly complex PivotTable with several values to chart. Each value is displayed as a bar in this bar
graph. To see what each bar represents, you’ll need to display the legend. Click the Legend button in the

Design tab of the ribbon. The legend will look similar to Figure 5-27.

CHAPTER 5  CREATING PIVOTTABLES
119

Figure 5-27. The PivotChart legend
The problem with this chart is that it is trying to display too many values. In a bar chart, the Column
field(s) are used to define the bar legend (a color for each column) and the Row field(s) are used for the X
axis. Click the “Switch Row/Column” button, which will transpose the Row and Column fields. The
resulting chart should now look like Figure 5-28.


Figure 5-28. Switching the Row and Column fields
This is a little bit easier to follow. For the Classics category, for example, you can now see how each
media type contributes to the summary totals Loans, Fees, and Overdue. Notice the dropdown button
next to the CategoryDescription and MediaDescription fields. You can use these to filter the records that
CHAPTER 5  CREATING PIVOTTABLES
120
are included in the chart. Click the CategoryDescription dropdown and unselect all of the categories
except Classics, as shown in Figure 5-29.
Figure 5-29. Selecting only the Classics category
Changing the Chart Type
With the data limited to a single category, you may want to use a different type of chart. Click the
“Change Chart Type” button in the ribbon. Select the Line type shown in Figure 5-30.
Figure 5-30. Selecting a Line chart
The resulting chart is shown in Figure 5-31.
CHAPTER 5  CREATING PIVOTTABLES
121

Figure 5-31. The PivotChart View using a line chart

You can see from this chart that overdue fees have been charged for Hardback books, but no fees
have been charged for DVD videos or paperback books even though there have been overdue items of
these media types.
 Note You will likely have different values in your database. My purpose here is to give you an example of how
to read the chart.
Now display the PivotTable View by selecting the PivotTable link from the View button. Notice that
this view now looks very different from where you left it. This illustrates a very important point: The
PivotTable View and the PivotChart View use the same Row, Column, and Filter area definitions. If you
change this configuration, it is automatically changed in the other, as well. More subtle and perhaps
more significant, you can only have one PivotTable/PivotChart View for each table or query.
 Caution You can define only one PivotTable/PivotChart View for each table or query. The PivotTable and
PivotChart views share the same configuration; when you change one, the other is also updated.
CHAPTER 5  CREATING PIVOTTABLES
122
Exporting a PivotTable View to Excel
Access 2010 allows you to export a PivotTable or PivotChart view to Excel. This will allow you to
manipulate the data and view using the Excel application. As I mentioned earlier, the concepts are
similar but the implementation is different.
From the PivotTable View, click the CategoryDescription dropdown and select all categories. Save
the database and then click the “Export to Excel” button in the Design tab of the ribbon. You will see a
pop-up window warning you about possible compatibility issues, shown in Figure 5-32. Click the OK
button.


Figure 5-32. Compatibility warning
Access will then open the Excel application and create a PivotTable worksheet, as shown in Figure
5-33. Notice the similarities to Access 2010 and the differences.


Figure 5-33. The PivotTable View in Excel

The most obvious difference is that the multi-field hierarchy is not as evident. Notice that the
CheckedOut By Month field is replaced by a set of fields, one for each of the data components such as
Year, Quarter and Month.
The raw data is also imported in the second worksheet, as shown in Figure 5-34.
CHAPTER 5  CREATING PIVOTTABLES
123

Figure 5-34. The underlying Excel data
Excel uses the raw data that the PivotTable is based on. In order for the PivotTable to work, all of the
raw data must be imported, as well. You can use this approach create a new PivotTable and/or chart in
Excel using the data from Access and the PivotTable features from Excel.
 Caution When you export data to Excel, this is a one-time operation. Subsequent changes to the data in Access
are not pushed to Excel using manual or automatic means. The only way to update Excel is to re-import the data
from Access.
Summary
PivotTables and charts are an excellent way to present and analyze data from your Access database. You
configure a PivotTable or chart by adding one or more fields from the underlying table to each of the
following areas:
• Data area: This contains the values that ate presented and normally use an
aggregate function to summarize the data.
• Row and Column: Fields in these areas define the attributes that are used for
slicing and dicing the data.
• Filter: (Optional) Fields in the Filter are used to limit the data that is being
presented.
If you specify multiple Row or Column fields, they are added in hierarchical fashion allowing you to
drill down from one attribute to the next. The built-in By Month and By Week date fields are an excellent
example of this.
The most significant limitation to keep in mind is that you can only have one PivotTable/PivotChart
View for each table or query. The PivotTable and PivotChart views share the same configuration values;
when you change one, both are updated. If you want to create additional PivotTables, refer to Chapter 4

for instructions on how to create a query that joins several tables.
You can also export the data and PivotTable to Excel and manipulate the presentation using the
Excel tools.
125
P A R T 3



Creating Forms and Reports
In Part 2, you created your database, designed the tables, and wrote data macros to implement many of
the business rules. You created queries to provide de-normalized views into your data, and even
designed a pivot table to analyze the data that is being collected. Although still a bare-bones solution,
your database fulfils all the basics requirements. In Part 3, however, you’ll put flesh on those bones and
create a rich user experience.
Chapter 6 will show you how to use the built-in form templates to easily generate many of the
simpler forms. Chapters 7, 8, and 9 demonstrate how to build more complex forms from scratch. This
will demonstrate a lot of handy tricks for creating useful forms. Chapter 10 will show you how to create
menus and navigation pages that will guide the user to the provided features. In Chapter 11, you’ll focus
on the visual, branding aspects of your application, including themes, graphics, and background images.
Finally, in Chapter 12, I’ll show how to create reports, which are essentially forms that are designed
for print output.

C H A P T E R 6



127
Standard Forms
One of the really great features of Access is that it can generate forms for you based on your table design.
This is another good reason for starting with a well thought out data schema. In this chapter, I’ll

demonstrate several common form patterns that will satisfy many of your UI requirements. These are
created using standard form templates or the Form Wizard, and do not require writing any code.
This chapter will also provide a foundation that applies to both these simple forms as well as
advanced custom forms. In subsequent chapters, I’ll show you how to design forms yourself and use
macros and VBA code to implement more complex solutions. These custom forms are based on the
same general principles that I will explain in this chapter.
Creating a Single Form
We’ll start by creating a simple form to display records in the Category table.
Using the Form Wizard
From the Create tab of the ribbon, click the Form Wizard button. In the first dialog box of the Form
Wizard, select the Category table, and then move the CategoryCode and CategoryDescription fields to the
Selected Fields list, as shown in Figure 6-1.
CHAPTER 6 ■ STANDARD FORMS
128

Figure 6-1. Selecting the record source for the form
Each form is based on a single table or query, and the first step in designing a new form is to specify
which will be the source for this form. After selecting a table or query from the dropdown list, all the
available fields will display. You can then select all of the fields to be included on the form or just a
subset of them.
■ Caution If your form will be used to add records, you should generally use a table for the source, rather than a
query. Queries usually supply only a subset of fields or rows and often use multiple tables. All of these
characteristics are problematic when inserting records. That is not to say that you cannot use a query; however, if
you use a query for a form that allows new records, make sure that every required field is included on the form or
has a default value assigned. Otherwise, the form will not be able to save a new record.
Notice that the primary key was not included in this form; this is a common design practice. As I
discussed in Chapter 2, the primary key is a surrogate key generated by the database engine to ensure
uniqueness. In many cases it is not meaningful to the end user, so there is no need to display it in that
scenario.
In the second dialog, you’ll need to choose how you want the fields organized on the form. As shown

in Figure 6-2, there are four layout options available. I will explain these choices later in this chapter.
Because this form only has two fields on it, just leave the default option of Columnar and click the Next
button.
CHAPTER 6 ■ STANDARD FORMS
129

Figure 6-2. Choosing the desired layout option
In the final dialog box, enter Category for the title of this form. For the radio options, select the first
option, “Open the form to view or enter information,” as shown in Figure 6-3.


Figure 6-3. Specifying the form name
The new form should look like Figure 6-4.
CHAPTER 6 ■ STANDARD FORMS
130

Figure 6-4. The initial Category form
In this simple form, a single record is shown. Notice the record navigator at the bottom of the form.
You can use this to move to the first, previous, next, or last record in the table. The last control in this
group will display a blank record for adding a new category.
Using the Available Views
Just like with tables and queries there are several views available when working with a form. To display
all the view options, perform the following steps:
1. Click the Design View button in the ribbon. If the Property Sheet is not
currently visible, click the Property Sheet in the Design tab of the ribbon. There
is a dropdown list at the top of the Property Sheet that you can use to select the
object that you want to view. You can select the Form, any of the sections such
as Form Header or Detail, or one of the individual controls. Because there are a
lot of properties, these are grouped into separate tabs.
2. Select the Form object and the Format tab. There are several properties that

control what views are allowed for this form. The Form Wizard generates the
form with the Datasheet View turned off. Change this value to Yes as shown in
Figure 6-5.


Figure 6-5. Allowing the Datasheet View
CHAPTER 6 ■ STANDARD FORMS
131
Notice that the View options in the ribbon now has four options, which are shown in Figure 6-6.


Figure 6-6. The allowed views
Every form usually has two modes that it can be viewed in; Form View and Datasheet View. You’ve
already seen the Form View. In this view, a single record is displayed and a record navigator control is
used to move through the available records. Each field is represented by an appropriate data-bound
control, such as Text Box, Check Box, or ComboBox depending on the type of data contained in the field.
In addition, Label controls are used to annotate what each data control is for. All of these controls are
arranged on the form.
Select the Datasheet View from the ribbon, which should look like Figure 6-7.


Figure 6-7. Displaying the Datasheet View
The Datasheet View of a form looks very much like the table when it is displayed in the Datasheet
View. In this view, you can re-order the columns by selecting one and then dragging it to the desired
position. You can change the width of the column by clicking the gridline and dragging it left or right.
The column headings are defined by the Datasheet Caption property. Let's change them now, with the
following steps:
1. Select the CategoryCode column and then, in the Property Sheet, select the
Other tab.
CHAPTER 6 ■ STANDARD FORMS

132
2. The default value of the Datasheet Caption property is blank. When this is the
case, the heading text is defined by the Control Source property, which is the
associated column of the underlying table. The Caption that was defined for
that column will be used as the column heading. Enter Code for the Datasheet
Caption property.
3. Likewise, for the CategoryDescription column, enter Description for the
Datasheet Caption property.
■ Note The configuration of the Datasheet View is independent of the Form View. Changing the order of the
columns, the column widths, or the column headings has no effect on the layout of the Form View. If you allow
both views of your form, you should check the layout of the Datasheet View and make sure it looks like you want it
to.
The Layout View and Design View are used to design and modify the form. You will use these views
extensively in subsequent chapters as you build custom forms. The Design View allows you configure all
aspects of the form. The Layout View is an interesting and useful view. It looks like the Form view and
displays the fields just like the Form View would, but it also allows you to add and rearrange controls. It
is essentially a WYSIWYG editor.
Sorting the Records
To sort the records, click the dropdown icon next to the Description column and click the Sort A to Z
link, as shown in Figure 6-8.
Figure 6-8. Sorting the Datasheet View
CHAPTER 6 ■ STANDARD FORMS
133
This will display the records in alphabetical order based on their descriptions. This change is
reflected on both views. You can verify this by switching to the Form View; the records should be in the
same order as in the Datasheet View. Go to the Design View and select the Data tab of the Property
Sheet. Notice the Order By property has been populated, as shown in Figure 6-9.


Figure 6-9. The Order By property

Using Split Forms
All forms are based on these two display modes, Form View and Datasheet View. However, there are also
two variations of these that are a composite of both views. The first is called Split Form, which shows
both views simultaneously. The other is a Continuous Form, which combines features from both the
Datasheet and Form views.
The Split Form is a really useful feature, especially for smaller tables. It enables you to see all the
records in a compact Datasheet View. At the same time, a single record can be viewed and modified in
the more user-friendly Form View. A good example would be for longer text fields. The Datasheet View
will probably only show the first few words but the Form View can display the entire text on multiple
lines, with scrollbars if necessary.
Generating the Media Form
You’ll now create a Split View for the Media table with the following steps:
1. Close the Category form and any other tabs that may be open.
2. Select the Media table in the Navigation pane.
3. From the Create tab in the ribbon, click the More Forms dropdown and then
click the Split Form link, as shown in Figure 6-10.
CHAPTER 6 ■ STANDARD FORMS
134

Figure 6-10. Creating a Split Form
This will generate a form based on the current table, which should look like Figure 6-11.


Figure 6-11. The initial Split Form
CHAPTER 6 ■ STANDARD FORMS
135
■ Tip Creating a form using the Split Form button does not start the Form Wizard, which allows you to configure
how the form will be generated. Instead it creates a form based on the currently selected table and includes all the
available fields. You need to make sure you have the correct table selected in the Navigation pane before using
this option. If you create a form from the wrong table, just delete it and try again.

Modifying the Form Fields
The form was generated using all the fields. You’ll need to remove the MediaID field from both forms; to
do so, right-click the MediaID column in the Datasheet View (the lower portion of the form) and click the
Delete link. This will also remove this column from the Form View.
Just like with the previous form, you configure the layout of both the Form View and Datasheet View
separately. We’ll start with the Form View.
1. The label for the RenewalsAllowed field is a little long (How many times can a
loan for this type of item be renewed?). To make this form look better, you’ll
use the Layout View to resize the controls. From the Design tab of the ribbon,
click the Layout View button.
2. Select the label control for the RenewalsAllowed field, which will highlight the
control with an orange border.
3. Resize this control so it is about half as wide and twice as high. The form
should look like Figure 6-12.

Figure 6-12. Resizing the label control
4. Now adjust the column heading in the Datasheet. To do that, use the Property
Sheet and select the RenewalsAllowed control.
5. In the Other tab, enter Renewals for the Datasheet Caption property.
6. Save the form and select the default form name Media when prompted.
Go back to the Form View to view the final version, which is shown in Figure 6-13.
CHAPTER 6 ■ STANDARD FORMS
136

Figure 6-13. The final Media form layout
■ Tip If you remove a field from one of the views, it is automatically removed from both. This happened, for
example, when you removed the MediaID field. Normally, both views will include the same fields. However, there
are a couple of ways around this if you want to have different fields in each view. To remove a field from the Form
View only, use the Property Sheet to set the Visible property to No. The field will still be included in the Datasheet
View, but both the field and its associated label will be hidden from the Form View. To remove a field from only the

Datasheet View, simply resize the column to a 0 width.
Figure 6-14 shows the form with OverdueFee field removed from the Form View and the
RenewalsAllowed field from the Datasheet View.
CHAPTER 6 ■ STANDARD FORMS
137

Figure 6-14. Updated form with fields removed
■ Tip In the Format tab of the Property Sheet, you can set the Split Form Orientation property. The default value is
Datasheet On Top, but you can change this to put the Datasheet View on the top if you prefer. You can also put it
on the right or left.
Using Continuous Forms
The other variation is called a Continuous Form. It’s like a Datasheet View in that all records are
displayed sequentially. However, each record is displayed as a collection of controls just like the Form
View. It is basically the Form View, except instead of only displaying a single record, the form controls
are repeated over and over again. Obviously, this won’t work very well for long forms, but it can be useful
for relatively short forms.
Generating the InventoryItem Form
You create a Continuous Form just like the Form View that you created at the beginning of the chapter.
Then you have to the change the Default View property to Continuous Form. You’ll now create a
Continuous Form for the InventoryItem table following these steps:
CHAPTER 6 ■ STANDARD FORMS
138
1. From the Create tab of the ribbon, click the Form Wizard button. In the first
dialog box, select the InventoryItem table and include all the available fields as
shown in Figure 6-15.


Figure 6-15. Selecting the InventoryItem table
2. In the second dialog box, select the Tabular layout, as shown in Figure 6-16.
The Tabular layout is the best choice if you’re planning to use a Continuous

Form. It was designed specifically for that purpose.


Figure 6-16. Using the Tabular layout
CHAPTER 6 ■ STANDARD FORMS
139
3. In the final dialog box, leave the default form title as InventoryItem, as shown
in Figure 6-17. Select the second radio option, which is to modify the form’s
design. This will cause the new form to be opened in the Design View instead
of the Form View.


Figure 6-17. Specifying the form title
The form should now be displayed in the Design View and look similar to Figure 6-18.


Figure 6-18. The initial form in Design View
The first thing you’ll probably noticed is that the Label controls are in the Form Header rather that
the Detail section. In a Continuous Form, only the Detail section is repeated for each record. The Form
Header and Form Footer are only displayed once. Typically you will want to put the labels in the header
to save “space” in the repeated section. This will help you keep the Detail as thin as possible. However,
you don’t have to do this; you can also put some or all of the labels in the Detail section if you want.
CHAPTER 6 ■ STANDARD FORMS
140
■ Tip You cannot drag a control from one section of a form to another. If you want to move a label from the Form
Header section to the Detail section, right-click the control and click the Cut link. Then right-click in a blank area of
the Detail section and click the Paste link.
Also, the default layout has all of the fields on a single row, much like a datasheet. Again, you don’t
have to keep them that way; you can arrange them in any way you want to. In general, however, you will
want to keep the Detail section thin, especially if you expect numerous records to be included.

USING THE MULTIPLE ITEMS TEMPLATE
Access often provides multiple ways to accomplish the same thing. Instead of using the Form Wizard, you
could have selected the
InventoryItem table in the Navigation pane and then click the Multiple Items
button in the Create tab of the ribbon, as shown in Figure 6-19.

Figure 6-19. Using the Multiple Items button to create a form
This would create a new form that is very similar to the one created through the Form Wizard.
Designing the InventoryItem Form
It is helpful to think through how a form will be used; specifically, is it for view only or will it be used to
add or update records. The first two forms that you created were designed to view, add, change, and
delete records from the Category and Media tables. The InventoryItem form will be primarily used for
viewing but will allow some restricted updates.
In Chapter 4, you created an append query that is used to insert a record into the InventoryItem
table. This query takes a single parameter, the ItemID, and all the other fields will have default values.
CHAPTER 6 ■ STANDARD FORMS
141
You will eventually add a button to an Item form that will execute this query to add a copy of that item to
the inventory. Also, the ItemID should not be editable once the record is created. Likewise, the Status
field is controlled by the data macros that you implemented in Chapter 3. The only user-modifiable
fields on the form are Condition and Comment.
This InventoryItem form will be used as a subform, which I will explain later in this chapter. It will
be used to show the InventoryItem records for a specific item. Since the associated item is inferred by
the context in which this subform is placed, you can remove the ItemID from this form. Also, the
CurrentLoanID field is not necessary for this purpose. The Status field was generated as a ComboBox
control to allow the desired value to be selected. Because this should not be editable, you’ll want to
replace this control with a TextBox control. This will display the current value without implying to the
user that it can be modified.
Modifying the Form Fields
From the Design View, make the following changes:

1. Delete the ItemID ComboBox control. Notice that its associated label was not
also removed. Because they are in different sections, they are not linked
together. You’ll need to manually delete the Label control as well.
2. Delete the Status ComboBox control (leave the label).
3. From the Design tab of the ribbon, click the TextBox button. Then click in the
Detail section, holding the mouse button down and dragging it to form a
rectangle in the same location and about the same size as the previous
dropdown list control. In the Other tab of the Property Sheet, enter Status for
the Name property. In the Data tab, select Status from the Control Source
property.
4. The new Textbox control will have added an associated Label control. Delete
this, as it is not needed.
5. On both the InventoryItemID and Status controls, set the Locked property to
Yes. You can find this in the Data tab of the Property Sheet.
6. Change the Caption property of the InventoryItemID label to ID and set the
Text Align property to Left.
7. Remove the CurrentLoanID ComboBox control as well as its associated label.
8. Rearrange the controls in both the Detail and Form Header section to remove
the empty spaces.
The form in Design View should look similar to Figure 6-20.
CHAPTER 6 ■ STANDARD FORMS
142
Figure 6-20. The completed Design View
Select the Form object in the Property Sheet and then select the Format tab. Notice that the Default
View property is already set to Continuous Form. This is set by the Form Wizard whenever you use the
Tabular layout.
■ Note You can change the Default View to Single Form if you want to see what the form would look like as a
Single Form. This does not affect the design or layout of the form. Make sure you change it back to Continuous
Form if you do change the Default View.
Save the form changes and select the Form View from the ribbon. The final form should look like

Figure 6-21.

×