Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Adding a Parameter to a Union Query


View products that this article applies to.

Summary

An easy way to prompt a user for a search criteria in a query is to create a parameter query. However, to use a parameter in a union query, you must follow some specific rules. This article uses the Customers and Suppliers By City query in the sample database Northwind.mdb to describe how to add a parameter to a union query.

↑ Back to the top


More information

The Customers and Suppliers By City union query selects records from both the Customers and Suppliers tables. The SQL of this query is as follows:
SELECT City, CompanyName, ContactName, "Customers" AS [Relationship]
FROM Customers
UNION SELECT City, CompanyName, ContactName, "Suppliers"
FROM Suppliers
ORDER BY City, CompanyName;
				
To add a parameter to the union query, you must duplicate the parameter in each SELECT clause in the query. To add a parameter (in this case a city) to the query, follow these steps.

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Open the sample database Northwind.mdb.
  2. Open the Customers and Suppliers By City query in Design view.
  3. Change the query's SQL so that it looks like the following:
    SELECT City, CompanyName, ContactName, "Customers" AS [Relationship]
    FROM Customers
    WHERE Country = "Brazil" and City = [Enter City]
    UNION SELECT City, CompanyName, ContactName, "Suppliers"
    FROM Suppliers
    WHERE Country = "Brazil" and City = [Enter City]
    ORDER BY City, CompanyName;
    					
  4. Save the query and close it.
  5. Open the query in Datasheet view and type Rio de Janeiro in the Enter City dialog box. Note that only the Customers and Suppliers from Rio de Janeiro are displayed.

↑ Back to the top


References

For more information about union queries, click Microsoft Access Help on the Help menu, type union queries in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB208815, kbusage, kbhowto

↑ Back to the top

Article Info
Article ID : 208815
Revision : 2
Created on : 6/30/2004
Published on : 6/30/2004
Exists online : False
Views : 259