You need to use what you just learned and what you learned in the previous project, and maybe learn a little more. The exercises with databases will require more steps than in the other lessons!
Complete all parts of the following exercises. Don't forget to backup your Class disk when you have completed the exercises or whenever you stop for the day and saved a document along the way.
What you will do: | Save and use a copy of a database Change properties of fields Create relationships Create and use an update query Create and use a parameter query Compact and repair database Create select and totals queries with criteria Create a calculated field in a query Print documentation - datasheets, documenter report, relationships, object dependencies |
Start with: Access open, gamescores.accdb from the resource files
People who play games like to keep score. Lots of scores! Some computer games track lots of things during the game but do not save all of that lovely information to compare with your next attempt. You can do that in your own Access database!
You will be working with a database that records scores for each of the
76 levels in a popular computer game, Zuma®, and some totals for
successfully completed games. The resource file gamescores.accdb provides you with actual scores
from actual games, played by me.
(Yes, it was a lot of 'work'. But it was all for YOU! I confess that
this was not the first time I played Zuma. It took a lot of practice to get good enough
to actually finish all of the levels!!)
If you have never played it, I have written a short explanation: How Zuma works
Disclaimer: I do not have an investment with or any
relationship with the creators of Zuma. I just enjoyed the game enough to
buy it!
Warning:
Playing computer or console games for a long time can damage your wrists
and back. Take breaks. Gradually increase your playing time, just
like for any other physical skill. Too much practice can hurt! Tendonitis
is not fun!
Create a copy to work with, using the name ex3-1-gamescores-Lastname-Firstname.accdb , with your own first and last names, of course.
Access 2007: Office button > Save As
Access 2010, 2013, 2016: File tab > Save As > Save Database As
The new copy opens in Access, ready for your changes.
If you see a security warning, click the Enable Content button.
This database has 3 tables, Level Scores, Levels, and TotalScores with data already entered.
Some of the field properties are not particularly well-chosen. You will change some of them. When data has already been entered, some changes are more dangerous than others!
TotalScores table:
About the table: This table holds the final score and statistics for completed games. The values in the Time field appear to be clock times, like 2:21:15 AM. They are actually elapsed times, like 2 hours, 21 minutes, 15 seconds. You have two choices - use one of the date/time formats or change to Text type and create your own format. The easy way is to use an existing format! That also allows you to do arithmetic with the times which you cannot do with text data. (Do you see something coming later in this exercise??)
Remember: What Access actually stores is not always what you see!! Times and dates are really just numbers. A time value is the decimal part of a date/time value, while the whole number part of the value is the number of days since the starting date, 12/30/1899. Times without a date attached have zero as the whole number part, like 0.058574.
Did you see?
In TotalScores, one game finished much faster than the others with
a much lower score. What happened? I thought I had paused the game when the doorbell
rang, but the game kept running while I was gone until all my lovely
stack of lives was used up.
Levels table:
About the table: The field AceTime shows what time the game designers challenge you to beat.
You don't have to guess at how big the field sizes should be. You've got actual values to look at! You will now change those field sizes to reduce the size of the database. The larger the database, the more effect field sizes have on the total size of the database and therefore on the speed of actions like sorting and filtering.
Change the Field Size for the fields Stage and Level from Long Integer to Byte.
The size Byte will hold only whole numbers from 0 to 255. Long Integer numbers use 4 bytes.
Since our numbers must be between 1 and 13, this will work out great!
Another choice would have been to change these two fields to Text fields with a size of 2 characters. We are not going to do any arithmetic with the stage and level values!
Level Scores table:
About the table: In the Level Scores table, the field LevelID is a foreign key. It is the primary key for the table, Levels. It must be a Long Integer size in Level Scores, even though we know that the numbers only go up to 76 for this game, because the field is in an AutoNumber in the Levels table. That is the nature of keys.
There are some missing values for level scores. Somehow I simply failed to record those levels! I know it is hard to believe, but I cannot deny that those blanks are there. Data entry can be hard. It's not that it is hard to do. It's just hard to remember to do!
You might think that I could reconstruct a missing score by adding up the other levels and subtracting the sum from the TotalScore for the whole game. Sounds good, but... there are bonus points added at the end of a game, so the difference will not be just from the missing level score. <sigh>
We don't need to change any of the field properties but the table name needs a change. Also, to join this table to the TotalScores table, we need to add a field.
Drag the LevelScores table
from the Navigation pane and drop it in the Relationships diagram.
The join you created before comes back. Whew! That's a relief. There was
no warning about this effect.
Resize the tables at the top of the Relationships window to show all of the field names.
Inspect the Relationships: Do you see a field that you could use to join LevelScores and TotalScores?
There is not
one! We need to add a field to LevelScores that tells us which TotalScores record the level belongs to.
Good database practices would have created a blank TotalScores record before starting the new game. Then the correct TotalScore ID could be entered with a level score. After all, it is possible to play under different user names so that you have several unfinished games in progress at the same time.
Problem: No subdatasheets
in the TotalScores table
Access needs to refresh its understanding of the table after you
create a new relationship.
Solution: Close the table and open it again.
In the LevelScores table, the TotalsID column is blank. You will create a parameter update query to put values in that column. The value for the TotalsID field is the same for a whole set of levels, which is 76 records. That's because there are 76 levels whose scores are added up to make a Total. These levels were added to the LevelScores table all at once, so the LevelScoreID values are in sequence. The Level numbers start over again when the scores go with a new total. That makes it easy to create an update query to put in the TotalsID.
Since you reduced some field sizes, it is likely that the database file is taking up more room on the disk than necessary.
Now that your tables are neatly related to each other, you can create some queries to answer some questions!
Question: Are there missing scores or finish times?
Drag the * from the table field list to the design grid for
each table.
Drag the field FinishTime from the LevelScores table to the grid.
When you use the * to include all fields and you want to use criteria, you must add the fields for criteria separately.
Inspect the datasheet for LevelScores.
There is one null cell and 2 with zeros. Access does not think of zero as the same as Null. A Null values means that
no value has been entered. Zero means "0" was entered. An important
difference!
Some fields have a default value set to zero to avoid this issue.
Question: What was the highest score for any level?
Question: What are the highest scores for each level?
Run the query.
The datasheet shows the maximum value for each of the named
levels but there are only 22 records instead of 76. What happened? The query grouped on LevelName. But the level names and the associated map repeat as you go through the game, but they become more difficult each time. A level appears in the game as many as 4 times or as few as once. So there are 22 different maps.
Question: Can you tell quickly which of these "highest" values is the smallest?
An easy way exists so don't save yet!
Question: Which level has the smallest of the high scores?
Question: What is the fastest time for a level?
Use the Min function to find the shortest time.
Run the query.
Whoops! The value shown is 12:14:00 AM! Where did THAT come from?
About Time values: (Working with dates and times is tricky!!)
Access records time values as the decimal part of a number where the whole number represents the date. It does its arithmetic with those decimal numbers and then re-interprets the result according to the format that you chose. Access thinks that your 0:14 is a Date/Time value for 14 minutes after midnight!
Access does not have special formats or functions for handling elapsed time! You could write a Visual Basic function to do this,
but that is beyond the level of these lessons!! It's amazing how
difficult some ordinary things can be to do on a computer.
Question: Which times are faster than the AceTime?
Ace Time is what the game designers set as a goal for you to beat.
Question: How many of the FinishTime values are slower than the AceTime for each level? For which levels is it the hardest to beat the AceTime?
Since you have added a number of objects (queries) to the database, it is likely that the database file is once again taking up more space than it really needs.
Now, create your own queries to find the answers to the following questions:
Question: What is the average of the scores for all the levels?
Create QAvgOfScore,
which produces just 1 number.
Question: What is the average score for the level that has a name
containing "mix"?
Create QAvgOfScore-mix (Use the Like operator in the Criteria row. This is a very useful
operator when you can't quite remember the text value you are looking
for!)
What other questions can you ask and answer with this database?
Question: Write your own question and create a query that finds the answer. Save your query with a name like QMyQuestion-FirstnameLastname where you use your own name and add a phrase that makes it clear what the question was, like QMyQuestion-MaxExtraLifeBonus-JanSmith.
There are LOTS of Documenter reports now available to you - one for each table and for each query. You can also print datasheets for each of those tables and queries. You can print the Relationships diagram. We won't do all of that! You do need to do a little printing, just to see what the limitations are.
Write your name and "Ex. 3-1" at the top left of each print-out
below.
(Unfortunately you cannot configure the page's header to include
such information automatically. Wait for Reports!)
Open a Documenter report for the table LevelScores.
Zuma® - ©2003-2006 PopCap Games, Inc. Zuma is a registered trademark of PopCap Games, Inc. All rights reserved.
This exercise uses a file from the databases resource files.
You cannot make changes to these files and save them in the same place. Save the changed documents to your Class disk. This keeps the original resource files intact in case you need to start over or another student will be using this same computer.
How to handle a full disk