Chapter 17: Adding, Finding, and Querying Data

Add a Record to a Table

You build a database by adding records to a table in the database. Any new records that you add appear at the end of the table. You add records to a table in Datasheet view.

As your table grows longer, you can use the navigation buttons on your keyboard to navigate it. You can press tab.eps to move from cell to cell, or you can press the keyboard arrow keys. To move backward to a previous cell, press shift.eps+tab.eps.

After you enter a record in a database table, you can edit it if necessary. You edit records in a table in Datasheet view.

Add a Record to a Table

571941-fg1701.eps

001 In the Navigation pane, double-click the table to which you want to add a record.

Access opens the table, placing the cursor in the first cell of the first blank row.

By default, the first field in each table is an ID field, containing a unique ID number for the record. This value is set automatically.

002 Press tab.eps.

571941-fg1702.eps

Access moves your cursor to the next cell in the row.

003 Type the desired data in the selected cell.

004 Press tab.eps.

005 Repeat Steps 3 and 4 until you have filled the entire row.

006 Press ent.eps or press tab.eps to move to the next row, or record.

571941-fg1703.eps

Access adds the new record.

Access moves your cursor to the first cell in the next row.

571941-fg1704.eps

007 Repeat Steps 2 to 6 to add more records to the table.

Access adds your records.

You can resize a column by dragging the column border left or right.

You can use the scroll bars to view different portions of the table.

Note: To edit a record in a table, open the table in Datasheet view, click in the cell whose data you want to change, double-click the data to select it, and type over the data to replace it.

Add a Record to a Form

You can use forms to quickly add records to your Access databases. Forms present your record fields in an easy-to-read format. You add records to a form in Form view; this view presents each field in your table as a box that you can use to enter data.

After you enter a record in a form, you can edit it if necessary. (See the tip at the end of this section for more information.) For help locating a particular record in the form window in order to edit it, see the next section, “Navigate Records in a Form.”

Add a Record to a Form

571941-fg1705.eps

001 In the Navigation pane, double-click the form to which you want to add a record.

Note: If the form is not visible in the Navigation pane, click the 571941-ma435.tif along the top of the pane, choose Object Type, and locate the desired form under the Forms heading.

Access opens the form.

002 Click the Home tab.

003 Click the New button in the Records group.

571941-fg1706.eps

Access opens a blank form, placing the cursor in the first cell of the first field.

By default, the first field in the table associated with this form is an ID field, containing a unique ID number for the record. This value is set automatically.

004 Press tab.eps.

571941-fg1707.eps

Access moves your cursor to the next field in the form.

005 Type the desired data in the selected field.

006 Press tab.eps.

571941-fg1708.eps

Access moves to the next field in the form.

007 Repeat Steps 5 and 6 until you have filled the entire form.

008 Press ent.eps or tab.eps.

Access displays another blank record, ready for data.

To close the form window, you can click the Close button (571941-ma017.tif).

Navigate Records in a Form

You may find it easier to read a record using a form instead of reading it from a large table containing other records. Similarly, editing a record in a form may be easier than editing a record in a table. You can locate records you want to view or edit using the navigation bar that appears along the bottom of the form window. This navigation bar contains buttons for locating and viewing different records in your database. The navigation bar also contains a Search field for locating a specific record. (You learn how to search for a record in a form in the next section.)

Navigate Records in a Form

571941-fg1709.eps

001 In the Navigation pane, double-click the form whose records you want to navigate.

Note: If the form is not visible in the Navigation pane, click the 571941-ma435.tif along the top of the pane, choose Object Type, and locate the desired form under the Forms heading.

Access displays the form.

The Current Record box indicates what record you are viewing.

002 Click Previous Record (571941-ma193.tif) or Next Record (571941-ma191.tif) to move back or forward by one record.

571941-fg1710.eps

Access displays the previous or next record in the database.

Click First Record (571941-ma192.tif) or Last Record (571941-ma194.tif) to navigate to the first or last record in the table.

Click New (Blank) Record (571941-ma521.tif) to start a new, blank record.

Search for a Record in a Form

As mentioned, you may find it easier to read and edit records in a form than in a large table containing other records.

One way to locate records you want to view or edit is to use the various buttons in the navigation bar, such as the Previous Record button, the Next Record button, and so on. (Refer to the preceding section for help using these buttons.) This method can become time-consuming, however, if the form contains numerous records. An easier approach is to search for the record using Access’s search functionality, also accessible from the navigation bar.

Search for a Record in a Form

571941-fg1711.eps

001 In the Navigation pane, double-click the form containing the record you want to find.

Note: If the form is not visible in the Navigation pane, click the 571941-ma435.tif along the top of the pane, choose Object Type, and locate the desired form under the Forms heading.

Access displays the form.

002 Click in the Search field.

571941-fg1712.eps

003 Type a keyword that relates to the record you want to find (here, a person’s last name).

As you type, Access displays matching records.

Delete a Record from a Table

You can remove a record from your database if it holds data that you no longer need. Removing old records can reduce the overall file size of your database and make it easier to manage. When you delete a record, all of the data within its fields is permanently removed.

You can remove a record from a database by deleting it from a table or by deleting it from a form. This section shows you how to delete a record from a table. (For help deleting a record from a form, see the next section, “Delete a Record from a Form.”)

Delete a Record from a Table

571941-fg1713.eps

001 In the Navigation pane, double-click the table that contains the record you want to delete.

Access opens the table.

002 Position your mouse pointer over the gray box to the left of the record that you want to delete (the mouse pointer changes to 571941-ma522.eps) and click.

The record is selected.

003 Click the Home tab.

004 Click the Delete button in the Records group.

Note: You can also right-click the record and then click Delete Record.

571941-fg1714.eps

Access displays a warning box about the deletion.

005 Click Yes.

Access permanently removes the record from the table.

Delete a Record from a Form

In addition to removing records directly from a table, as described in the preceding section, “Delete a Record from a Table,” you can remove records that you no longer need by using a form.

The first step is to locate the record you want to delete; refer to the sections “Navigate Records in a Form” and “Search for a Record in a Form” for help locating the record.

Removing old records can reduce the overall file size of your database and make it easier to manage. When you delete a record, whether from a table or a form, all the data within its fields is permanently removed.

Delete a Record from a Form

571941-fg1715.eps

001 In the Navigation pane, double-click the form containing the record you want to delete.

Note: If the form is not visible in the Navigation pane, click the 571941-ma435.tif along the top of the pane, choose Object Type, and locate the desired form under the Forms heading.

Access displays the form.

002 Navigate to the record you want to delete.

003 Click the Home tab on the Ribbon.

004 Click the Delete button’s 571941-ma009.tif.

005 Click Delete Record.

571941-fg1716.eps

Access displays a warning box about the deletion.

006 Click Yes.

Access permanently removes the record.

Sort Records

Sorting enables you to arrange your database records in a logical order to match any criteria that you specify. For example, with a contacts database, you might sort the records alphabetically or based on the ZIP code. You can sort in ascending order or descending order. For example, if you are sorting alphabetically, you can sort from A to Z (ascending) or from Z to A (descending).

You can sort records in a table, or you can use a form to sort records. In this section, you learn how to do both.

Sort Records

Sort a Table

571941-fg1717.eps

001 Open the table you want to sort.

002 Position your mouse pointer over the column header for the field by which you want to sort (the mouse pointer changes to 571941-ma100.eps) and click.

003 Click the Home tab on the Ribbon.

004 Click a sort button.

Click Ascending to sort the records in ascending order.

Click Descending to sort the records in descending order.

571941-fg1718.eps

Access sorts the table records based on the field you choose.

This example sorts the records alphabetically by last name in ascending order.

In the prompt box that appears when you close the table, you can click Yes to make the sort permanent, or No to leave the original order intact.

Sort Using a Form

571941-fg1719.eps

001 Open the form you want to sort.

002 Click in the field by which you want to sort.

003 Click the Home tab on the Ribbon.

004 Click a sort button.

Click Ascending to sort the records in ascending order.

Click Descending to sort the records in descending order.

571941-fg1720.eps

Access sorts the table records based on the field you choose.

This example sorts the records alphabetically by last name in ascending order.

You can use the navigation buttons to view the sorted records.

Filter Records

You can use an Access filter to view only specific records that meet criteria you set. For example, you may want to view all clients buying a particular product, anyone in a contacts database who has a birthday in June, or all products within a particular category. You can also filter by exclusion — that is, filter out records that do not contain the search criteria that you specify.

You can apply a simple filter on one field in your database using the Selection tool, or you can filter several fields using the Filter by Form command.

Filter Records

Apply a Simple Filter

571941-fg1721.eps

001 Open the form you want to filter.

002 Click in the field by which you want to filter.

003 Click the Home tab on the Ribbon.

004 Click the Selection button.

005 Click a criterion.

571941-fg1722.eps

Access filters the records.

In this example, Access finds two records matching the filter criterion.

You can use the navigation buttons to view the filtered records.

To undo a filter, click the Toggle Filter button.

Filter by Form

571941-fg1723.eps

001 Open the form you want to filter.

002 Click the Home tab on the Ribbon.

003 Click the Advanced button.

004 Click Filter By Form.

571941-fg1724.eps

A blank form appears.

005 Click in the field by which you want to filter.

006 Click the 571941-ma009.tif that appears and choose a criterion.

007 Repeat Steps 5 and 6 to add more criteria to the filter.

You can set OR criteria using the tabs at the bottom of the form.

008 Click the Toggle Filter button.

Access filters the records.

To remove the filter, you can click the Toggle Filter button again.

Apply Conditional Formatting

You can use Access’s Conditional Formatting tool to apply certain formatting attributes, such as bold text or a fill color, to data in a form when the data meets a specified condition.

For example, if your database tracks weekly sales, you might set up Access’s Conditional Formatting feature to alert you if sales figures fall below what is required for you to break even.

You apply conditional formatting by creating a rule, which specifies the criteria that the value in a field must meet. Values that meet the criteria are formatted using settings you specify.

Apply Conditional Formatting

571941-fg1725.eps

001 Open the form to which you want to apply conditional formatting in Layout view.

002 Click the field to which you want to apply conditional formatting.

003 Click the Format tab.

004 Click the Conditional Formatting button.

571941-fg1726.eps

The Conditional Formatting Rules Manager dialog box opens.

005 Click the New Rule button.

The New Formatting Rule dialog box opens.

006 Set the criteria you want to use to apply conditional formatting.

007 Specify how values that meet your criteria should be formatted.

008 Click OK.

571941-fg1727.eps

Access creates a rule based on the criteria you set.

009 Click OK.

571941-fg1728.eps

Access applies the conditional formatting.

Perform a Simple Query

You can use a query to extract information that you want to view in a database. Queries are especially useful when you want to glean data from multiple tables.

Queries are similar to filters, but offer you greater control when it comes to viewing records. You can use the Query Wizard to help you select what fields you want to include in the analysis. There are several types of Query Wizards. These include Simple, covered here; Crosstab, to display information in a spreadsheet-like format; Find Duplicates, to find records with duplicate field values; and Find Unmatched, to find records in one table with no related records in another table.

Perform a Simple Query

Create a Query

571941-fg1729.eps

001 Open the table or form for which you want to perform a simple query.

002 Click the Create tab on the Ribbon.

003 Click the Query Wizard button.

571941-fg1730.eps

The New Query dialog box appears.

004 Click Simple Query Wizard.

005 Click OK.

571941-fg1731.eps

The Simple Query Wizard opens.

006 Click the Tables/Queries 571941-ma009.tif and choose the table containing the fields on which you want to base the query.

007 In the Available Fields list, click a field that you want to include in the query.

008 Click the Add button (571941-ma523.tif).

571941-fg1732.eps

The field is added to the Selected Fields list.

009 Repeat Steps 7 and 8 to add more fields to your query.

You can repeat Step 6 to choose another table from which to add fields.

Note: When using fields from two or more tables, the tables must have a prior relationship.

010 Click Next.

Perform a Simple Query (continued)

During the process of creating a new query, the Query Wizard asks you to give the query a unique name. This is so that you can refer to the query later. All queries that you create are saved in the Navigation pane; you can double-click a query in the Navigation pane to perform it again.

If, after performing a query, you determine that you need to add more criteria to it, you can easily do so. For example, you may realize that the query needs to include an additional table in your database. You can also sort and filter your query results.

Perform a Simple Query (continued)

571941-fg1733.eps

011 Type a name for the query.

012 Click the Open the query to view information radio button.

013 Click Finish.

571941-fg1734.eps

A query datasheet appears, listing the fields.

Add Criteria to the Query

571941-fg1735.eps

001 Double-click the query in the Navigation pane to open it.

Note: If the query is not visible in the Navigation pane, click the 571941-ma435.tif along the top of the pane, choose Object Type, and locate the desired query under the Queries heading.

002 Click the View button to switch to Design view.

003 Click in the Criteria field and type the data that you want to view.

This example lists a ZIP code as the criterion.

571941-fg1736.eps

004 Click the View button again to switch back to Datasheet view to see the results.

The table now shows only the records matching the criteria.

Create a Report

You can use Access to create a report based on one or more database tables. This can be a simple report, which contains all the fields in a single table, or a custom report, which can contain data from multiple tables in a database. (Note that to use fields from two or more tables, the tables must have a prior relationship; refer to the tip “What Is a Table Relationship?” in the preceding section for more information.)

To create a custom report, you can use the Report Wizard; it guides you through all the steps necessary to turn complex database data into an easy-to-read report.

Create a Report

Create a Simple Report

571941-fg1737.eps

001 Open the table for which you want to create a simple report.

002 Click the Create tab on the Ribbon.

003 Click the Report button.

571941-fg1738.eps

Access creates a simple report based on the table you selected.

Create a Custom Report

571941-fg1739.eps

001 Open a table you want to include in a custom report.

002 Click the Create tab.

003 Click the Report Wizard button.

The Report Wizard opens.

004 Click the Tables/Queries 571941-ma009.tif and choose a table you want to include in the report.

005 Under Available Fields, click a field that you want to include in the report.

006 Click the Add button (571941-ma523.tif).

The field is added to the Selected Fields list.

007 Repeat Steps 5 and 6 to add more fields.

008 Click Next.

571941-fg1740.eps

009 Click the field you want to use to group the data.

010 Click the Add button (571941-ma523.tif).

A preview of the grouping appears here.

011 Click Next.

Create a Report (continued)

As the Report Wizard guides you through the steps for building a report, you are asked to decide upon a sort order. You can sort records by up to four fields, in ascending or descending order. The wizard also prompts you to select a layout for the report. Options include Stepped, Block, and Outline, in either portrait or landscape mode. (Note that you can change other design aspects of the report by opening it in Design view; for more information, see the tip at the end of this section.) After you create the report, you can print it.

Create a Report (continued)

571941-fg1741.eps

012 To sort your data, click the first 571941-ma009.tif and click the field by which you want to sort.

You can add more sort fields as needed.

Note: Fields are sorted in ascending order by default. Click the Ascending button to toggle to descending order.

013 Click Next.

571941-fg1742.eps

014 Click a layout option.

You can set the page orientation for a report using these options.

015 Click Next.

571941-fg1743.eps

016 Type a name for the report.

017 Click the Preview the report radio button.

018 Click Finish.

571941-fg1744.eps

Access creates the report and displays the report in Print Preview mode.