
- •Using Data Filters
- •Filter by Selection
- •To Do: Perform a Filter by Form
- •Using Queries
- •Creating a Query with the Query Wizard
- •To Do: Build a Query with the Simple Query Wizard
- •Using the Query Design View
- •To Do: Create a Query from Scratch
- •To Do: Work with Complex Queries
- •Table 19.1. Access's Relational Operators
- •Summary
- •If I save a filter as a named query, what does the query's Query Design view look like?
Work with package MS Office: MS Access
Retrieving Your Data
Although your database contains a lot of data, you rarely want to see all that data simultaneously. Generally, you want to see only data subsets. Access filters and queries produce small, meaningful subsets of data from your tables.
When you view data subsets, you prevent unwanted data from getting in the way of the information with which you need to work. The power of queries is that you can select records and fields from multiple tables and view that selected data subset from within a single Datasheet or Form view.
The highlights of this hour include the following:
What a filter is
Why you sometimes use Filter by Selection and sometimes use Filter by Form
When a filter limits you
Why queries are more robust than filters
How to use the query wizards
How to use the Query Design view to create and edit queries
Where to specify advanced selection criteria
Using Data Filters
A filter is a subset of data from a table. Suppose that you want to see only certain records from a table, such as all customers who are past due. Instead of hiding the records that you don't want to see, create a filter. The filter removes unwanted records from view. The records don't go away, and you don't have to unhide the records later (as you do when you actually hide records by right-clicking over a column or row in the Datasheet view and selecting Hide Columns or Hide Row). A filter works similar to a short-term record hider, putting certain records out of the way while you work with the filtered data.
Access supports three filtering approaches:
Filter by Selection— Filters data based on selected table data
Filter by Form— Enables you to choose the data fields that you want Access to use for filtering
Advanced Filter/Sort— Controls advanced filtering options from the Access menu bar
The easiest and most common filter options are Filter by Selection and Filter by Form, which the following two sections describe.
Note
Access includes an Advanced Filter/Sort option on the Records, Filter menu, but you will almost always prefer creating a query to using the advanced filter. In addition, Access supports a Filter Excluding Selection that filters the opposite of Filter by Selection. A query is easier to save and work with in the future than a filter.
Filter by Selection
Filter by Selection works by example. Suppose that you want to display only those table records that contain a specific field value; for example, you need to work only with customer records from Brazil. If your customer table contains a Country field with scattered Brazil entries, you can filter out all those records that do not contain Brazil in their Country fields.
You can practice working with filters by opening the sample Northwind Traders database that comes with Access. (You'll locate this northwind.mdb file in your Office Samples folder.) When you first open the Northwind Traders database, a splash screen will appear; a splash screen acts as a title window that appears when you first start an application, and you can designate splash screens for databases that appear when you open them. Close the splash screen by clicking the window's OK button, and perform the following steps to design a filter that filters out records that don't contain Brazil:
Display the Datasheet view for the Customers table. To do so, click the Tables entry inside the Database window and double-click the Customers table.
Locate one record with Brazil in the Country field. You might have to click the horizontal scrollbar to see the Country field from the Datasheet view.
Double-click the single field value that contains Brazil to select that value. If you select only the first part of the field instead of selecting the entire field, such as the letter B, you filter all records that do not start with B.
Click the toolbar's Filter by Selection button. Access filters out all records that don't match your selected criteria. Figure 19.1shows a filtered Datasheet view that displays only records containing a Brazil entry in their Country fields. Before the filter, this Datasheet view held more than 90 records. By locating the sample field and clicking once, you just changed the display to those records that only contain Brazil for the Country field value.
Figure 19.1 Filter by Selection filters out all unwanted records.
To return to the full Datasheet view, click the toolbar's Remove Filter button.
As you can see, a filter removes unwanted records; Access filters those unwanted records from view.
Note
To filter out records that contain your selected value (instead of all records that do not contain a value), select Records, Filter, Filter Excluding Selection. The Filter Excluding Selection option works similarly to a reverse filter. Click the Remove Filter to revert the Datasheet view to its full table once again.
You do not first have to locate a customer record from Brazil to create the previous list's filter. If you right-click any field value in the table and then type a value in the shortcut menu's Filter for text box, Access applies the Filter by Selection command to your entered filter value. Therefore, you can right-click on the Country column, select Filter By, and then enter Brazil to more quickly create the same filter that the previous steps created.
For now, remove the filtering by clicking the Remove Filter button on the toolbar.