# Lesson6.16.Update Queries

### Lesson 6.16. Update Queries

##### Figure 6-38. The update query

You can use an update query to change a pile of records at the same time. For example, you could create an update query to lower prices by eight percent or to change the sales representative for all your clients in Oregon from "Mr. Potter" to "George Bailey." Just like other action queries, you create an update query by first creating a select query and then converting the select query to an update query.

In this lesson you will create an update query to raise the prices of all trips to Europe by 10 percent.

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

The New Query dialog box appears.

2. Select Design View and click OK.

The query design window and Show Table dialog box both appear. Here you have to select the tables and/or queries you want to use in the update query.

3. Double-click the tblTours table and click Close.

Now you need to convert the select query to an update query. Here's how:

4. Click the Query Type button list arrow and select Update Query from the list.

Access converts the select query to an update query. Notice an Update To row appears in the design grid. Now you have to specify the fields you want to update.

5. Double-click the TourID, Normal Price, and First Class Price fields in the tblTours field list.

The next step is a little bit trickyyou have to tell Access which fields to update and how to update them. You want to raise the price of both the Normal Price and First Class Price fields by 10 percentyou will have to write an expression (or formula) in the Update To rows of both fields to make this little bit of magic happen.

6. Click the Normal Price column's Update To row and type [Normal Price]+([Normal Price]*.1).

So what does this confusing expression actually mean? Let's assume the Normal Price for a record is \$100. The expression would then look something like this: [\$100] + ([\$100] * .1).

Access first calculates anything it sees in parentheses, so it multiples \$100 x 0.1 (or 10%) and comes up with \$10. Access then adds this \$10 to the Normal Price (as in \$10 + \$100) and comes up with \$110. Make a little more sense? Hope sobecause you have to do the same thing to the First Class Price field.

7. Click the First Class Price column's Update To row and type:

[First Class Price]+([First Class Price]*.1).

Next you need to specify any limiting criteria. You want to raise prices for only those tours that are in Europe. Sadly, the only way to determine this is by looking at the values in the tblTours table. Here are all the European TourIDs: 1, 2, 3, and 9. You will have to create an OR statement in your query to make sure you get them all.

8. Click the TourID field's Criteria row and type 1.

This will select the record whose TourID is "1," but what about the rest?

9. Press the (down arrow key) to select the Criteria row (the or row) and type 2.

This will select the records whose TourID is "2." Go to the next step and finish adding the rest of the OR criteria.

10. Press the (down arrow key) to select the Criteria row, type 3, press the (down arrow key) to select the Criteria row and type 9.

Compare your query to the one in Figure 6-36. Look the same? Let's run the query.

11. Click the Run button on the toolbar.

Access asks about the updating the selected records, as shown in Figure 6-37.

12. Click Yes.

Access updates the selected records by raising both their Normal Price and First Class Price fields by 10 percent. Let's open the new table.

13. Close the query without saving your changes, click the Tables icon in the Objects bar, and double-click the tblTours table.

Compare your table with Figure 6-38. Notice that the prices of four European tours have been increased by ten percent.

14. Close the tblTours table and close the database.

## QUICK REFERENCE

TO CREATE AN UPDATE QUERY:

1. CREATE A NEW QUERY IN DESIGN VIEW, THEN SELECT THE TABLES AND/OR QUERIES YOU WANT TO USE IN THE UPDATE QUERY.

2. CLICK THE QUERY TYPE BUTTON LIST ARROW ON THE TOOLBAR AND SELECT UPDATE QUERY.

OR...

SELECT QUERY

DOUBLE-CLICK THE FIELDS THAT YOU WANT TO APPEAR IN THE QUERY OR CLICK AND DRAG THE FIELDS ONTO THE DESIGN GRID.

3. ENTER AN EXPRESSION TO UPDATE THE SELECTED FIELD.

4. ENTER ANY CRITERIA, IF NEEDED, TO SELECT WHICH RECORDS SHOULD BE UPDATED.

5. CLICK THE VIEW BUTTON TO VIEW THE RESULTS OF THE UPDATE QUERY.

6. IF YOU'RE SATISFIED THAT THE APPROPRIATE RECORDS WILL BE UPDATED, CLICK THE RUN BUTTON ON THE TOOLBAR AND CLICK YES TO CONFIRM THE UPDATE.

Access 2003 Personal Trainer (Personal Trainer (OReilly))
ISBN: 0596009372
EAN: 2147483647
Year: 2005
Pages: 209

Similar book on Amazon