An update query selects
certain fields in existing records and changes values in them. Why would you want to do such an update?
A well-designed database stores data in only one location. However, primary keys can be used in other tables as foreign keys, so those values can appear in several tables, even when following good design principles. Real life databases run into odd and complex issues.
Cascading updates: When you change the primary key for a record and cascading updates are
allowed, all instances of that value in all related tables are
automatically updated. This is usually a very good thing!
AutoNumber primary keys: Access will not change primary keys in an AutoNumber field.
Danger with some
update queries: If you run an update query a second time, calculated
values may change yet again! Is that what you want to happen? The step-by-step below describes this type of
query.
Do you remember these warnings now?
No Undo: You cannot "undo" an action query's action.
Backup first: Before running an action query, be sure you have a current backup of the database or an extra copy of ALL tables that will change.
Verify selection first: Be sure the query selected the records that you thought you did! View the datasheet to see the query's datasheet. Check the records carefully. Then, run the action query with the Run button
.
In the Language Arts Classes database there is a set of update queries
that assign a letter grade to scores. This was necessary in earlier versions of Access which did not have the Calculated data type for a field in a table. Let's look at the query Assign A to >=94.
Problem: How to calculate a letter grade based on a number grade?
A Simple Solution: Create a set of update queries, one for each letter
grade.
A query that would update any number grade to the correct letter grade is a much more complex solution!
It would take a whole set of 'nested IIF expressions.
Steps in the Process:
Create a Select query based on the table you want to update and include only fields that will be updated or that help you select the records to update.
Set criteria that select the records.
Change the query to an Update Query.
A new row appears in the grid,
Update To.
Enter the new value in the appropriate field.
View the datasheet.
The example query uses only one table, Scores, and only two fields from that table, LetterGrade and Score. The update query looks at the value in the Score field. If it meets the criteria >=94, then the LetterGrade field is updated to A, the value in the row Update To. Similar queries have criteria for the other grade ranges and assign the correct letter grade for each one.
The advantage of using update queries to assign letter grades is that they won't make a mistake! And, it avoids a lot of data entry. I really liked that part!
Of course if you change the original value for Score, the letter grade does NOT automatically update with this method. This is a serious disadvantage!
The example update query won't help either because it requires the Letter Grade field to be null! So it is still possible to have an incorrect value in the table. This kind of synchronization error drives database designers crazy. They spend many hours trying to make it impossible, or at least difficult, to make this kind of error. Since I was writing the database for my own use only, I just had to remember how things worked and work around glitches like this. Not a good plan if anyone else had been using the database!
A field with the new Calculated data type would automatically update when the original score is changed. Much, much, much better!
Experiment: Update Query
In the Language Arts Classes database, enter some scores in
the Scores table for A/T ID 90, the partial records that were added the
example Append query. Run the various update queries to assign letter grades to
those scores.
For even more practice, create a new assignment in the Assignments/Tests table, append records, add number scores, and use the update queries to get the letter grades for the new scores.
![]() |
Step-by-Step: Update Query |
![]() |
What you will learn: | to create a Select query that uses OR in criteria to change the query to an update query and run it to verify that the updates were done |
Start with: , Projects database from previous lesson open.
An update query is useful when a value needs to be changed and it appears in several records or several tables. When postal code areas or telephone code areas are split, you may have many entries to update. The hard part is figuring out a way to select the records that need to be changed.
For this section, you will be updating the Budget values for projects that meet certain criteria. We want to pick out the projects that have not finished yet (Cost would be zero or null!) and those with large budgets. Those budgets will be increased by $500.
Again, with just a few records there is no real need to use a query. Practice, however, IS what you need!
Create a new query based on the Projects table.
Drag the fields ProjectID, Cost, and Budget to the design grid.
Add criteria to find projects with large budgets that have not finished yet:
Cost: 0
Budget: >2000
View the query's datasheet.
No results! But there should be two, #6 BMA Medical Conference and #8 Celebrate Argentina. These two have budgets over $2000 and no cost entered yet.
What happened?
The blank cells for cost for those records are NULL, not with a value assigned of zero. It is a subtle difference that Access 2007, 2010, 2013, and 2016 seem to handle a bit differently than previous versions.
Problem: Query did find some results
You did not delete the default values of 0 when the records were created.
Solution: Open the Projects table and delete the $0 default values for these records.
Edit the criteria row for the Cost columns:
Cost: 0 or is null.
That covers both situations!
Notice that your typing is reformatted to Is Null. How kind of Access!
View the query's datasheet.
Success - two records!
Switch back to Query Design View.
Click the button
Update Query on the ribbon tab Query Tools: Design.
A new row appears in the design grid
called Update To:
In
the Update To row, in the Budget column, type:
[Budget]+500
This makes Access calculate the new value by adding 500 to the old value.
Square brackets: [ ] surround a field name in expressions.
View the datasheet again.
Only one column shows, the one to update. But the values did not change!
This is unexpected. You will not see the new values until you actually run the query.
Switch back to Query Design View.
Run the query.
A message appears to tell you that you are about to update so many rows.
Click on Yes.
You are back in the query's design grid. Did anything change? You cannot tell from here! You must go
look at the Projects table (see next section).
Running this query
again will increase the new values by another 500! There will be no
warning!!
Open the table Projects in Datasheet view.
The two values have been updated.
Save the query as QUpdate Budget.
Close the query and the table.
View the list of queries in the Navigation Pane.
Can you pick out the action queries
in the list without reading the names?
Thinking to the future, which of these queries really needed to be saved? Any of them?
Are the names clear enough? If not, what would you suggest as a
method for naming queries? For naming action queries?