0% found this document useful (0 votes)
29 views42 pages

Lesson 2

Uploaded by

cmhbr2mwph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views42 pages

Lesson 2

Uploaded by

cmhbr2mwph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Table design view

• So far we have been opening tables in Datasheet View where we


can view and change data in rows of a table. When in Datasheet
View we can switch from datasheet view to Design View

When the Design View icon is clicked, the display changes: the icon
becomes a Datasheet View icon and the display changes to reveal design
information .
Table design view
Generally we want data in a database to be reasonable and correct. We can
use datatypes and properties to achieve certain types of correctness.
Consider the following integrity rules as rules we would like to enforce:
• Call numbers, titles, and authors are alphanumeric. Any text you can
type on the keyboard is acceptable.
• Each call number must be unique (there can be no duplicates)
• Each book must have a title
• A value for call number must be no more than 50 characters long
• A value for title must be no more than 255 characters long
• A value for author must be no more than 255 characters long
• The author field can be left out (it can be null).
We discuss how these integrity rules are obtained in Table Design View
Table design view
Table design view
• Beside the callNo field you can see the key icon. This means the
callNo field is the primary key. A primary key is a unique identifier –
every row in the table must have a unique value in that field. Every
table should have a PK specified and there can be only one PK for a
table. When a field is defined as the PK then a value must be provided
in each and every row.
• The callNo field has a datatype of Text and a field size of 50. Any value you can
type on the keyboard is acceptable but the overall length, number of
characters, is restricted to at most 50.
• The callNo field is indexed and in this case no duplicates are allowed. The
index constructed by MS Access is similar in purpose to the index at the back
of any book: the index allows MS Access to quickly locate a specified row.
However, this index is different from that at the back of a book because it
allows only one entry per indexed value (No Duplicates is specified for the
Indexed property). Each call number is unique.
Table design view
For title:
• The title field has a datatype of Text with a field size of 255. A
text field can comprise any combination of letters, digits, and
punctuation. Any value entered by a user cannot exceed 255
characters in length.
• A value is required. When entering data for some book, the
user cannot omit the title.
• There is no index on title.
For author:
• The author field has a datatype of Text with a field size of 255.
A text field can comprise any combination of letters, digits,
and punctuation. Any value entered by a user cannot exceed
255 characters in length.
• A value is not required. When entering data for some student,
the user can omit the author.
• There is no index on author.
Activity
• Open the Customers table.
• Add a new record to the table. Be sure to
enter data for every field.
• Find the record with the name Sula Smart,
and replace it with a name of your choice.
• Hide a field, then unhide it.
• Change the alternate row color.
Activity
• Open the Member table and then the Loan table
in design view. Examine the properties of each
field.
• Use Design View to add fields to the Member table as
indicated:
gender: Text field of length 6 to accommodate the values
male and female. Make this a required field that is not
indexed.
birthDate: a Date/Time field; required; not indexed.
Switch back to Datasheet View (You must reply yes to the
system prompt to save your changes). You should notice
there are no values for gender nor birthDate.
Activity
• 2. Now enter values you deem appropriate in the
gender and birthDate fields for each member. Close
the table and reopen it. You will see the values you
entered are still there.
• 3. When new members join the library information
about them must be entered into the Member table.
Each member is given an id value automatically. Add
new members to the library and note how MS Access
will not let you enter id values; instead, MS Access
generates those values for you – id values are
generated sequentially. Close the table and then
reopen the table to confirm your additions worked.
Activity
4. Typically a library assesses a fine the user must pay if they
keep a book out past the due date. As well the library needs to
track the amount, if any, the member has paid. In this exercise
we add two fields to the Loan table so we can keep track of fines
that are assessed and the amount the member has paid.
a) Open the Loan table in design view and add two new fields
named fineAssessed and finePaid. These fields must have a
datatype of Currency.
b) Save the Loan table and then view the rows of the table.
There are no amounts for these fields.
c) Choose some row(s) in the Loan table and enter values for
the fineAssessed and finePaid fields. Note the values you
enter will appear as dollars and cents.
Activity
5. In exercise 3 you added a new member and
in exercise 4 you added fields to the Loan
table. Consider that the person you added
now borrows a book and so a row must be
entered into the Loan table. Enter such a row.
Activity
6. After successfully entering data for exercises 3, 4, and 5 you are
aware of a member and a book for which there are references in the
Loan table.
a) View the Member table and try to delete that member, and
then view the Book table and try to delete that book. These
deletion attempts are unsuccessful because of the
references to the Loan table.
b) Now open the Loan table and find the loan record you
entered in exercise 5. If you delete this row you will find that
you are able to delete the member (provided you did not
enter more loans for this person). These actions mirror the
way in which data would typically be deleted from a
database: if you want to delete a row you must first delete (or
modify appropriately) any rows that reference it.
Table: Data type
Text
If you specify that a field has the Text data type
then Access will permit any characters to be
placed in that field in a row of the table. This is a
common choice when the data will not be used in
calculations. The Text data type provides for
values that have fewer than 256 characters. If you
know that a maximum length less than 255 would
be appropriate then you could use the Field Size
property (discussed in the next section) to limit the
maximum length of a text string.
Memo
A designer selects Memo if the field will have
character data but the length might be longer than
255. Memo allows for a maximum length of 63,999
characters. Consider the description field of the
Course table: could these be longer than 255
characters?
Table: Data type
Number
If a field is used for storing values that are
used in numerical calculations (e.g. quantity
ordered)
then Number is appropriate. The Field Size
property (properties are discussed later) can
be usedto limit the number of storage
locations used per value.

Date/Time
If a field contains date and or time values,
then the Date/Time data type should be
chosen. The
Format property (discussed later) allows you
to control how these values will appear to the
user.
Table: Data type
Currency
If a field will contain monetary values then the
Currency data type should be chosen. This data
type provides for numeric calculations that are
accurate to 15 digits to the left of the decimal and
4 digits to the right of the decimal.

AutoNumber
If you choose AutoNumber MS Access will
generate a value for you when a row is inserted
into the table. You can, via the New Values
property, arrange the numbers to be generated
sequentially or randomly. Often control numbers
for things like orders, invoices, registrations, etc.
are numeric and we can leave it to the system to
generate a next value for us .
Table: Data type
Yes/No
This data type restricts possible values to yes
or no.

Lookup Wizard
Sometimes you need to restrict values to a list
of known values (e.g. a list of genders: Male,
Female), or to values appearing as primary
key values elsewhere in the database.
Consider the creditHours field – a suitable list
could be (1, 2, 3, 6). The Lookup Wizard is a
suitable data type for these situations; when
selected the system steps you through a
series of windows where you can make the
appropriate choices.
Activity
These exercises refer to the Library database.

1. Consider the Member table. The id field was defined with the
AutoNumber datatype. Experiment by adding new members and
you will note that id values increase by 1. Now try deleting the last
two members that you added. If you add those members back in
what id values do they get? Are id values reused?
2. Consider the Book table. Add a field, paperback, that can be used
to indicate whether or not a book is a paperback. Choose the
YES/NO datatype. Save the design and switch to datasheet view.
Now you will see how to enter such values – MS Access provides a
box that is to be checked, or not. You can select (a ‘Yes’) using the
mouse or by using the space bar. You should experiment with this.
Activity
These exercises refer to the Library database.
3. Consider the Member table. Previously you added a gender field.
Open the Member table in design view and change the datatype for
gender to be Lookup Wizard. The wizard will automatically present 3
successive popup windows where you will:

i) specify that you are providing the lookup values;


ii) enter the values (Male and Female);
iii) specify that values are to be limited to your list.

Save the table and enter datasheet view so you can test out the
datatype you have just created. You will notice the user sees a drop
down list containing Male and Female, and so the user cannot
enter/select an inappropriate value.
Table: Properties
Each field must have a data type as discussed above. According to the
data type, MS Access will present to you a set of field properties that
you can tailor for your table.
• Field Size : Data integrity is a serious issue for databases. Setting
Field Size for Text data and Number data is a common thing to do.
Often organizations will limit the data they collect for fields such as
last name and first name (for example, 30 characters). If the data
type is Number then values selected for Field Size are values such
as Byte, Integer, Long Integer, etc. These kinds of values are
associated with increasing number of memory locations used per
value. A selection of Byte restricts storage to 1 byte of memory (8
bits), and since the largest positive integer that can be stored in a
byte is 255, the values stored in the field are forced to be in the
range from 0 to 255.
Table: Properties
• Format: The Format property is used to customize the way text,
number, dates, and times are displayed to the end user. For
instance, selecting Medium Date causes values like January 14,
2023 to be displayed as 14-Jan-23; selecting Long Date results in
the display January-14-23. If you have Text data such as
department code then you could force the display to be in capital
letters by specifying > as the format code. An interesting Format
specification is @;None. If this is used and if there is no value at all
to display the word None will be displayed to the user.
Table: Properties
• Input Mask: The Input Mask property is used to force the user to add
data according to some pattern. This is another nice feature to help
improve the overall quality of data added to a database. When the
cursor is in the Input Mask area a ‘builder button’ appears. When you
click this button you will see a list of popular controls. If you were to
choose the mask for phone number you will see the control !(999) 000-
0000 appear. As a result of this choice, the user must enter a 7-digit
phone number with an optional 3-digit area code).

• Caption: If there is no caption, then the heading used in displays of data


is the field name. Sometimes the field name is not what you want your
users to see. For example, instead of the heading deptCode above a
list of department codes, you may prefer to use the words Department
Code. To accomplish this just enter such a heading in the caption
property for the field.
Table: Properties
• Default value: If some value for a field is very common then you
should consider setting a default value. For example, if most courses
are 3-credit hour courses then the value 3 can be set as the default
for all new courses.

• Validation Rule & Validation Text: If a field has a validation rule


then the rule is tested whenever the user enters data. If the test fails
the user is prompted with a message containing the validation text. A
simple use of this could enforce the credit hours to be less than 10
by entering the rule <10 and the validation text Please enter a value
between 0 and 10. Again, this is a nice feature to improve overall
data quality.
Table: Properties
• Required: Consider the deptName field of the Department table. If a
user enters data for a new department then it is unreasonable for the
deptName field to not have a value. To ensure there will be a value
we make the field required – i.e. we choose Yes for the Required
property.

• Indexed: MS Access automatically creates an index (unique – no


duplicates) on a field that is the primary key. A unique index is a
special internal data structure that Access builds to facilitate two
things: (1) to ensure fast access to rows of data when the user
specifies a value for such a field in a query, and (2) to ensure in the
case of no duplicates that no two rows of the indexed table could
have the same value for that field. You may choose to have an index
on any field. If a field could have duplicate values then you must
choose an index that allows duplicates.
Table: Properties
• Show Date Picker: If the data type is Date/Time, then this selection
enables the user to select a date using a picker – a convenient tool
for data entry.

• New Values: If the data type is AutoNumber you can use New
Values to specify whether the next value for the field will be the next
highest integer, or if it will be a random integer.
Activity
In the next two exercises you are working with your
University database.
1. Consider the Department table. In design view, set the
deptCode field to have a length of 4 and use > as the display
format. Set the length of the deptPhone field to be 10, and
choose the Phone Number input mask. Save the table and
switch to datasheet view. Use the table below as a guide and
enter data into the Department table.
Activity
In the next two exercises you are working with your University
database.
2. Create a Course table with attributes for department code, course number,
title, short description, and credit hours. The credit hours field should be
numeric with no decimal places, and the other fields are Text fields. Set the
deptCode field to be Text with a length of 4 so that it matches the properties of
deptCode in Department. Later it will be important that the deptCode field in
both Department and Course are defined the same. Use the table below as a
guide and enter data into the Course table.
Activity
In the next two exercises you are working with your University
database.
2. Create a Course table with attributes for department code, course number,
title, short description, and credit hours. The credit hours field should be
numeric with no decimal places, and the other fields are Text fields. Set the
deptCode field to be Text with a length of 4 so that it matches the properties of
deptCode in Department. Later it will be important that the deptCode field in
both Department and Course are defined the same. Use the table below as a
guide and enter data into the Course table.
Activity
These exercises refer to the Library database.

3. Consider the firstName and lastName fields in the Member table.


Modify the caption for these fields to be First Name and Last Name
respectively. Save the table and reopen in datasheet view. You will see
these captions at the top of their respective columns.

4. The Loan table has fields that are defined with the DateTime
datatype. Experiment with different formats for these dates.

5. Consider the id field in the Member table. In design view change the
increment property of the id field to be random instead of increment.
This is a nonreversible action (but you can first do a copy of the
database to get a fresh copy). Now add some new members. What can
you say about the id values that are assigned?
Activity
These exercises refer to the Library database.

6. Validation rules and validation text are important features to assist


database users.
a. Consider the Loan table and its date fields. MS Access has many
built in functions one of which is Date() which always returns today’s
date. So, to ensure that someone always enters a due date later than
today, in the properties section
for the date due field enter the following:
Validation rule: >= Date()
Validation text: Enter a future date.
In this situation we are entering a field-level validation rule. These rules
are useful when we can state a requirement independent of other
fields. Test the effect of this validation rule by switching to datasheet
view and entering valid and invalid values for the due date.
Activity
These exercises refer to the Library database.
6. Validation rules and validation text are important features to
assist database users.
b. To ensure the date borrowed value is less than or equal to the
date returned value we construct a validation rule that involves
two fields. MS Access will not let you enter this rule at the field
level; instead, such a rule must be specified at the table level.
To enter a table level rule you must click in a small square just
below the
table name and choose properties. enter the following properties:
Validation rule: [dateBorrowed]<=[dateReturned]
Validation text: Date returned cannot be prior to date
borrowed.
The square braces, [ ], that appear in the expression are required.
These inform MS Access of references to fields in the table. Enter this
rule and verify that it prevents the user from entering improper dates.
Table: Primary Key (PK)
We assume you have created the Department and Course tables. Every
table should have a primary key, but this is just a rule-of-thumb that
most database designers follow. In our University database:
The Department table has deptCode as its primary key.
The Course table has a composite primary key – a key formed using
two attributes: deptCode and courseNo.

• To set a primary key the table must be open in Design View.


You must first select the field (or combination of fields) and
then click the Primary Key icon. This is straightforward for the
Department table, but not for the Course table because its’
primary key comprises two fields. Because the PK involves
more than one field we say this primary key is composite.
Activity
These exercises refer to the University database.

1. Set the primary key for the Department table. With the
Department table in Design View, select the deptCode field and
then click on the Primary Key button. When done successfully you
will see the deptCode field with a key icon beside it. If MS Access
rejects your primary key then you must examine the values you
previously entered for deptCode, there must be some duplicated
value. If this happens you must view the table in Datasheet View
and find the duplicated value and make necessary changes. Once
MS Access has accepted your primary key you should open the
table in datasheet view and experiment: How does MS Access
respond if you try to create a new row with an existing primary
key value?
Activity
These exercises refer to the University database.

2. Set the composite primary key for the Course table. To do


this you first select one field, and then while holding the
Control key down select the other field. With both fields
selected, click the Primary Key button:
a. Select the deptCode (click to the left of the deptCode field).
b. To select the next field to be part of the PK: while holding
the Control key down, click the courseNo field (and now
release the Control key).
c. Next, click the Primary Key icon. You will now see the key
image beside both fields
Forms
• Forms are used for entering, modifying, and viewing records.
You likely have had to fill out forms on many occasions, like
when visiting a doctor's office, applying for a job, or
registering for school. The reason forms are used so often is
that they're an easy way to guide people toward entering data
correctly. When you enter information into a form in Access,
the data goes exactly where the database designer wants it to
go: in one or more related tables.
• For each table you should create a basic form that can be
used to manage data for the table. Once forms have been
created your users will have a more user-friendly way of
entering and managing data in your database (datasheet view
is not considered user-friendly).
Creating Forms through the Form wizard

• The Form Wizard steps you through a sequence


of choices where you choose the table for the
form, the fields to appear on the form, the
layout for the form, the style, and the title to
appear at the top of the form.
Creating Forms through the Form wizard

• As the wizard steps you through each case, you should


select/enter:
– 1. All fields of the table to appear on the form (try clicking
the button when it shows)
– 2. A Columnar layout
– 3. A style of your choice 4. A title of your choice
The last thing you do with the wizard is choose one of:
Open the form and view data, or, to Modify the form’s
design – choose to open the form to view data. Note that
data appears according to information you provided for
data types and properties.
Creating Forms through the Form wizard
• At this point you should create two forms in your University database, one
for the Department table and one for the Course table. A major difference
now is that the user will see just one row at a time. Notice the navigation
buttons at the bottom of the form.
Modifying Forms

• You can make forms easier to use by adding


buttons for common operations of, say,
– 1. adding a new row
– 2. deleting the currently displayed row
– 3. closing the form
Forms: Adding buttons
• To add buttons to an existing form you must open
the form in Design View. At this point make sure
that wizard capabilities are available – if
necessary select the Use Control Wizards: (see
below) to turn wizard capabilities on. To add a
button you click the Button button:
Forms: Adding buttons
• Next, you click a location on the form where you would like
the button to be placed. Click in the area labeled Form Footer
and space will be added to the form’s design to accommodate
the button. Because Use Control Wizards is on, the system
will take you through a series of prompts where you specify
the nature of the button. You need to add three buttons to
each form. Consider selecting/entering the following at the
pertinent prompts:
Forms: Adding buttons
• Anytime after creating a button you can switch to Form View to test
your design. If some button is not working as you like then just
switch back to Design View, delete the button and try again.
Forms: Adding labels
• A label is a control that holds text for display
purposes only. By default, MS Access adds a label
containing the table name in the Form Header area
of the form.
• To add a label you must click the Label control, then
click (and drag for sizing) where you want the label
placed. You can then type the content for the label
and adjust its properties for formatting (e.g. font size,
colour, ...).
Forms: Adding calculation fields
• A label is a control that holds text for display
purposes only. By default, MS Access adds a label
containing the table name in the Form Header area
of the form.
• To add a label you must click the Label control, then
click (and drag for sizing) where you want the label
placed. You can then type the content for the label
and adjust its properties for formatting (e.g. font size,
colour, ...).

You might also like