Microsoft Access tutorials

Tables

Introduction: The importance of good table design

Tables are where data in a database is stored; con-sequently, tables form the core of any database application. In addition to basic data, Access permits a large amount of domain knowledge (such as cap-tions, default values, constraints, etc.) to be stored at the table level.
Extra time spent thinking about table design can result in enormous time savings during later stages of the project. Non-trivial changes to tables and relationships become increas-ingly difficult as the application grows in size and complexity.

Learning objectives

How do I enter and edit data in the datasheet view of a table?
How do I create a new table?
How do I set the primary key for a table?
How do I specify field properties such as the input mask and caption?
Why won’t an autonumber field restart counting at one?
What are the different types of keys?

Tutorial exercises

In this tutorial, you will learn to interact with existing tables and design new tables.

Datasheet basics

If you have not already done so, open the univ0_vx.mdb database file from Tutorial 1.
Open the Departments table. The important elements of the datasheet view are shown in Figure 2.1.
Use the field selectors to adjust the width of the DeptName field as shown in Figure 2.1.
Add the Biology department (BIOL) to the table, as shown in Figure 2.2.
Delete the “Basket Weaving” record by clicking on its record selector and pressing the Delete key.

Creating a new table

In this section you will create and save a very basic skeleton for table called Employees. This table could be used to keep track of university employees
Tutorial exercises
such as lecturers, department heads, departmental secretaries, and so on.
Return to the database window and create a new table as shown in Figure 2.3.

Specifying the primary key

Tables normally have a primary key that uniquely identifies the records in the table. When you desig-nate a field as the primary key, Access will not allow you to enter duplicate values into the field.
Follow the steps in Figure 2.5 to set the primary key of the table to EmployeeID.

Setting field properties

In this section, you will specify a number of field properties for the EmployeeID field, as shown in Figure 2.6.
Since we are going to use the employees’ Social Insurance Number (S.I.N.) to uniquely identify them, set the Field Size property to 11 characters (9 for numbers and 2 for separating spaces)
Set the Input Mask property to the following: 000\ 000\ 000;0
Set the Caption property to Employee ID
Tutorial exercises
Click on the grey box beside the field (or fields) that form the primary key.
To select more than one field for use as the primary key, hold down the Control key while clicking on the grey boxes.
Either click the key-shaped icon in the tool bar or select Edit > Primary Key from the menu.
Home Previous 8 o f 18 Next
Tables
Select View > Datasheet from the main menu to switch to datasheet mode as shown in Figure 2.7. Enter your own S.I.N. and observe the effect of the input mask and caption on the EmployeeID field.
Select View > Table Design from the main menu to return to design mode.
Set the field properties for FName and LName (note that Length and Caption are the only two properties that are relevant for these two fields)

Using the input mask wizard

In this section, you will use the input mask wizard to create a complex input mask for a standard field type. You will also use the help system to learn more about the meaning of the symbols used to create input masks.
Select the Phone field, move the cursor to the input mask property, and click the button with
Discussion
three small dots ( ) to invoke the input mask wizard.
Follow the instructions provided by the wizard as shown in Figure 2.8.
Press F1 while the cursor is still in the input mask property. Scroll down the help window to find the meaning of the “0”, “9”, “>” and “L” input mask symbols.

Discussion

Key terminology

A key is one or more fields that uniquely determine the identity of the real-world object that the record is meant to represent. For example, there is a record in the student information system that contains infor-mation about you as a student. To ensure that the record is associated with you and only you, it con-Try entering various characters and numbers into the EmployeeID field.
If a caption is specified, it replaces the field name in the field selector.
Note that the input mask will not let you type any characters other than numbers from 0-9. In addition, the spaces between the groups of numbers are added automatically.
Press the Escape key when you are done to clear the changes to the record.
Input masks provide a relatively easy way to avoid certain basic data input errors without having to write complex error checking programs. Note, however, that it is possible to over-constrain a field so that users are unable to enter legitimate values.
Select “phone number” from the list of commonly-used field types.
The items in this list depend on the “international settings” specified for Windows (e.g., “Zip Code” may show instead of “Postal Code”).
In Step 2, you may edit the input mask (e.g., remove the area code section).
Since the input mask controls how the information in the field looks, it is possible to save some disk space by storing the data without the extras symbols, spaces, etc. For the size of system we are building, however, this savings is negligible.
The advantage of using student number as a key instead of some other field—like “student name”—is that there may be more than one person with the same first and last name. The combination of stu-dent name and address is probably unique (it is improbable that two people with the same name will at the same address) but using these two fields as a key would be cumbersome.
Since the terminology of keys can be confusing, the important terms are summarized below.
Primary key — The terms “key” and “primary key” are often used interchangeably. Since there may be more than one candidate key for an application, the designer has to select one: this is the primary key.
Concatenated key: The verb “concatenate” means to join together in a series. A concatenated key is made by joining together two or more fields. Course numbers at UBC provide a good example of a concatenated key made by joining together two fields: DeptCode and CrsNum. For example, department alone cannot be the primary key since there are many courses in each department (e.g., COMM 335, COMM 391). Similarly, course number cannot be used as a key since there are many courses with the same number in different departments (e.g., COMM 335, HIST 335, MATH 335). However, department and course number together form a concatenated key (there is only one COMM 335).
Foreign key: In a one-to-many relationship, a foreign key is a field (or fields) in the “child” record that uniquely identifies the correct “parent” record. For example, DeptCode and CrsNum in the Sections table are foreign keys since these two keys taken together are the primary key of the Courses table. Foreign keys are identified in Access by creating relationships (see Tutorial 3).

Fields and field properties

Field names

Access places relatively few restrictions on field names and thus it is possible to create long, descrip-tive names for your fields. The problem is that you have to type these field names when building que-ries, macros, and programs. As such, a balance should be struck between readability and ease of typing. You are advised to use short-but-descriptive field names with no spaces.
For example, in Section 2.3.2 you created a field with name FName. However, you can use the caption property to provide a longer, more descriptive label such as First name. The net result is a field name that is easy to type when programming and a field caption that is easy to read when the data is viewed.
In addition, you can use the comment field in the table design window to document the meaning of field names.
It is strongly recommended that you avoid all non-alphanumeric characters whenever you name a field or database object. Although Access will permit you to use names such as Customer#, non-alphanumeric characters (such as #, /, $, %, ~, @, etc.) may cause undocumented problems later on.

Data types

The field’s data type tells Access how to handle the information in the field. For instance, if the data type is date/time, then Access can perform date/time arithmetic on information stored in the field. If the same date is stored as text, however, Access treats it just like any other string of characters. Normally,
the choice of data type is straightforward. However, the following guidelines should be kept in mind:
Do not use a numeric data type unless you are going to treat the field as a number (i.e., perform mathematical operations on it). For instance, you might be tempted to store a person’s student number as an integer. However, if the student number starts with a zero, then the first digit is dropped and you have to coerce Access into dis-playing it. Similarly, a UBC course number (e.g., 335) might be considered a number; however, since courses like 439B have to accommodated, a numeric data type for the course number field is clearly inappropriate.
Access provides a special data type called Auto Number (Counter in version 2.0). An autonum-ber/counter is really a number of type Long Inte-ger that gets incremented by Access every time a new record is added. As such, it is convenient
for use as a primary key when no other key is provided or is immediately obvious.
Since an autonumber is really Long Integer and since relationships can only be created between fields with the same data type, it is important to remember that if an autonumber is used on the “one” side of a relationship, a long integer must be used for the “many” side.

 “Disappearing” numbers in autonumber fields

If, during the process of testing your application, you add and delete records from a table with an auto-number key, you will notice that the deleted keys are not reclaimed.
For instance, if you add records to your Customer table (assuming that CustID is an autonumber), you will have a series of CustID values: 1, 2, 3… If you later delete customer 1 and 2, you will notice that your list of customers now starts at 3.
Clearly, it would be impossible for Access to renum-ber all the customers so the list started at 1. What would happen, for instance, to all the printed invoices with CustID = 2 on them? Would they refer to the original customer 2 or the newly renumbered customer 2?
The bottom line is this: once a key is assigned, it should never be reused, even if the entity to which it is assigned is subse-quently deleted. Thus, as far as you are con-cerned, there is no way to get your customers table to renumber from CustID = 1.
Of course, there is a long and complicated way to do it, but since used an autonumber in the first place, you do not care about the actual value of the key— you just want it to be unique. In short, it makes absolutely no difference whether the first customer in your customers table is CustID = 1 or 534.

Input masks

An input mask is a means of restricting what the user can type into the field. It provides a “template” which tells Access what kind of information should be in each space. For example, the input mask >LLLL consists of two parts:
The right brace > ensures that every character the user types is converted into upper case. Thus, if the user types comm, it is automatically converted to COMM.
The characters LLLL are place holders for letters from A to Z with blank spaces not allowed. What this means is that the user has to type in exactly four letters. If she types in fewer than four or types a character that is not within the A to Z scope (e.g., &, 7, %), Access will display an error message.
There are a large number of special symbols used for the input mask templates. Since the meaning of many of the symbols is not immediately obvious, there is no requirement to remember the character codes. Instead, simply place the cursor on the input mask property and press F1 to get help. In addition, the wizard can be used to provide a basic input mask which can later be modified.

Input masks and literal values

To have the input mask automatically insert a char-acter (such as a space or a dash) in a field, use a slash to indicate that the character following it is a lit-eral.
For example, to create an input mask for local tele-phone numbers (e.g., 822-6109), you would use the following template: 000\-0000;0 (the dash is a lit-eral value and appears automatically as the user enters the telephone number).
The semicolon and zero at the end of this input mask are important because, as the on-line help system points out, an input mask value actually consists of three parts (or “arguments”), each separated by a semicolon:
the actual template (e.g., 000\-0000), a value (0 or 1) that tells Access how to deal with literal characters, and the character to use as a place holder (showing the user how many characters to enter).
When you use a literal character in an input mask, the second argument determines whether the literal value is simply displayed or displayed and stored in the table as part of the data.
For example, if you use the input mask 000\-0000;1, Access will not store the dash with the tele-phone number. Thus, although the input mask will always display the number as “822-6109”, the num-ber is actually stored as “8226109”. By using the input mask 000\-0000;0, however, you are telling Access to store the dash with the rest of the data.
If you use the wizard to create an input mask, it asks you a simple question about storing lit-eral values (as shown in Figure 2.8) and fills in the second argument accordingly. How-ever, if you create the input mask manually, you should be aware that by default, Access does not store literal values. In other words, the input mask 000\-0000 is identical to the input mask 000\-0000;1. This has impor-tant consequences if the field in question is subject to referential integrity constraints (the value “822-6109” is not the same as “8226109”).

1. Introduction to Microsoft Access
1.1 Introduction: What is Access?
1.1.1 The many faces of Access
1.1.2 What is in an Access database file?
1.2 Learning objectives
1.3 Tutorial exercises
1.3.1 Starting Access
1.3.2 Creating a new database
1.3.3 Opening an existing database
1.3.4 Importing data from other applications
1.3.5 Getting help
1.3.6 Compacting your database
1.4 Discussion
1.5 Application to the assignment
2. Tables
2.1 Introduction: The importance of good table,design
2.2 Learning objectives
2.3 Tutorial exercises
2.4 Discussion
2.5 Application to the assignment
3. Relationships
3.1 Introduction: The advantage of using tables and relationship
3.2 Learning objectives
3.3 Tutorial exercises
3.4 Discussion
3.5 Application to the assignment
4. Basic Queries Using QBE
4.1 Introduction: Using queries to get the information you need
4.2 Learning objectives
4.3 Tutorial exercises
4.4 Discussion
4.5 Application to the assignment
5. Basic Queries using SQL
5.1 Introduction: The difference between QBE and SQL
5.2 Learning objectives
5.3 Tutorial exercises
5.4 Discussion
6. Form Fundamentals
6.1 Introduction: Using forms as the core of an application
6.2 Learning objectives
6.3 Tutorial exercises
6.4 Discussion
6.5 Application to the assignment
7. Subforms
7.1 Introduction: The advantages of forms within forms
7.2 Learning objectives
7.3 Tutorial exercises
7.4 Application to the assignment
8. Combo Box Controls
8.1 Introduction: What is a combo box?
8.2 Learning objectives
8.3 Tutorial exercises
8.4 Discussion
8.5 Application to the assignment
9. Advanced Forms
9.1 Introduction: Using calculated controls on forms
9.2 Learning objectives
9.3 Tutorial exercises
9.4 Discussion
9.5 Application to the assignment
10. Parameter Queries
10.1 Introduction: Dynamic queries using parameters
10.2 Learning objectives
10.3 Tutorial exercises
10.4 Application to the assignment
11. Action Queries
11.1 Introduction: Queries that change data
11.2 Learning objectives
11.3 Tutorial exercises
11.4 Application to the assignment
12. An Introduction to Visual Basic
12.1 Introduction: Learning the basics of programming
12.2 Learning objectives
12.3 Tutorial exercises
12.4 Discussion
12.5 Application to the assignment
13. Event-Driven Programming Using Macros
13.1 Introduction: What is event-driven programming?
13.2 Learning objectives
13.3 Tutorial exercises
13.4 Discussion
13.5 Application to the assignment
14. Data Access Objects
14.1 Introduction: What is the DAO hierarchy?
14.2 Learning objectives
14.3 Tutorial exercises
14.4 Discussion
14.5 Application to the assignment
15. Advanced Triggers
15.1 Introduction: Pulling it all together
15.2 Learning objectives
15.3 Tutorial exercises
15.4 Application to the assignment

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *