My Access Handout 3A
My Access Handout 3A
Tables are the most important component of an Access database because tables are where all of your information is stored.
Tables in an access database are similar in many ways to Excel tables. Most Access databases will consist of more than
one related table, but the first database we will create will be a simple, single-table database.
FIELDS
Creating a Table
1. Begin by making sure you are still in the Tables section of the database window.
2. Click the NEW button to create a new table.
3. The New Table dialog will appear. Select Design View from the list of options and click OK. A new blank
table will now appear in the design view.
The top section of the window is where you enter the names of the fields in your table along with the data type for
each field. When you have a field name selected in the top section, the bottom section will display properties
which allow you to customize the selected field
Creating Fields
1. Make sure the first row in the Field Name column is selected.
2. Type Last Name and press [Tab] or [Enter]. This will move you to the Data Type column.
3. Leave the Data Type as Text and press [Tab] or [Enter] to move to the Description column.
4. In the Description column type Last name of the student and press [Tab] or [Enter] to move on to the next
line.
5. For the next field enter First Name as Field Name, Text as Data Type and First name of the student as the
Description.
6. For the next field enter Date of Birth as Field Name.
7. In the Data Type column we want to change it to Date/Time. An easy way of selecting Data Types is to press
the first letter of the one you want. Press the letter D and Date/Time will become selected. Enter Birth date of the
student for the Description and move to the next line.
Note it is not essential to provide a description for each field but it is a good idea. These descriptions can be
used to provide information for people using the database.
You can also insert a new row by right-clicking on a row and choosing Insert Rows or by clicking the icon on the
toolbar.
An AutoNumber field is the only one where you don’t need to type anything in to a record. For each new record
entered, Access will automatically place a new number in the AutoNumber field. It is useful as a primary key field as
it ensures that each record will have a unique number.
6) From the Edit menu select Primary Key. A key symbol will appear next to that field.
You can also set a field as a primary key by right-clicking and selecting Primary Key or by clicking
the icon on the toolbar. If you accidentally set the wrong field as a primary key, then simply select
the intended field and choose the primary key option once again to set it as the new primary key.
Saving a Table
It is usually best to specify a primary key for a table before saving it as we have done. Otherwise, Access will want to
create an extra field as a primary key. When a table is saved, it doesn’t become a new file. It becomes an object within the
database file you have already created. In all other ways though, it is similar to saving a file.
1) From the File menu choose Save (you can also press [Ctrl][S] or click the SAVE icon).
2) For the table name type STUDENTS.
3) Click OK to finish saving the table.
Note You can call the table whatever you want but it is best to choose names that describe what sort of information the
table will store. This helps when there are several tables in the database. Also, some developers like to name tables in
uppercase letters and queries in lowercase letters or use prefixes in names to make it easy to distinguish between the two.
Entering a Record
While a table is open, you can quickly switch between the design view (used for modifying the design of the table) and
the datasheet view (used for working with the actual data in the table).
1) Click the View icon on the toolbar (first icon on the far left). This is a quick way to switch between views. You
can also change views from the View menu.
2) Notice that the first column is for the Student Number field and it is filled with the text (AutoNumber). We
don’t need to enter anything in this field since a student number will be generated automatically (remember
that’s what an AutoNumber field’s for) so press [Tab] to move to the Last Name field.
3) Type Robbins as the Last Name. Notice that as soon as you enter information, a number will appear in the
Student Number column. Every record will be given a unique number. If you delete a record, that number
won’t be used again.
4) Press [Enter] after typing the name and the cursor will move to the next field.
5) When you reach the last field (Comments) press [Enter] again to move to the next line.
As soon as you move off that row, the record will be saved. Changes to table data are saved automatically in
Access. The only time you need to save any changes yourself is when you’ve modified the structure of a
database by adding or modifying the design of objects (such as tables). Changes to data are saved for you.
6) Click the View icon to return to the Table Design view.
Table Properties
A database will often be used by a lot of different people and the person designing it will not always be one of the users.
For that reason, it is important to ensure that the database is designed to be easy to use. A table’s fields each have several
properties that can be changed to make the table easier to use. The properties available for each field will vary depending
on data type. The following table provides a summary of the purpose of each field property.
1) Click once on the STUDENTS table and then click the Design button above the list. This will return you to the
table design view. We will modify the field size for some of the fields.
2) Click on the Postcode field. At the bottom of the window you will see a list of properties for the selected field.
3) Since we will only use this field for Australian postcodes, we will limit the field size to 4 characters. Click in
the Field Size property and change the number from 50 to 4.
4) In the top part of the window, click in the State field.
5) This field will only be used to store 2 and 3 letter abbreviations for state names such as WA and NSW. Change
the Field Size property to 3.
6) In the top part of the window, click in the Mark field.
7) For number fields, you have a range of Field Size options to choose from (such as byte, single and integer).
To find out more about any property you want to modify, you can use the [F1] key to go straight to help
about the selected property. Press [F1] now to find out more about the options for the Field Size property.
8) Read through the information about the different field sizes that appears in the help window on the side of the
screen. When you are finished, close the help window.
9) For our marks field we want an option that allows for decimal places but doesn’t have a large number range.
Single would be a suitable option so choose that for Field Size.
10) Click or press [Ctrl][S] to save the changes to the design of the table.
1) Select the Date of Birth field from the top section of the window.
2) Click in the Format property at the bottom. An arrow to the right of the property indicates that there are some
built-in formats that can be chosen from a list.
3) Click the arrow to display the list and choose the Short Date option. This will mean that any dates in this field
will be displayed as dd/mm/yyyy so that the 1st of January 2004 would display as 01/01/2004.
You can also create your own custom formats for any field. Remember that you can use the [F1] key for help
on creating custom formats.
4) In the Format property for the date, delete Short Date and change it to dd-mm-yy. This will mean that the date
from the example in step 3 would appear as 01-01-04.
5) Save the changes to the table design.
1) Select the Default Value property for the State field. We will assume that most of the addresses will be in
WA so we will make that the default value.
2) Enter WA for the default value.
3) Save changes to the table design.
TIP If you have a field where you want the default value to be the current date, you can use the =Date()
function. Whenever you enter a record, Access will enter the current date in that field.
In this instance, an input mask could be entered to ensure that everyone enters phone numbers the same way.
1) Select the Phone field from the top section of the window and then click in the Input Mask property at the
bottom.
2) A button will appear to the right of the property […]. Click on this button to begin the Input Mask Wizard
(you may be prompted to save changes to the table first).
3) In the first step of the wizard, make sure the first phone number option is selected.
4) Press [Tab] to move to the Try It: box.
5) Type in your own phone number to see a sample of how the input mask will work. Notice that the brackets and
spaces are put in for you so you only need to enter the numbers.
6) Click Next to move to the next step of the wizard.
7) The top part of this step will show the code for the input mask as !\(999) 9900\ 0000. We only want a 2
digit area code so change it to !\(99) 9900\ 0000. Leave the placeholder character as it is and click
Next.
8) For the final step, choose the first option, With the symbols in the mask, and click Finish.
9) The Input Mask property will now contain !\(99) 9900\ 0000;0;_ (remember you can press [F1] for
an explanation of what the input mask code means).
10) Save the changes to the table design.
1) Select the State field in the top section of the window and then select the Validation Rule property in the bottom
section.
2) For the Validation Rule enter ACT or NSW or NT or QLD or SA or TAS or VIC or WA. When you press the
[Enter] key after typing the rule, Access will place “ ”s around each entry. This rule will mean that the user
of the database can only enter information that matches one of the entries in the rule. Now we will enter
Validation Text in the next property.
3) For the Validation Text property enter, Must be an Australian state. This means that if someone enters
something that is not allowed by the rule, this text will appear as an error message.
4) Enter the following Validation Rule and Validation Text for the Date of Birth field.
This rule will only accept dates that are earlier than Date() (which means the current date in Access).
Obviously we won’t have any students that haven’t been born yet, so any future dates entered would be an
error. Validation rules and text can let a user know when they have made an error such as that.
Note If you create a validation rule, make sure you also provide a clear validation text message. If a user gets
an error message, they’ll want to know why there’s an error.
1) Select the Last Name field. We want to set the filed properties so that the user has to make an entry in this field.
2) Change the Required property to Yes (wherever you have a drop-down list for a property you can double
click on the to quickly change it to the next available option which would be Yes in this case). When the
Required property is set to Yes, the user must enter something in this field or they will get an error.
3) Change the Allow Zero Length property to No. When the Allow Zero Length property is set to No,
entering a blank space and nothing else in the field will not be accepted as a valid entry.
4) Change the Indexed property to Yes (Duplicates OK).
When a field is indexed, it means that Access will keep track of what has been entered in to that field. That
means that Access will be able to find information in that field a lot quicker which speeds up operations such
as sorting, searching and querying. It is similar to the way an index in a book allows you to find information
quicker without you have to search one page at a time. It is a good idea to index any field that is likely to be
used a lot in searches and sorting. The Duplicates OK part simply means that it will be OK to have more than
one record with the same last name in the field.
2) The student record you entered before will be displayed. Click in the Last Name field underneath Robbins.
3) Type Stevens for the Last Name and press [Enter].
4) Type Sarah for the First Name and press [Enter].
We will test the Validation Rule by entering a date that’s not accepted. Enter a future date such as 020809
(remember you won’t need to enter the / since the input mask puts them in there for you). As soon as you
press [Enter], the Validation Rule will recognise that the entry is invalid and the Validation Text will
appear.
6) Click OK to clear the message. You won’t be able to proceed until you enter a valid date or press [Esc] to
cancel the entry. Enter 100489 (10/04/89) for the date and press [Enter] to proceed to the next field.
7) Enter 24 Browne Ave for the Address Field and Yokine for the Suburb field.
8) Close the table when complete. The new data will be saved automatically in the Database.
1) From the Database Window, make sure the STUDENTS table is selected.
2) From the Insert menu, select AutoForm.
A form will be created automatically using the Fields and Records from your table.
You can move from one record or Field to another using your mouse or the keyboard.
To use your keyboard:
• Press ↓ or [Tab] to move to the next field and ↑ and [Shift][Tab] to move to the previous field.
• Press [Page Down] to move to the next record and [Page Up] to move to the previous record.
• Press [Ctrl][End] to move to the last record and [Ctrl][Home] to move to the first record.
• If you are already on the last record, moving to the next one will give you a blank record. To use your
mouse, click the icons at the bottom of the form.
3) After you have entered the last record, close the form. You will be prompted to save.
4) Since this was only a temporary table to use for data entry, click No.
• In a table with a lot of records, you may not want to have to go all the way to the bottom to add a new record.
• If you have several records to enter, it is a lot easier to check what you have just typed when the new records are the
only ones displayed.