In this section, we'll explore the world of creating reports and forms in Access, and how to configure your reports to appear as you want and modify forms to have appropriate controls, subforms, lookup fields, and even buttons.
A report is a summary of data retrieved from the database, usually presented in a more user-friendly format than the simple datasheet generated by a query. Reports can group information, perform additional calculations, and generate nicely-formatted printed copies. However, reports cannot be used for data entry or editing (that requires a form).
Reports are always based on a table or, more usually, a query, which means they are always compiled using current data and if the underlying query changes, so does the report. Some examples are shown below:
The Report Wizard enables a report to be constructed from choices, meaning you can ensure only relevant data are presented in the most useful format.
To construct a report you will need to:
Once the Report Wizard is open, there are a number of steps you will go through to create your report. These steps may not exactly match the sequence below as what you see does depend on your data, but this gives an example:
The report will be generated based on your choices, but it is unlikely to look exactly how you want and will require further editing.
There are four views (like design and datasheet view for tables and queries) available for reports:
Report | The compiled and formatted report intended for onscreen viewing, but not for printing. |
---|---|
Print Preview | A preview showing how the report would appear when printed using the current settings for paper and margins. You can adjust page and paper settings and print from this view. |
Layout | The compiled and formatted report with the ability to adjust the width, height, and position of the data fields. |
Design | Full control of the structure, content, and functionality of the report is only available from the more complex Design review. |
When creating and editing reports you will mostly use the Design and Layout views, choosing Report and Print Preview to check the onscreen and printed views.
In order to edit a report you need to use the Design view, which is divided into sections with each section corresponding to a portion of the compiled report.
An grouped report has a more complex structure in the Design view than an ungrouped report. We will cover each separately first before looking at some other features of report editing.
An ungrouped report created with the wizard will have 5 sections, though the Report Footer. may be set to zero height. The sections are:
Report Header | Content is shown only once at the top. |
---|---|
Page Header | Content is shown at the top of every page. |
Detail | This section displays the data and is repeated for each record found in the underlying query or table. |
Page Footer | Content is shown once at the foot of every page. |
Report Footer | Content is shown once only at the end of the report, at the end of the data (before the Page Footer). |
A grouped report has a more complex structure including a Group Header above the Detail section. In some circumstances you may also need a Group Footer below the Detail (e.g. for subtotals).
When a grouped report is compiled the Group Header - Detail - Group Footer sequence is repeated for each different value found in the field chosen for grouping.
Report Header | Content is shown only once at the top. |
---|---|
Page Header | Content is shown at the top of every page. |
[Group]* Header | For each group of records, displays data relevant to that group |
Detail | This section displays the data and is repeated for each record found in the underlying query or table. |
[Group]* Footer | For each group of records, displays data relevant to that group. |
Page Footer | Content is shown once at the foot of every page. |
Report Footer | Content is shown once only at the end of the report, at the end of the data (before the Page Footer). |
*The group header and footer display the name of the field chosen for grouping.
The Hide Duplicates property is particularly useful as it will prevent the display of repeated values in the detail section. To modify a field property:
Every object on a report is referred to as a control. Most data from the underlying query will be presented using a pair of controls:
The names of Text Boxes and Labels can be confusing, as a Label contains text you can edit and a Text Box links to the data in the underlying query or table. It can also be difficult to spot which is which in Design view so you might need to change to Layout view to check which one contains the data.
All controls have a collection of properties that define size, appearance, and behaviour. Many of these can be configured from the ribbon (Home > Text > Formatting) and the size adjusted using the sizing handles.
If the text box and label for a field are in the same report section they can be moved together.
If a Yes/No field is included in the underlying data, the Wizard will render this as a checkbox. When the intention is to print the report, this is the only other control type that is likely to be used.
A report may also included graphic elements. For example, lines may be added to enhance readibility or other images may be included. These are inserted in the appropriate section via Report Design Tools > Design > Controls.
The Report Wizard automatically places two Text Box controls on the Page Footer section containing functions to display system information on printing.
=Now() displays the current date and time.
="Page " & [Page] & " of " & [Pages] Displays the current page number [Page] and total number of page numbers [Pages], using concatenation, so it shows in the format Page 2 of 6
Grouping and sorting have their own set of controls that can be used even if you did not initially choose grouping in the wizard. This allows you to:
To use these additional controls:
The alyout of a completed report can be further ocntrolled by modifying the properties for each section. To view and modify properties for a section:
Of particular interest are the properties that allow you to allow a region to grow/shrink with the amount of content, ensure all content in a section is kept together on the same page, and force the start of a new page before/after the section.
When designing for printed output, bear in mind you will be limited to paper width. Use the controls of the Report Design Tools Page Setup tab and rule to adjust as needed.
The report width (edited using report properties or rule) plus page margins must not exceed 21cm (A4 portrait) or 29.7cm (A4 landscape). Access will warn if the report width is too great for the defined paper and margin sizes.
Totals and subtotals can be added to a grouped report using the Group and Total configuration controls (see above), but may also be added using an unbound Text Box control - meaning one that is not tied to a specific field in the underlying data. An expression is then entered that references field names using appropriate syntax.
To insert an unbound text box:
Remember that a Text Box contains data (in this case, an expression to be calculated) and a Label contains text as a label for that data.
Placing the control in the Group Header or Footer will perform the calculation of the records in each group.
Placing the control in the Report Footer will perform the calculation for all records in the report.
Expressions can be constructed using functions, field names, text, and numbers.
Excel users should note that Access and Excel functions are similar but may have different names.
Some tips for creating expressions:
Function | Example |
---|---|
Sum (total) | =Sum([FieldName]) |
Average | =Avg([FieldName]) |
Count | =Count([FieldName]) |
Max | =Max([FieldName]) |
Min | =Min([FieldName]) |
Arithmetic | = [Cost] * [Quantity] * 10% |
Combined calculations | =Avg([Cost] * [Quantity]) |
Including text | ="Average is: " & Avg([FieldName]) |
To control the display of numerical data you must modify the Text Box properties:
Text data can be concatenated (using an ampersand &) in an expression and can be manipulated with text functions. Additional text, including spaces, must be enclosed in double quotes.
Text format | Example |
---|---|
Simple concatenation with space between | =[Forename] & " " & [Surname] |
Fields combined with text | ="First name is: " & " " & [Forename] |
Initial letter from Forename field | =Left([Forename],1) |
Surname, Initial | =[Surname] & ", " & Left([Forename],1) |
Expression | What it does |
---|---|
=Now() | Current date and time |
=Date() | Current date |
=If([Price]>25, "too big", [Price]]) | Immediate IF - works like Excel's IF |
=[Page] | Current page number |
=[Pages] | Number of pages in report |
=[Name] | Name of report object (not report title) |
In Access, forms are used to provide more user-friendly on-screen interaction with data than is possible with tables and queries. Forms can include familiar controls such as drop-down lists and buttons. They interact with the underlying tables, but do not themselves store data. Ideally they need:
Forms can be designed to display one record at a time, a continuous list of records, or one main record (on the main form) along with continuous records of related data (on the subform).
When you choose a query to create a form, the Form Wizard analyses your data relationships and offers various ways of displaying the data. Some choices will generate a main form/subform combination, some a single form. If the end result does not meet your needs, delete and re-run the wizard.
When constructing a form:
The Wizard process will depend in part on your data. Read the questions posed at each step in the wizard and respond as you think appropriate to define the form.
As with other objects, there are several views, with some dependent on your data structure::
Form | The on-screen form with which users will enter, view, and edit data. |
---|---|
Datasheet | Data presented in the format of a datasheet. It looks the same as a table/query datasheet. |
Layout | The on-screen form with the ability to adjust the width, height, and position of the data fields - use for editing layout. |
Design | Full control of the structure, content, and functionality of the form is only available from the more complex Design review. |
Forms based on a simple data structure may also offer you:
Detailed editing of forms is carried out in Design view, with layout possible to be adjusted in Layout view. Editing form design is very similar to editing report design.
From design view has three sections: Header, Detail, and Footer. The content of the Header and Footer generally remain fixed, and record data displays in the Detail section.
Every item on a form is referred to as a control. Data from an underlying table or query is usually presented as a pair of controls:
The names of Text Boxes and Labels can be confusing, as a Label contains text you can edit and a Text Box links to the data in the underlying query or table. It can also be difficult to spot which is which in Design view so you might need to change to Layout view to check which one contains the data.
There are two options for forms in Design view: single and continuous record view. Single record view always presents one record at a time, whereas when choosing continuous records the detail section is repeated for each available record in the underlying data, filling the available space between the form header and footer.
In form design view you can edit a range of elements, as with reports. Form controls can be edited using the same methods as report controls and any other controls needed can be added to the form. For example, you might add more fields to the form using features like a combo box (see Lookups below). To add more controls:
To add text to the form, choose the Label control and click where you want the text to be positioned - don't use a Text Box control as this expects data.
Several form features are controlled using the form properties. To view the form properties, in Design view ensure that no controls or sections of the form are selected and then view the properties list by going to Form Design Tools > Design > Tools > Property Sheet.
Some useful form properties are:
Property | Tab | Use |
---|---|---|
Default view | Format | Switch between single record and continuous records. There are other options here too. |
Record selectors | Format | Turn off/on the grey box to the left of the detail section that allows you to select a record. |
Record source | Data | Indictates that table/query on which the form is based. |
Pop up | Other | Form will always remain on top, even when not active. |
There are also some useful field properties. When you have the property sheet open, select a section or field to see its properties. Some useful ones are:
Property | Tab | Use |
---|---|---|
Format | Format | Use in conjunction with Decimal Places property to control display for numerical data. |
Show date picker | Format | When set to For Dates (default) date fields display a small calendar to choose the date. |
One feature of a relational database is that a single record in one table is frequently related to several records in a related table or tables. A subform allows you to view both at the same time and is configured so the subform keeps in-step with the main form (so it changes as you cycle through records).
In order for the main form and subform to synchronise, both must share a common field, usually the primary key of the data on which the main form is based.
There are two ways that a form-subform combination can be constructed, either by using the Wizard in one step or by creating the main form and subforms separately and then combining. Whichever approach you use, two new forms will be present in the object list.
To create both the main form and subform at the same time using the wizard:
To make a form and subform without using the wizard, first decide which fields will be required on the main form and which on the subform, making sure a common field is available to synchronise records - this will generally be the primary key for the main form data.
Create any queries on whic hthe forms will be based and then contruct the main form and subform. Note that the main form must be in single record view and the subform must display either continuous records or datasheet view.
To combine the main form and the subform, make sure the main form design allows space for the subform, and then:
You can also add the subform to the main form using the Subform/Subreport item in the Controls gallery.
Once this is done, you can tweak the layout using the design view of the main form. Any other subform settings must be configured by opening the subform directly in design view. One change you may want to make is to remove the subform navigation controls: you can edit these and scroll bars from the subform format properties.
As the subform is synchronised with the main form it is in effect filtered so it is possible to perform additional summary calculations just on these records. These are usually placed in the footer region of the subform using an unbound text box. Expressions are constructed using functions, field names, text, and numbers. Unbound controls are inserted from the controls gallery using the same method as for reports.
Simple display or entry into underlying tables may not always be the most useful approach from a user perspective. Access provides a lookup features that improves usability by allowing the user to choose a value from a list that corresponds to a different underlying value.
The Lookup feature also exists for table fields, but when used in a table it is inherited by subsequent queries, reports, and forms. This can cause confusion with queries, as the value displayed isn't the value stored in the field. However, lookups are useful when added just to forms.
Consider a form that requires the entry of a department code. Many users would prefer to choose from a list of department names rather than remember the codes. Using a Lookup, the field would obtain its value by "looking it up" in a list, and this would be presented to the user. The list can be:
If you are replacing an existing Label/Text book control with a lookup, you must first delete these from the form.
You need to be able to see the list of existing fields and the gallery of controls (both on the Form Deisgn Tools > Design tab). Also check the control wizard is enable to use the wizard, or disabled to use manual configuration (found in the More option for the controls gallery.
The property settings depend on wehterh you want to hard-code the list of look up a value in a table/query.
Property (Tab) | Hard-coded value list | Lookup in another table/query |
---|---|---|
Row Source Type (Data) | Must be Value List | Must be Table/Query |
Control Source (Data) | The name of the field in the underlying table/query (should be already set if you've dragged the field onto the form) | The name of the field in the underlying table/query (should be already set if you've dragged the field onto the form) |
Row Source (Data) | A list of the possible values with each value in quotes separated by semicolons. Enter the values here, seaparated by semicolons. | The name of the table or query in which values will be looked up. Select the table/query from the drop down. |
Bound Column (Data) | Always 1 | Usually 1. This is the column in the Lookup that shared the value used in the original form field. |
Column Count (Format) | Always 1 | The number of columns from the Lookup table/query that will be needed. |
Column widths (Format) | N/a | To hide the coded value in the drop-down, enter 0cm to set the first column width to zero. other width can be set if necesary, separated by semicolons. |
Limit to list Allow Value List Edits (Data) | These properties determine whether the user can use values not in the list, and whether new values can also be added for future use. | td>These properties determine whether the user can use values not in the list, and whether new values can also be added for future use.
Using a query rather than a table for a lookup is particularly poweful as it allows you to:
The form controls gallery includes the Button control, which can be configured (via a wizard) to carry out a range of actions including opening other objects - for example, a report. This allows you to make forms that enable other users to interact with the database entirely using the form, without needing to work directly with the tables, queries, and reports.
To add a button to review a report:
The resulting control will now allow the user to view (and print) a report without needing to know anything about the underlying data or queries.
Other buttons can be added for other options, like moving between records.
Make sure you've downloaded the Essential Access exercise files before attempting these exercises.