Microsoft Office Online
Sign in to My Office Online (What's this?) | Sign in

 
 
Microsoft Office Access
Search
Search
 
Check for updates: (c) Microsoft
Office downloads
 
 
 
Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.

Email this linkEmail this link Printer-Friendly VersionPrinter-Friendly Version Bookmark and ShareShare
Troubleshoot queries (MDB)
 

All queries

ShowI'm getting an unexpected message.

ShowI'm getting an SQL syntax message.

ShowI'm getting the message "Can't bind name <name>."

You may encounter this message if you didn't explicitly declare in the Query Parameters dialog box the parameters used for a crosstab query (crosstab query: A query that calculates a sum, average, count, or other type of total on records, and then groups the result by two types of information: one down the left side of the datasheet and the other across the top.) or a query that a crosstab query or chart is based on. To resolve this, do the following:

  1. In the query that contains the parameter, specify the parameter and its data type in the Query Parameters dialog box.

    ShowHow?

    1. On the Query menu, click Parameters.

    2. In the first Parameter cell, type the first prompt you entered in the query design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.).

    3. In the Data Type cell to the right, click the appropriate data type (data type: The characteristic of a field that determines what type of data it can hold. Data types include Boolean, Integer, Long, Currency, Single, Double, Date, String, and Variant (default).) according to the following guidelines:

      If the parameter field data type is Click this data type in the Data Type cell
      Currency, Date/Time, Memo, OLE Object, Text, and Yes/No Currency, Date/Time, Memo, OLE Object, Text, and Yes/No
      Number Byte, Single, Double, Integer, Long Integer, and Replication ID. These data types correspond to the five FieldSize property settings of the Number data type.
      Unknown Value. This is a generic data type that accepts any type of data.
      Binary Binary. Used with parameter queries directed to linked tables (linked table: A table stored in a file outside the open database from which Access can access records. You can add, delete, and edit records in a linked table, but you cannot change its structure.) that do recognize this data type (Microsoft Access does not recognize this data type).
    4. Repeat Steps 2 and 3 for each parameter you want to specify a data type for.

  2. In the same query, set the ColumnHeadings property.

    ShowHow?

    You can change the order or limit the column headings in a crosstab query (crosstab query: A query that calculates a sum, average, count, or other type of total on records, and then groups the result by two types of information: one down the left side of the datasheet and the other across the top.). For example, in column headings containing the months of the year, you can display the months chronologically rather than alphabetically. Or, you can limit the columns to just January through June.

    1. Open the crosstab query in Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.).
    2. Click the background of query Design view, outside the design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.) and the field lists (field list: A window that lists all the fields in the underlying record source or database object, except in data access page Design view. In data access page Design view, it lists all the record sources and their fields in the underlying database.).
    3. On the toolbar, click Properties Button image to display the query's property sheet.
    4. In the ColumnHeadings property box, enter the column headings you want to display, in the order in which you want to display them. Between the column headings, type a comma or the list separator for your country/region. (To find the list separator for your country/region, see the regional settings in Microsoft Windows Control Panel.)

      The column headings you enter must exactly match the column headings in the query datasheet. For example, if a column heading in the datasheet is "USA," you must enter a column heading of "USA"— not "US." (After you press ENTER or move the pointer to a different location, Microsoft Access places quotation marks around each heading.)

    5. To view the query's results, click View Button image on the toolbar.

    Notes

ShowI'm getting a key violation message.

A key violation occurs if:

If you run such a query, it will not modify the records that cause the key violation.

To modify records in a way that will violate referential integrity, you can break the relationship between the affected tables or turn referential integrity off, and then modify the affected tables one at a time. However, the resulting data in the two tables may then conflict.

ShowI'm getting a validation rule violation message.

A validation rule (validation rule: A property that defines valid input values for a field or record in a table, or a control on a form. Access displays the message specified in the ValidationText property when the rule is violated.) violation occurs if you attempt to update or append records that violate the validation rules for a field or record.

ShowRestrict or validate data

A field validation rule is used to check the value entered into a field as the user leaves the field. A record validation rule controls when an entire record can be saved. Unlike field validation rules, record validation rules can refer to other fields.

You can also determine whether data that already existed before you specified a validation rule or changed the Required or AllowZeroLength property violates the current settings.

  1. Open a table in Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.).
  2. Do one of the following:

    ShowDefine a validation rule to control what values can be entered into a field

    1. In the upper portion of the window, click the field you want to define a validation rule for.
    2. In the lower portion of the window, click the ValidationRule property box, and then type the validation rule, or click the Build button Button image to create the validation rule using the Expression Builder (Expression Builder: An Access tool that you can use to create an expression. It includes a list of common expressions that you can select.).

      For example, you could define the validation expression ">9" for a Quantity field to prevent a user from placing an order for fewer than 10 units.

    3. In the ValidationText property box, type the message that you want displayed when the rule is broken.

      For example, for the validation expression ">9", you could enter "You must order 10 or more units."

    4. If you set a validation rule in a field that contains data, Microsoft Access will ask if you want to apply the new rule to existing data when you save the table. If you click Yes, Access will warn you when existing data violates the validation rule.

    ShowDefine a validation rule to control when a record can be saved

    1. Click Properties Button image on the toolbar to display the table's property sheet.
    2. In the ValidationRule property box, type the validation rule. Or click the Build button Button image to create the validation rule using the Expression Builder (Expression Builder: An Access tool that you can use to create an expression. It includes a list of common expressions that you can select.).

      For example, you could define the validation expression "[RequiredDate]<=[OrderDate]+30" to make sure that the date entered into the RequiredDate field is within 30 days of the date in the OrderDate field.

    3. In the ValidationText property box, type the message that you want Microsoft Access to display when the rule is broken.

      For example, for the validation expression "[RequiredDate]<=[OrderDate]+30", you could enter "The required date must be within 30 days of the order date."

    4. If you set a validation rule in a table that contains data, Microsoft Access will ask if you want to apply the new rule to existing data when you save the table. If you click Yes, Microsoft Access will warn you when existing data violates the validation rule.

    ShowTest data that already existed before you specified a validation rule

    • Right click the title bar of the table, and click Test Validation Rules.

      Microsoft Access will warn you if existing data violates a validation rule or the Required or AllowZeroLength settings.

ShowI'm getting the message "Query contains ambiguous outer joins."

When you see the following message:

"The SQL statement couldn't be executed, because it contains ambiguous outer joins."

You tried to execute an SQL statement that contains multiple joins (join: An association between a field in one table or query and a field of the same data type in another table or query. Joins tell the program how data is related. Records that don't match may be included or excluded, depending on the type of join.). There are three types of joins in Microsoft Access: inner joins (inner join: A join where records in two tables are combined in a query's results only if values in the joined fields meet a specified condition. In a query, the default join is an inner join that selects records only if values in the joined fields match.) (also called equi-joins), left outer joins (outer join: A join in which each matching record from two tables is combined into one record in the query's results, and one table contributes all of its records, even if the values in the joined field don't match those in the other table.), and right outer joins. In some multiple joins, the results of the query can differ depending on the type of join and the order in which the joins are performed.

ShowExample of a multiple join using three tables

For example, if you have three tables, Consultants, Active Consultants, and Projects, and you create an SQL query by joining these tables in the following way:

Consultants LEFT OUTER JOIN Active Consultants INNER JOIN Projects
								

Microsoft Access can process this query in two ways:

  • (Consultants LEFT OUTER JOIN Active Consultants) INNER JOIN Projects
    										

Access first creates a left outer join between the Consultants and Active Consultants tables, and then creates an inner join between the results of those two tables and the Projects table.

  • Consultants LEFT OUTER JOIN (Active Consultants INNER JOIN Projects)
    										

Access creates a left outer join between the Consultants table and the results of the inner join between the Active Consultants and Projects tables

Because Microsoft Access can produce different results between these two ways of joining, you must specify which way to join by changing one of the joins or by separating the query into two queries.

ShowMultiple joins that aren't ambiguous

The following combinations of joins can only produce one result and will not generate an "ambiguous outer joins" message:

  • Consultants INNER JOIN Active Consultants INNER JOIN Projects
    										

  • Consultants INNER JOIN Active Consultants LEFT OUTER JOIN Projects
    										

  • Consultants LEFT OUTER JOIN Active Consultants LEFT OUTER JOIN Projects
    										

  • Consultants RIGHT OUTER JOIN Active Consultants INNER JOIN Projects
    										

  • Consultants RIGHT OUTER JOIN Active Consultants LEFT OUTER JOIN Projects
    										

  • Consultants RIGHT OUTER JOIN Active Consultants RIGHT OUTER JOIN Projects
    										

ShowMultiple joins that are ambiguous

The following combinations of joins can produce more than one result and will generate an "ambiguous outer joins" message:

  • Consultants INNER JOIN Active Consultants RIGHT OUTER JOIN Projects
    										

  • Consultants LEFT OUTER JOIN Active Consultants INNER JOIN Projects
    										

  • Consultants LEFT OUTER JOIN Active Consultants RIGHT OUTER JOIN Projects
    										

ShowI'm getting a type mismatch message.

ShowI'm getting the message "Too few parameters expected."

You may encounter this message if a parameter query (parameter query: A query in which a user interactively specifies one or more criteria values. A parameter query is not a separate kind of query; rather, it extends the flexibility of a query.) exported to the IDC (IDC/HTX files: Microsoft Internet Information Server uses an IDC file and an HTX file to retrieve data from an ODBC data source and format it as an HTML document.) file type doesn't have data types specified in the Query Parameters dialog box.

  1. In the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.), click Queries Button image under Objects.
  2. Click the name of the parameter query you want to modify, and click Design on the Database window toolbar.
  3. Specify a data type for the parameter.

    ShowHow?

    1. On the Query menu, click Parameters.

    2. In the first Parameter cell, type the first prompt you entered in the query design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.).

    3. In the Data Type cell to the right, click the appropriate data type (data type: The characteristic of a field that determines what type of data it can hold. Data types include Boolean, Integer, Long, Currency, Single, Double, Date, String, and Variant (default).) according to the following guidelines:

      If the parameter field data type is Click this data type in the Data Type cell
      Currency, Date/Time, Memo, OLE Object, Text, and Yes/No Currency, Date/Time, Memo, OLE Object, Text, and Yes/No
      Number Byte, Single, Double, Integer, Long Integer, and Replication ID. These data types correspond to the five FieldSize property settings of the Number data type.
      Unknown Value. This is a generic data type that accepts any type of data.
      Binary Binary. Used with parameter queries directed to linked tables (linked table: A table stored in a file outside the open database from which Access can access records. You can add, delete, and edit records in a linked table, but you cannot change its structure.) that do recognize this data type (Microsoft Access does not recognize this data type).
    4. Repeat Steps 2 and 3 for each parameter you want to specify a data type for.

  4. Save and run the query.

ShowI'm getting the message "Operation must use an updatable query."

This error occurs when the current query's Update To row includes a field from either a crosstab query or select query in which an aggregate (aggregate function: A function, such as Sum, Count, Avg, or Var, that you use to calculate totals.) (total) was calculated for the field (using either the Totals row or a domain function (domain aggregate function: A function, such as DAvg or DMax, that is used to calculate statistics over a set of records (a domain).) in the Field row). To update a field using the aggregate of another field, calculate the aggregate in the update query itself, not a different query.

  1. In query Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.) for the current query, remove the field you added from the crosstab or select query (where an aggregate was calculated for the field).
  2. In the Update To cell of the field you want to update, enter a domain function that calculates the same aggregate as is calculated for that field in the select or crosstab query.

    For example, if you had a calculated field (calculated field: A field, defined in a query, that displays the result of an expression rather than displaying stored data. The value is recalculated each time a value in the expression changes.) named Sales So Far in your Products table, you could update it with the product of the Quantity and UnitPrice fields in the Order Details table by adding it to the design grid in the update query and then entering the following domain function in its Update To cell.

    DSum("[Quantity] * [UnitPrice]", "Order Details", "[ProductID]=" & [ProductID])
    								

  3. To run the query, click Run Button image on the toolbar.
  4. To see the results, open the table by clicking Tables Button image under Objects in the Database window and clicking Open on the Database window toolbar.

ShowThe wildcard character in my query is behaving unexpectedly.

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible and use different wildcard characters. For example, if you run:

When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use because if you create a later query under a different ANSI SQL query mode than the current mode of your Access database, your query could produce runtime errors or unexpected results.

ShowAbout using wildcard characters to search for partial or matching values

You use wildcard characters as placeholders for other characters when you are specifying a value you want to find and you:

  • Know only part of the value.
  • Want to find values that start with a specific letter or match a certain pattern.

Wildcard characters are meant to be used with fields that have the Text data type. You can sometimes use them successfully with other data types, such as dates, if you don't use the Microsoft Windows Control Panel to change the regional settings for these data types.

If you are using Microsoft Jet database engine SQL in a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you can use the following wildcard characters in queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) and expressions (expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.) to find such things as field values, records, or file names. You can also use these characters in the Find and Replace dialog boxes in an Access database or a Microsoft Access project (Microsoft Access project: An Access file that connects to a Microsoft SQL Server database and is used to create client/server applications. A project file doesn't contain any data or data-definition-based objects such as tables and views.).

Character Description Example
* Matches any number of characters. It can be used as the first or last character in the character string. wh* finds what, white, and why
? Matches any single alphabetic character. B?ll finds ball, bell, and bill
[ ] Matches any single character within the brackets. B[ae]ll finds ball and bell but not bill
! Matches any character not in the brackets. b[!ae]ll finds bill and bull but not ball or bell
- Matches any one of a range of characters. You must specify the range in ascending order (A to Z, not Z to A). b[a-c]d finds bad, bbd, and bcd
# Matches any single numeric character. 1#3 finds 103, 113, 123

To find such things as field values, records, or file names in a Microsoft Access project, or in a Microsoft Access database that uses Microsoft SQL Server-compatible syntax, use the following ANSI-92 wildcards.

Note  If you are using the Find and Replace dialog boxes in an Access project or in an Access database that uses Microsoft SQL Server-compatible syntax, you must use the Microsoft Jet SQL wildcards.

Character Description Example
% Matches any number of characters. It can be used as the first or last character in the character string. wh% finds what, white, and why
_ Matches any single alphabetic character. B_ll finds ball, bell, and bill
[ ] Matches any single character within the brackets. B[ae]ll finds ball and bell but not bill
^ Matches any character not in the brackets. b[^ae]ll finds bill and bull but not ball or bell
- Matches any one of a range of characters. You must specify the range in ascending order (A to Z, not Z to A). b[a-c]d finds bad, bbd, and bcd

Notes

ShowThe same underlying query behaves differently in a data access page and form.

The query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) underlying a form (form: An Access database object on which you place controls for taking actions or for entering, displaying, and editing data in fields.) runs under the current ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) setting of the Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.). On the other hand, the query underlying a data access page (data access page: A Web page, published from Access, that has a connection to a database. In a data access page, you can view, add to, edit, and manipulate the data stored in the database. A page can also include data from other sources, such as Excel.) always runs under ANSI-92 SQL query mode because a page connects to your Access database by using ADO (ActiveX Data Objects (ADO): A data access interface that communicates with OLE DB-compliant data sources to connect to, retrieve, manipulate, and update data.) and the Microsoft Jet OLE DB provider, both of which can only use ANSI-92 SQL. Therefore, if the SQL query mode setting of your Access database is ANSI-89, the query underlying the form runs as an ANSI-89 query, but the query underlying a page always runs under ANSI-92 regardless of the Access database setting. To resolve this, use a different query for the form and, if possible, rewrite the query for the form to produce the same results as the underlying query for the page.

ShowI'm not getting the records I want.

ShowThere are too many records.

ShowThere are too few records.

ShowThe records are wrong.

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/region beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 - 2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

ShowI'm not getting the columns I want.

ShowThere are too many columns.

ShowThere are too few columns.

ShowThe columns are wrong.

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/regions beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 - 2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

ShowI'm not getting the calculation results I expected.

ShowThe results of a totals (aggregate) calculation are wrong.

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/regions beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 -2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

Specific types of queries

ShowSelect queries

ShowMy AutoLookup query isn't filling in data.

For AutoLookup to work, certain conditions must be met:

ShowI can't update data from a query.

In some cases, you can edit data in query Datasheet view (Datasheet view: A window that displays data from a table, form, query, view, or stored procedure in a row-and-column format. In Datasheet view, you can edit fields, add and delete data, and search for data.) to change the data in the underlying table. In other cases, you can't. The following information shows whether a query's results can be updated, and if not, whether there is an alternative.

ShowData is updatable under certain conditions

If a query is based on tables with a one-to-many relationship (one-to-many relationship: An association between two tables in which the primary key value of each record in the primary table corresponds to the value in the matching field or fields of many records in the related table.), you might not be able to edit the data for the following query fields.

Query field Solution
Join field from the "one" side Enable cascading updates (cascading update: For relationships that enforce referential integrity between tables, the updating of all related records in the related table or tables when a record in the primary table is changed.) between the two tables.
New records, if the "many" side join field doesn't appear in the datasheet Add the join field from the "many" side to your query to allow adding new records.
Join field from the "many" side, after you've updated data on the "one" side Save the record; then you'll be able to make changes to the "many" side join field.
Blank field from the table on the "one" side of a one-to-many relationship where an outer join (outer join: A join in which each matching record from two tables is combined into one record in the query's results, and one table contributes all of its records, even if the values in the joined field don't match those in the other table.) exists Enter values in fields from the table on the "many" side, but only if the joined field from the "one" side contains a value for that record.
New records, if entire unique key of ODBC (Open Database Connectivity (ODBC): A standard method of sharing data between databases and programs. ODBC drivers use the standard Structured Query Language (SQL) to gain access to external data.) table isn't output Select all primary key (primary key: One or more fields (columns) whose values uniquely identify each record in a table. A primary key cannot allow Null values and must always have a unique index. A primary key is used to relate a table to foreign keys in other tables.) fields of ODBC tables to allow inserts into them.

ShowData can be deleted but not updated

Query or query field Solution
Query (or underlying table) for which Update Data permission (permissions: A set of attributes that specifies what kind of access a user has to data or objects in a database.) isn't granted To modify data, permissions must be assigned.
Query (or underlying table) for which Delete Data permission isn't granted To delete data, permissions must be assigned.

ShowData can't be updated

Query or query field Solution
Query based on three or more tables in which there is a many-to-one-to-many relationship Though you can't update the data in the query directly, you can update the data in a form or data access page (data access page: A Web page, published from Access, that has a connection to a database. In a data access page, you can view, add to, edit, and manipulate the data stored in the database. A page can also include data from other sources, such as Excel.) based on the query if the form's RecordsetType property is set to Dynaset (Inconsistent Updates).
Crosstab query (crosstab query: A query that calculates a sum, average, count, or other type of total on records, and then groups the result by two types of information: one down the left side of the datasheet and the other across the top.) None
SQL pass-through query (pass-through query: An SQL-specific query you use to send commands directly to an ODBC database server. By using pass-through queries, you work directly with the tables on the server instead of having the Microsoft Jet database engine process the data.) None
Query that calculates a sum, average, count or other type of total on the values in a field, or an update query that references a field in the Update To row from either a crosstab query, select query (select query: A query that asks a question about the data stored in your tables and returns a result set in the form of a datasheet, without changing the data.), or subquery (subquery: An SQL SELECT statement that is inside another select or action query.) that contains totals or aggregate functions By using a domain aggregate function (domain aggregate function: A function, such as DAvg or DMax, that is used to calculate statistics over a set of records (a domain).) in the Update To row of an update query, you can reference fields from either a crosstab query, select query, or subquery that contains totals or aggregate functions.
Union query (union query: A query that uses the UNION operator to combine the results of two or more select queries.) None
Query whose UniqueValues property is set to Yes None
Query that includes a linked ODBC (Open Database Connectivity (ODBC): A standard method of sharing data between databases and programs. ODBC drivers use the standard Structured Query Language (SQL) to gain access to external data.) table with no unique index (unique index: An index defined by setting a field's Indexed property to Yes (No Duplicates). A unique index will not allow duplicate entries in the indexed field. Setting a field as the primary key automatically defines the field as unique.), or a Paradox table without a primary key (primary key: One or more fields (columns) whose values uniquely identify each record in a table. A primary key cannot allow Null values and must always have a unique index. A primary key is used to relate a table to foreign keys in other tables.) None
Query that includes more than one table or query, and the tables or queries aren't joined by a join line in Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.) You must join the tables properly in order to update them.
Calculated field (calculated field: A field, defined in a query, that displays the result of an expression rather than displaying stored data. The value is recalculated each time a value in the expression changes.) None
Field is read-only; the database was opened as read-only or is located on a read-only drive None
Field in record that has been deleted or locked (locked: The condition of a record, recordset, or database that makes it read-only to all users except the user currently modifying it.) by another user A locked record should be updatable as soon as it is unlocked.

ShowI get prompted for a parameter when I don't expect it.

If you get prompted to type parameters when you attempt to run a query, report, or data access page (data access page: A Web page, published from Access, that has a connection to a database. In a data access page, you can view, add to, edit, and manipulate the data stored in the database. A page can also include data from other sources, such as Excel.), and you don't expect this prompt, one of the following might apply:

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/regions beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 - 2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

ShowParameter queries

ShowI get prompted for a parameter when I don't expect it.

If you get prompted to type parameters when you attempt to run a query, report, or data access page (data access page: A Web page, published from Access, that has a connection to a database. In a data access page, you can view, add to, edit, and manipulate the data stored in the database. A page can also include data from other sources, such as Excel.), and you don't expect this prompt, one of the following might apply:

ShowI want the option of returning all records with a parameter query.

  1. In the design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.) under the field that contains the prompt in the Criteria cell, type [prompt] Is Null in the Or cell, where prompt is the same prompt that is in the Criteria cell for that field. For example:

    In this cell Type
    Criteria Like [Enter Category ID:]
    Or [Enter Category ID:] Is Null
  2. Run the query.

  3. Leave the Enter Parameter Value dialog box blank, when Microsoft Access prompts you.

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/regions beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 - 2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

ShowCrosstab queries

ShowThe column headings aren't in the order I want, or I want to exclude some of them.

You can set the query's ColumnHeadings property to specify which column headings are displayed and in what order you want them to appear.

ShowOne or more column headings in the query's results are labeled "<>".

Microsoft Access returns "<>" as the column name for any Null (Null: A value you can enter in a field or use in expressions or queries to indicate missing or unknown data. In Visual Basic, the Null keyword indicates a Null value. Some fields, such as primary key fields, can't contain Null.)Null value in the field with Column Heading in its Crosstab cell. To avoid this you can:

ShowThe wrong field values are showing as row or column headings.

Verify that the field whose values you want to use as column headings is in the query design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.) with Column Heading in the Crosstab cell, and that the field or fields whose values you want to use as row headings are in the query design grid with Row Heading in the Crosstab cell. To change the value in a Crosstab cell, click the cell, and then click a value from the list. If you want to display only certain values as column headings, set the query's ColumnHeadings property.

ShowSort or limit column headings displayed in a crosstab query

You can change the order or limit the column headings in a crosstab query (crosstab query: A query that calculates a sum, average, count, or other type of total on records, and then groups the result by two types of information: one down the left side of the datasheet and the other across the top.). For example, in column headings containing the months of the year, you can display the months chronologically rather than alphabetically. Or, you can limit the columns to just January through June.

  1. Open the crosstab query in Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.).
  2. Click the background of query Design view, outside the design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.) and the field lists (field list: A window that lists all the fields in the underlying record source or database object, except in data access page Design view. In data access page Design view, it lists all the record sources and their fields in the underlying database.).
  3. On the toolbar, click Properties Button image to display the query's property sheet.
  4. In the ColumnHeadings property box, enter the column headings you want to display, in the order in which you want to display them. Between the column headings, type a comma or the list separator for your country/region. (To find the list separator for your country/region, see the regional settings in Microsoft Windows Control Panel.)

    The column headings you enter must exactly match the column headings in the query datasheet. For example, if a column heading in the datasheet is "USA," you must enter a column heading of "USA"— not "US." (After you press ENTER or move the pointer to a different location, Microsoft Access places quotation marks around each heading.)

  5. To view the query's results, click View Button image on the toolbar.

Notes

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/regions beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 - 2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

ShowAction queries (append, update, delete)

ShowThe append query appended data to or from the wrong fields.

Verify that you have selected the correct fields for the data you want to append, entered the correct criteria (criteria: Conditions you specify to limit which records are included in the result set of a query or filter.) in the query design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.), and selected the correct fields to which you want to append data. Restore the appended query to its original state, through a backup copy if one is available, then do one or more of the following:

ShowSelect the fields whose data you want to append

Add the following fields to the query design grid for your append query:

ShowSelect the fields to which you want to append data

  • In the Append To cell of the fields whose data you want to append, click the name of the field you want to add the data to.

Note  If you added the asterisk (*) in the Field cell, select the asterisk in the Append To cell. All the field names in both tables must be exactly the same. You can't specify criteria for selecting records in the column that contains the asterisk, but you can specify criteria in a separate column.

ShowThe update query didn't perform the update I wanted.

ShowI don't know the best way to delete duplicate records after using the Find Duplicates Wizard.

Delete the duplicate record or the original directly in query Datasheet view (Datasheet view: A window that displays data from a table, form, query, view, or stored procedure in a row-and-column format. In Datasheet view, you can edit fields, add and delete data, and search for data.).

ShowDelete a record

ShowDelete a record in a data access page

Important  Once you delete a record, you cannot undo the deletion from the database.

  • On the record navigation toolbar, click Delete Button image.

Note  If you don't see the Delete button, it means one of the following:

  • The page does not support deletions.
  • The designer of the page has customized the button. Look for custom instructions on how to use the page, or contact the designer.

If you want Microsoft Access to delete all the duplicates and keep the original records automatically, don't use the Find Duplicates Wizard or convert a Find Duplicates query to a delete query.

ShowDelete duplicate records from a table

Deleting duplicate records from a table requires two steps. First, you create a copy of the structure of the table that contains duplicates, and then make primary keys (primary key: One or more fields (columns) whose values uniquely identify each record in a table. A primary key cannot allow Null values and must always have a unique index. A primary key is used to relate a table to foreign keys in other tables.) of all the fields that contain duplicates. Second, you create and run an append query (append query: An action query that adds the records in a query's result set to the end of an existing table.) from the original table to the new table. Because fields that are primary keys can't contain duplicate records, this procedure produces a table without duplicate records.

  1. Copy the table and make primary keys of fields with duplicates.

    ShowHow?

    1. In the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.), click Tables Button image under Objects.
    2. Click the name of the table you want to delete duplicate records from.
    3. Click Copy Button image on the toolbar (toolbar: A bar with buttons and options that you use to carry out commands. To display a toolbar, click Customize on the Tools menu, and then click the Toolbars tab.).
    4. Click Paste Button image on the toolbar.
    5. In the Paste Table As dialog box, type a name for the copied table, click Structure Only, and then click OK.
    6. Open the new table in Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.), and select the fields that contained duplicates in the table you copied.
    7. Click Primary Key Button image on the toolbar to create a primary key based on the selected fields.
    8. Save and close the table.
  2. Append only unique records to the new table.

    ShowHow?

    1. Create a new query based on the original table that contains duplicates.
    2. In query Design view (Design view: A window that shows the design of these database objects: tables, queries, forms, reports, macros, and data access pages. In Design view, you can create new database objects and modify the design of existing ones.), click Query Type Button image on the toolbar, and then click Append Query.
    3. In the Append dialog box, click the name of the new table in the Table Name list, and then click OK.
    4. Include all the fields from the original table by dragging the asterisk (*) to the query design grid (design grid: The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort window. For queries, this grid was formerly known as the QBE grid.).
    5. Click Run Button image on the toolbar.
    6. Click Yes when you receive the message that you're about to append rows.
    7. Click Yes when you receive the message that Microsoft Access can't append all the records in the append query. This transfers only unique records to your new table and discards the duplicates.
    8. Open the table to see the results.
    9. When you're sure the new table has the correct unique records, you can delete the original table, and then rename the new table using the name of the original table.

ShowThe delete query deleted records from the wrong table.

Make sure the delete query includes:

In the query design grid, From or Where is displayed in the Delete row. From is displayed in the Delete cell of the table you'll delete records from, and Where is displayed in the Delete cell of the field(s) you specify criteria for.

ShowThe query is running under a different ANSI SQL query mode.

You created your query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) under a different ANSI SQL query mode (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.) than the current mode of your Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.).

ShowWhy you should avoid mixing queries created under different ANSI SQL query modes

The two ANSI SQL query modes (ANSI SQL query mode: One of two types of SQL syntax: ANSI-89 SQL (also called Microsoft Jet SQL and ANSI SQL), which is the traditional Jet SQL syntax; and ANSI-92 SQL, which has new and different reserved words, syntax rules, and wildcard characters.), ANSI-89 and ANSI-92, are not compatible. When you create a Microsoft Access database (Microsoft Access database: A collection of data and objects (such as tables, queries, or forms) that is related to a particular topic or purpose. The Microsoft Jet database engine manages the data.), you need to decide which query mode you are going to use, because mixing queries (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form, report, or data access page.) created in both query modes could produce runtime errors or unexpected results. The range of data types, reserved words, and wildcard characters are different in each query mode.

ShowExample of a query using wildcard characters

A query that uses wildcard characters in a criteria expression can produce different results under each query mode. For example, if you run:

  • An ANSI-89 SQL query in a database set to ANSI-92 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U*'

    It returns all customers from a country/region named "U*", not all countries/regions beginning with the letter "U", because the asterisk (*) is not a wildcard character in ANSI-92 SQL.

  • An ANSI-92 SQL query in a database set to ANSI-89 query mode, such as:

    SELECT * FROM Customers WHERE Country Like 'U%'

    It returns all customers from a country/region named "U%", not all countries/regions beginning with the letter "U", because the percent sign (%) is not a wildcard character in ANSI-89 SQL.

ShowExample of a query with a duplicate field and alias name

If a query uses an alias that is the same as a base column name and you create a calculated field using the ambiguous name, the query will produce different results under each query mode. For example, if you run the following query:

SELECT Orders.OrderID AS Freight, Orders.Freight, [Freight]*1.1 AS [New Freight] FROM Orders;

Under ANSI-92 SQL, Access calculates the New Freight value using the Freight field, and therefore increases each Freight by 10%. Under ANSI-89 SQL, Access calculates the New Freight value using the Freight alias, and therefore increases each OrderID by 10%.

ShowHow to avoid problems caused by mixing queries under different ANSI SQL query modes in the same database

In general, avoid doing the following to prevent problems caused by mixing queries created under different ANSI SQL query modes:

  • Converting an Access database set to ANSI-92 SQL query mode from 2002 - 2003 file format to 2000 or 97 file format. ANSI-92 SQL query mode is only supported in the 2002 - 2003 file format.
  • Changing the ANSI SQL query mode for the current database after you've created one or more queries. If you must do this, retest the existing queries to ensure that they still run or produce expected results, and rewrite the queries if necessary.
  • Importing queries created under one ANSI SQL query mode into an Access database set to another mode, or exporting queries created under one ANSI SQL query mode to an Access database set to another mode.
  • Changing the ANSI SQL query mode in an existing application. This could break your application and require rewriting your application.

    In Access 2000, you can only programmatically change the ANSI SQL query mode and any queries created under ANSI-92 mode were hidden in the Database window (Database window: The window that appears when you open an Access database or an Access project. It displays shortcuts for creating new database objects and opening existing objects.). In Access 2002 or later, you or a user can change ANSI SQL query mode through the user interface and ANSI-92 queries are no longer hidden in the Database window, so you should prevent accidental or intentional changes to the ANSI SQL query mode of your application by helping protect your code and preventing the changing of the query mode through the application's user interface.

  • Using ambiguous aliases and column names. To avoid confusion, ensure that aliases and column names are always unique in an SQL statement.

advertisement