Database Lab 01 (MS ACCESS 16)
Database Lab 01 (MS ACCESS 16)
LAB 01
Introduction To database
Objectives:
Tools/ Apparatus:
MS ACCESS 2016
Requirements:
Something required, something wanted or needed
MS ACCESS:
Microsoft Access (or MS Access) is bundled as part of the Microsoft Office suite. It is only
available on the PC version.
Access has traditionally been known as a desktop database system because its functions are
intended to be run from a single computer.
When you save a database in Microsoft Access, it is saved with a .accdb extension.
Older Access databases Extensions
In 2007, Microsoft started to phase these out in favor of the .accdb extension. If you're unable to
open an older database, you may need to convert to .accdb first using an older version of Access.
If you don't currently have Microsoft Access installed on your computer, you can download it
from the Microsoft website.
Access Versions
Creating Table
A table stores information in a row-column format a database similar to the way an Excel worksheet
stores information in a workbook.
Datasheet View displays the table as a grid. The fields are displayed as columns, and the records are
displayed as rows. The field names are listed as the column headers.
Datasheet View displays the data. If this table had data, it would be displayed in the cells.
Design View is used to create a tables, set up data types for each field, specify default values,
specify how data should be entered and displayed, and more.
Most tasks can be achieved using either Design View or Datasheet View, however there are some
advanced settings that can only be changed in Design View. Also, you might find that you prefer
to do certain tasks in Design View and other tasks in Datasheet View.
Design View doesn't display any data. Therefore, there's more space available to display other
settings.
In Design View, the fields are listed vertically. They are listed above and below each other as
opposed to side-by-side.
In Design View, you can see the data type listed next to each field.
Ensuring the Genre field is selected (by clicking on it), look at the bottom Field Properties
frame.
Double-click somewhere in the Required row, so that the value now reads Yes.
Now, double-click in the Allow Zero Length field so that it changes to No.
We have just made the Genre field a required field — all records must have a value in this
field, and it can't just contain a blank string.
5. Set a Primary Key
A primary key is the unique identifier for the table. Each value in this field
will be unique — no two records in the table can have the same value in the
primary key field.
Right-click on the GenreId field and select Primary Key from the drop down list.
This makes the field a primary key field. Once you've done this, you'll see a little key icon to
the left of GenreId.
2. Set the AlbumName field to be a required field. Also set Allow Zero Length to No.
An input mask is a set of simple rules that specifies the format in which data can be entered into a field.
Example: For example, an input mask of (999) 000-0000 could be used to determine how phone
numbers are entered. In this example, the area code is optional, but the rest of the number is must be
entered. This is because, with any input mask, a 9 specifies an optional number, a 0 specifies a
mandatory number.
2. Click on the ReleaseDate field then look to the bottom frame. The bottom frame
displays the properties for this field.
3. n the bottom frame, click somewhere in the Input Mask row. A small button with
three dots will appear. This button launches the Input Mask Wizard.
5. The Input Wizard Mask presents you with some options for how you'd like the
data to be entered.
b. Add two zeros, so that the input mask becomes: 99->L<LL-0000. This will
ensure the user enters the date as DD-MMM-YYYY. For example, 20-
Mar-2016.
c. Click Next.
8. Click Finsh
Number
1. Numeric data. 1, 2, 4, 8, or 16 bytes.
Date/Time
2. Dates and times. 8 bytes.
Currency
3. Monetary data, stored with 4 8 bytes.
decimal places of precision.
AutoNumber
4. Unique value generated by 4 bytes (16 bytes for
Access for each new record. ReplicationID).
Yes/No
5. Boolean (true/false) data; Access 1 byte.
stores the numeric value zero (0)
for false, and -1 for true.
OLE
6. Object Pictures, graphs, or other ActiveX Up to about 2 GB.
objects from another Windows-
based application.
Hyperlink
7. A link address to a document or Up to 8,192 (each part of a
file on the Internet, on an intranet, Hyperlink data type can contain up
on a local area network (LAN), or to 2048 characters).
on your local computer
Attachment
8. You can attach files such as Up to about 2 GB.
pictures, documents,
spreadsheets, or charts; each
Attachment field can contain an
unlimited number of attachments
per record, up to the storage limit
of the size of a database file.
Note, the Attachment data type
isn't available in MDB file
formats.
Calculated
9. You can create an expression that Dependent on the data type of the
uses data from one or more fields. Result Type property. Short Text
You can designate different result data type result can have up to 243
data types from the expression. characters. Long Text, Number,
Note, the Calculated data type Yes/No, and Date/Time should
isn't available in MDB file match their respective data types.
formats.
Field properties
Format
2. Text Customize the way that the field appears by default when
it is displayed or printed.
Memo
Number: Displays the number as it was entered.
Number
Currency: Displays the number by using the thousand
Date/Time separator, and applies the settings in the Regional and
Currency Language Options in Control Panel for negative
amounts, decimal and currency symbols, and decimal
AutoNumber places.
Yes/No For example, 3456.789 is displayed as $3,456.79.
Hyperlink EURO: For example, 3456.789 is displayed as
€3,456.79, and 3 456,789 becomes 3 456,79€.
Fixed: For example, 3456.789 is displayed as
3456.79.
Standard: Displays the number by using the
thousand separator. This format does not display
a currency symbol.For example, 3456.789 is
displayed as 3,456.79.
Percent:Multiplies the value by 100 and displays
the number with a percent sign appended to the
end. For example, 0.3456 is displayed as 35%.
Date/Time:
Select one of the following predefined display formats:
General Date — Displays the value by using a
combination of the Short Date and Long Time
settings.
Long Date — Displays the value by using the
Long Date setting of the Regional and Language
Options in Control Panel.
Medium Date — Displays the value by using the
format dd-mmm-yy (14-Jul-06, for example).
Short Date — Displays the value by using the
Short Date setting of the Regional and Language
Options in Control Panel.
Long Time — Displays the value by using the
Time setting of the Regional and Language
Options in Control Panel.
Medium Time — Displays the value by using
the format HH:MM PM, where HH represents the
hour, MM represents the minute, and PM
represents either AM or PM. The hour value can
range from 1 to 12. The minute value can range
from 0 to 59.
Short Time — Displays the value using the
format HH:MM where HH is the hour and MM is
the minute. The hour can range from 0 to 23.
Yes/No:
True/False — Displays the value as either True
or False.
Yes/No — Displays the value as either Yes or
No.
On/Off — Displays the value as either On or Off.
Decimal
3. Number Specify the number of decimal places to use when
Places displaying numbers.
Currency
New
4. AutoNumber Set whether an AutoNumber field is incremented or
Values assigned a random value when a new record is added.
Input
5. Text Display editing characters to guide data entry.
Mask
Number
Date/Time
Currency
Caption
6. All data types Set the text displayed by default in labels for forms,
reports, and queries.
Default
7. Text Automatically assign the specified value to a field when
Value a new record is added.
Memo
Number
Date/Time
Currency
Yes/No
Hyperlink
Validation
8. Text Supply an expression that must be true to add or change
Rule the value in this field.
Memo
For example, entering a rule such as >100 And <1000 in
Number the Validation Rule property forces users to enter values
Date/Time between 100 and 1,000.
Currency A rule such as [EndDate]>=[StartDate] forces users to
enter an ending date that occurs on or after a starting
Yes/No date.
Hyperlink
Validation
9. Text Enter text that appears when a value entered in this field
Text violates the expression in the Validation Rule box.
Memo
Entering text such as "Enter values between 100 and
Number 1,000" or "Enter an ending date on or after the start date"
Date/Time in the Validation Text property tells users when they
have made a mistake and how to fix the error.
Currency
Yes/No
Hyperlink
Required
10. All Data types Require that this field must contain a value in every
except Auto record.
Number
Allow
11. Text Allow entry (by setting to Yes) of a zero-length string
Zero ("") in a Text or Memo field.
Length Memo
Hyperlink
Text
12. Align All data types Specify the default alignment of text within a control.
except
Attachment
Show
13. Date DateTime Specify whether Access displays a date picker (calendar
Picker control) when users edit a field value.
Steps:
1. Launch the Import Wizard: o launch the wizard for a CSV file, click Text File from the
External Data tab on the Ribbon(both the source file, and the destination table are closed).
a. Use the Browse... button to navigate to and select the import file (in our case,
artists.csv).
b. Select Append a copy of the records to the table and then select the Artists table
from the drop down list of tables.
c. Click OK to continue.
3. Review the Format Options(his part of the Import Wizard will ask you to confirm the file's format). If
all looks OK, click Next >.
4. Select/Review the Delimiter. Access will ask you to select the file's delimiter. If it was wrong, you
can click another delimiter and see how the data updates to reflect the new delimiter.
5. You can also select whether or not the file contains header rows. If the first row of your file
contains headers, click First Row Contains Field Names. Otherwise, leave it unchecked.
6. Before continuing, click the Advanced... button, as we need to tell Access to skip the extra
field.
7. Click Finish to run the import.
Create a Relationship
Access allows you to create relationships between tables so that you can query related data from
multiple tables.
In relational database terms, a relationship is a situation where multiple tables can contain related
data that is linked by a common field.
A relationship consists of a parent table and a child table. The child table references the parent
table by having a field that matches a field in the parent table. The child's field is referred to as a
foreign key. The parent's field is the primary key.
In a relationship, any data entered into the child's foreign key field must match a value from the
parent's primary key field.
Leave the default option (I want the lookup field to get the values from another
table or query.) selected and click Next >.
Access has a Query Wizard that allows you to choose the type of query you want to run,
then walks you through the creation of the query.
Access also allows you to go straight to Query Design view which gives you more control
over creating the query.
Lab Task 3. First import album.cvs file into your album table
Query Design: Steps:
1. Launch the Query Designer.
Click Query Design from the Create tab on the Ribbon.
The Show Table dialog box will appear.
2. Select the Tables for the Query.
Select both the Artists and Albums tables and click Add.
Click Close to close the dialog box.
3. Design the Query.
Create a query that returns all albums from Iron Maiden.
Run the query by clicking the ! Run button on the Ribbon.
If you can't read the screenshot, the query options are:
Albums.AlbumName
Albums.Genre
Albums.ReleaseDate
Artists.ArtistName
Show: All except Artists.ArtistName.
Criteria: Apply "Iron Maiden" to the Artists.ArtistName field.
4. View the Query Results.
The query results are displayed in Datasheet View.
Notice that the results only display the fields for which we selected Show against.
You can view the code that Access generates behind the scenes by switching to SQL View.
To switch to SQL View, click on SQL in the bottom right corner.
Alternatively, you can use the View button on the Ribbon to switch to SQL View. To use this
option, click the little "down arrow" below the word View. This will expand all of the "View" options.
Click SQL View from the list.
For example, we previously created a query that returns all albums from Iron Maiden. This query
could be modified so that it returns all albums from a given artist. The artist can be specified by the
user.
Steps:
1. Make a Copy of the Existing Query
In the left navigation pane, right-click on the Iron Maiden Albums query and select
Copy from the contextual menu.
Now right-click again inside the navigation pane and select Paste.
Now run the query (either by clicking Run or View in the Ribbon, or clicking the
Datasheet View icon in the bottom right corner of the screen).
A prompt will display, asking for an artist. Enter an artist.
Expression Builder
Access 2016 includes an Expression Builder that simplifies the process of building expressions.
You can use the Expression Builder to create queries, set a default value on a field, set a
validation rule against a field, and more.
An expression is any legal combination of symbols that results in a value. The Expression Builder
includes the following to help you build an expression:
Built-in functions. For example, Count(), Date(), and MsgBox().
Constants. Such as True, False, NULL.
Operators. For example >=, &, And
Fields. From tables, queries, forms, and reports.
6. Add a Field.
In the top pane, click «date» inside the function's brackets so that it becomes
highlighted.
Now, expand Music.accdb, then expand Tables, then click on Albums to reveal all
the fields of that table. Double-click ReleaseDate so that it replaces «date» at the
top pane.
7. Add an Operator.
click to the right of the function so that you can continue building the expression.
On the left pane, click Operators, then Comparison in the middle pane to reveal the
comparison operators.
Double-click the greater than sign (>) so that it's added to the top pane.
Query now has the full expression listed in the Criteria field.
11. Click View or Run to run the query.
In Access 2016, you have the option of creating a blank form from scratch, using the Form
Wizard, or having Access automatically create a form from a table or query.
1. Design View
2. Layout View
3. Form View
1. Design View
Design View provides a more detailed view of the form's structure than Layout View.
Design View also allows you to modify the form without having any data getting in the way.
Most tasks can be done in either Design View or Layout View, but there are some form properties
that can only be changed in Design View.
2. Layout View
Layout View provides a more visual layout for editing the form. It bears a close resemblance to
the actual form that the user will see.
Layout View allows you to edit the form properties while it has real data.
3. Form View
Form View is how the user will see the form. You can't edit the form's properties, but you can use
the form just as a user would.
Form Properties
Access provides the Property Sheet for setting properties against a form and its elements. Here's an
overview.
Create a Report
In Access 2016, you can create a report from a table or query with the click of a button. You can
also use the Report Wizard to walk you through the process. And more experienced users can
start with a blank report and build it exactly as they wish.
A report is a printed document that displays information from a database. Reports are created
through the Report Wizard, then modified as necessary in the Design view.
The columnar (vertical) report is the simplest type of report. It lists every field for every record in
a single column (one record per page). The records in this report are displayed in the same
sequence as the records in the table on which the report is based.
The Tabular report displays fields in a row rather than in a column. Each record in the underlying
table is printed on its own row. Only selected fields are displayed, so the tabular report is more
concise than the columnar report.
Create a Macro
A macro is a set of actions that can be run automatically, and on demand. Macros can be used to
automate repetitive tasks, which can save time and effort for the user.
Macros are usually configured to run whenever a particular event occurs. You specify what that
event is when you create the macro.
You can create macros for almost any task, or set of tasks. The great thing about macros is that
they can run many tasks, one after the other, all at the click of a button. They can even be
configured to run automatically as soon as the database is opened.
6. Right-click on the macro's tab and select Save from the contextual menu.Name the macro
AutoExec and click OK.
Be sure to name this macro AutoExec. This is a special name that makes the macro
run whenever the database is opened.
7. Run the Macro.
The only way to test the macro is to run it.
One way to run it is to double-click on it in the left navigation pane.
But to test it properly, we need to make sure that it will run when the database is
opened. Therefore, we'll need to close the database and open it.
8. Click the File tab on the Ribbon. Then click Close from the menu.
9. Click the File tab on the Ribbon again. Then click Open from the menu. Then click the
database to open it. In our case, click Music.
10. The database opens and the form automatically appears.
Click the Command button tool. The mouse pointer changes to a tiny crosshair attached to
a command button when you point anywhere in the form.
Click and drag in the form where you want the button to go, then release the mouse. This
draws a button and simultaneously opens the Command Button Wizard.
Click the Record Operations in the categories list box. Choose Add New Record as the
operation. Click Next.
Click the Text option button in the next screen. Click Next. Type Add Record as the name
of the button, then click the Finish command button. The completed command button
should appear on your form. Save the form.
Create the Additional Command Buttons
Click the Command button tool. Click and drag in the form where you want the second
button to go.
Click Record Navigation in the categories list box. Choose Find Record as the operation.
Click Next command button. Click the Text option button in the next screen. Click Next
command button.
Type Find Record as the name of the button, then click the Finish command button. The
completed command button should appear on your form.
Repeat these steps to add the command buttons to delete record (Record Operations) and
close the form (Form Operations). Save the form.
Reset the Tab Order
Click anywhere in the Detail section. Pull down the View menu. Click Tab Order to
display the Tab Order dialog box.
Click the AutoOrder command button so that the Tab key will move to fields in left-to-
right, top-to-bottom order as you enter data in the form. Click OK to close the Tab Order
dialog box. Save the form.
Lab Task 5. Generate the report given below using Report wizard under create tab
SubForms:
Subform is a form within a form. A main form can have multiple subforms. Main table can have
multiple levels of subforms. Subform is useful for displaying a one-to-many relationship within a
one-to-many relationship.
The easiest way to create a subform is by using the Subform Wizard.
Steps:
1. First select the table and the fields from that table using form wizard under creat tab.
2. Add Columns
from both
tables Artist
and
album.
We want to diplay
albums by artists.
3. Click next
4. Choose how you want to view the records. Select by Artsts.
5. Choose a layout.
6. Save your form and subform as separate objects. And click Finish.
Lab Task 7. Create the given below tables and then create subforms using following
snapshot.
The Switchboard Manager
A switchboard is a user interface that enables a nontechnical person to open the objects in an
Access database by selecting commands from a menu. The switchboard itself is stored as a form
within the database, but it is quite different from the forms you have learned in the previous
sections, it is not used for data entry, but rather as a menu for the user.