Queries you create with the Unique Values property set to Yes to add the ANSI SQL DISTINCT modifier to the SQL statement aren't updatable. If you set the Unique Records property, instead of the Unique Values property, to Yes, some queries are updatable because Unique Records substitutes Jet SQL's DISTINCTROW modifier for DISTINCT.
Unique Records queries create Recordset objects of the updatable Dynaset type. You can't update table data with a query unless you see the tentative (blank) append record (with the asterisk in the select button) at the end of the query result table. The next few sections describe the conditions under which you can update a record of a table included in a query. The following sections also discuss how to use the Output Field Properties window to format query-data display and editing. Note You can't set both the Unique Values and Unique Records properties to Yes these choices are mutually exclusive. In Access 2003, the default setting of both the Unique Values and Unique Records properties is No. Characteristics That Determine Whether You Can Update a QueryAdding new records to tables or updating existing data in tables included in a query is a definite advantage in some circumstances. Correcting data errors that appear when you run the query is especially tempting. Unfortunately, you can't append or update records in many queries that you create. The following properties of a query prevent you from appending and updating records:
When designing a query to use as the basis of a form for data entry or editing, make sure that none of the preceding properties apply to the query. Tip You can't edit data returned by a query with three or more tables in Query Datasheet view, unless the query is one-to-many-to-many, but you can update values in other types of three-table queries in forms and data access pages (DAP) that are bound to the query. To make the query updatable with forms and DAP, set the Recordset Type property value to Dynaset (Inconsistent Updates). If none of the preceding properties apply to the query or any table within the query, you can append records to and update fields of queries in the following:
Updating the one table in a one-to-many query is a special case in Access. To enable updates to this table, follow these steps:
After following these steps, you can edit the nonkey fields of the one table. You can't, however, alter the values of key fields that have relationships with records in the many table, unless you specify Cascade Update Related Fields in the Relationships window's Edit Relationships dialog for the join. Otherwise, such a modification violates referential integrity. By adding lookup fields to tables, you often can avoid writing one-to-many queries and precisely following the preceding rules to make such queries updatable. For example, the Orders table, which includes three lookup fields (CustomerID, EmployeeID, and ShipVia), is updatable. If you want to allow updates in Datasheet view (called browse updating), using lookup fields is a simpler approach than creating an updatable query. Most database developers, however, consider simple browse updating to be a poor practice because of the potential for inadvertent data-entry errors. As mentioned earlier, browse updating with lookup fields is especially prone to data-entry errors.
For more discussion of the browse-mode method and other alternatives, see "In the Real World Alternatives to Action Queries," p. 514. Taking Advantage of Access's Row Fix-Up FeatureAccess queries and SQL Server views have a row fix-up feature (called AutoLookup by Access) that fills in query data when you add a new record or change the value of the foreign key of a many-side record. To take advantage of row fix-up, your query must include the foreign-key value, not the primary-key value of the join. Northwind Traders' Orders Qry is an example of a query that uses row fix-up. Orders Qry includes every field of the Customers and Orders tables, except the CustomerID field of the Customers table. To demonstrate row fix-up, do the following:
Row fix-up is more useful for forms that are bound to a query than for queries that update data in Datasheet view. Orders Qry is the data source for the sample Orders form. When you add a new order with this form, row fix-up automatically updates its customer data. Formatting Data with the Query Field Properties WindowThe display format of data in queries is inherited from the format of the data in the tables that underlie the query. You can override the table format by using the Format(ColumnName, FormatString) function to create a calculated field. In this case, however, the column isn't updatable. Access provides an easier query column formatting method: the Field Properties window, which you can use to format the display of query data. You also can create an input mask to aid in updating the query data. To open the Field Properties window, place the cursor in the Field cell of the query column that you want to format, and then click the Properties button of the toolbar. Figure 11.47 shows the Field Properties window for the OrderDate column of the Orders Qry. Specifying formats in queries lets you alter the column's display format without affecting the display of table fields. Figure 11.47. Access's Medium Date format (dd-mmm-yy) doesn't comply with Y2K requirements. To specify a four-year Medium Date format for all Windows operating systems, assign the dd-mmm-yyyy format.
By default, Access 2003's General Date, Long Date, and Short Date formats display four-digit years, which is required for Year 2000 (Y2K) conformance. (Four-digit years is the default for Windows XP and 2000.) You can alter the default Short Date or Long Date format in text boxes of the Date page of Control Panel's Customize Regional Options dialog. (Windows 2000's tool is called Regional Options.) Systemwide settings specify the General Date, Long Date, and Short Date formats, but don't affect the Medium Date style. To obtain a four-digit year display with Medium Date format, you must type the Format descriptor string dd-mmm-yyyy in the Format text box. Most sample tables in Northwind.mdb have the custom dd-mmm-yyyy format applied. Note The format symbol for month in the [Customize] Regional Options dialog is "M", not "m", which is the systemwide symbol for minutes. Access and VBA use "n" for minutes. Tip Always use the default Short Date and Long Date systemwide formats. Don't depend on users to change their default formats. If you want to specify two-digit day and month values, for example, use a custom date format, such as mm/dd/yyyy. The Field Properties window displays the following subset of the properties that apply to a query's fields:
For more information on the Input Mask Wizard and a listing of placeholders, see "Using Input Masks," p. 173.
To learn more about smart tags, see "Working with Object Dependencies and Access Smart Tags," p. 200. Each of the preceding query properties follows the rules described in Chapter 5 for setting table field properties. Adding a value (Received) for the Caption property of a query against the Orders table is the equivalent of adding a column alias by typing Received: as a prefix in the OrderDate column's Field cell. Adding a Caption property value, however, doesn't change the SQL statement for the query. The value of the Input Mask property need not correspond exactly to the value of the Format property, but input mask characters don't appear if you try to use a Short Date mask with a Medium Date format you apply in the query. Tip Add captions to queries, not tables. Table Datasheet view should display field names rather than captions to conform to good database design principles. Unfortunately, the tables in Northwind.mdb don't conform to this recommendation. For example, the Received (OrderDate) column in Figure 11.48, which shows the effect of setting the property values shown in the preceding list, has a single-digit (no leading zero) month and day for the Short Date display format, which overrides the mm/dd/yyyy format of the table field. The input mask (99/99/0000;0;_) permits updating with one-digit or two-digit months and days. Adding or editing a single-digit or two-digit entry gives the same result. Most typists prefer to enter a consistent number of digits in a date field. Figure 11.48. This query uses m/d/yyyy display format and a 90/90/0000 input mask to allow month and date entries as single- or two-digit values.
|