MS Access

Finding Unmatched Records

The helps you find the records in one table that do not have matching records in another table.

Select the table or query with the values you want to display in the query.

Specify the field that will join the records in the first table to the records in the second table.

The query displays those records in the Customers table that do not have any matching records in the tblCustomerTours table.

Some scenarios when you might need to create such a query include:

  • To find customers who have never placed an order.

  • To find products that have never been purchased.

  • To find "orphan" records. If you haven't enforced referential integrity in your related tables, deleting a record in one table could leave one or more orphan records in a related table. For example, if you delete a customer record from a Customer table, you may leave several for that customer in an Order table.

In this lesson you will use the Find Unmatched Query Wizard to create a query to find customers who have never booked a tour.

  1. In the Database window, click the Queries icon in the Objects bar and click the New button.

    The New Query dialog box appears.

  2. Select the Find Unmatched Query Wizard and click OK.

    The first step of the Find Unmatched Query Wizard appears, as shown in figure. You need to choose the table or query whose values you want to display in the query. Since you want to find customers without any tour packages, you would select the tblCustomers table.

  3. Select the tblCustomers table and click Next.

    The second step of the Find Unmatched Query Wizard appears. Here you have to tell Access which table contains the related records. Let's select the tblCustomerTours table.

  4. Select the tblCustomerTours table and click Next.

    The third step of the Find Unmatched Query Wizard appears, as shown in figure. Here you have to specify the related field to join the records in the first table to the records in the second table. Once you have selected the matching record in both tables, click the button to join the two tables.

  5. Verify that the CustomerID field is selected in both tables and click the button.

    The Matching fields area displays the fields used to join the tables (CustomerID = CustomerID).

  6. Click Next.

    Almost there! Now you have to specify which fields you want to see in the query.

  7. Double-click the LastName, FirstName, City, and State fields.

    The LastName, FirstName, City, and State fields should all appear in the "Selected fields" list.

  8. Click Next.

    Here you can give your query a nameor you can accept the default name that Access gives the query.

  9. Click Finish.

    Access saves the query with the name "tblCustomers Without Matching tblCustomerTours" and displays the results of the query, as shown in figure.

  10. Close the query.

To use the find unmatched query wizard:

  1. in the database window, click the queries icon in the objects bar and click the new button.

  2. select find unmatched query wizard and click ok.

  3. select the table whose values you want to display and click next.

  4. select the table that contains the related records and click next.

  5. specify the related fields that join the two tables, then click the button to join the two tables and click next.

  6. double-click any additional fields that you want to appear in the query results and click next.

  7. click finish.

by BrainBellupdated
Advertisement: