MCS 023 - Block 3
MCS 023 - Block 3
DEVELOPMENT OF A
HOSPITAL MANAGEMENT SYSTEM (An HMS)
Section Title Page Nos.
No.
1 Introduction 3
2 Need to Develop the Hospital Management System (An HMS)
2.1 Advantages of Hospital Management System
2.2 ER diagram for HMS
2.3 The Software Development Tools required
3 Creating a Database for HMS 5
3.1 Creating a Database using Ms-Access
3.2 Table Relationships
4 Developing Front End Forms 11
5 Reports 18
5.1 Creating a Report using the Wizard
5.2 Creating Mail Merge Labels using a Wizard
5.3 Creating a Report using the Access Report Wizard
5.4 Creating Report using Design View
6 Using Queries and Record set 35
6.1 Using the Simple Query Wizard
6.2 Creating a Query in Design View
6.3 Opening a Query
6.4 Adding Features in Query
6.5 Conditional Queries
6.6 Advanced Query Features
7. Summary 47
1.0 INTRODUCTION
In the two previous blocks, we have provided the details of the concepts of Relational
Database Management. This block provides an example of the use of a Database
Management System and user interface development tool to create a Hospital
Management System (HMS). The focus in this block is not on developing the Hospital
Management System but on providing practical oriented details about how to work
with the tools available in MS Access and Visual Basic to create the basic elements of
any application that are the tables, forms, reports and queries. Thus, in this block, we
will just show the steps about how to create forms, create tables and create reports and
queries. You, in turn, have to develop this system by practicing the steps for creating
various sample forms, reports, tables and queries.
Please note that although this application in the block is expected to be developed
using Microsoft Access and Visual Basic, you may like to use open source tools
MYSQL and PHP.
Each hospital is unique in its requirements and priorities. Hence the HOSPITAL
MANAGEMENT SYSTEM (HMS) covers many hospital activities. It also allows
hospitals to choose from the various modules to match their specific needs. Please
note that this is just a very simple prototype and should not be used for commercial
deployment. Also, you must discuss the specification and design errors of this
application for better description of the domain of Hospital administration.
This block pays attention to various steps involved in using the tools to evolve the
system. Less emphasis is given to analysis and design of the system, which is dealt in
more detail in Block 4.
3
Application Development
Objectives
After going through this block you should be able to:
• brief the very basic needs for an Information System in a Hospital;
• use the tools needed to develop tables, forms, reports and queries;
• work with various tools to create a sample system, and
• identify the problems encountered during the implementation of an Information
System.
The hospital also keeps the record of the doctors visiting the hospital, plus the
permanent employees of the hospital. Each doctor has a few days associated with
his/her visit to the hospital and also the timings when s/he is available in the hospital.
The employees draw their salary from the accounts department.
The hospital maintains the record of the inventory of the hospital including the
equipment and the medicines, blood bank, etc. A limit for inventory is maintained for
every item. When the limit is reached, an order for the purchase of the concerned item
is placed. The database is updated after the supplier supplies the product.
4
2.2 ER Diagram of HMS Development of an HMS
Hospital Department
Work for
Employees
Enrols
Registers
Doctors Examines
Patients
Patient Registration – Provides the details of a patient as well as arrival status such as
OPD or emergency and calculates charges incurred. If a patient is already registered,
updation of previous records is made.
Employee Details – This is used to provide the details of employees working in the
hospital irrespective of the designation and the department.
Department Details – This provides the details of any department in the hospital
including the department heads.
Exercise 2 : Identify the limitations of such a table design. Propose a modified table
design for the above.
5
Application Development 3.1 Creating a database using MS-Access
1. Open MS-Access
2. Dialog Box – Create a new database using
Blank database
Access database wizards, pages and projects
3. Select Create a new database using blank database
4. db1 – create
Now the database db1 has been created. The following screen will appear. You can
now create tables, forms, reports, queries etc. in the database db1.
• Create table in Design view will allow you to create the fields of the table.
This is the most common way of creating a table and is explained in detail
below :
• Create table using wizard will step you through the creation of a table.
• Create table by entering data will give you a blank datasheet with unlabelled
columns that looks much like an Excel worksheet. Enter data into the cells and
click the Save button. You will be prompted to add a primary key field. After
the table is saved, the empty cells of the datasheet are trimmed. The fields are
given generic names. Hence create a table “Docdetails” with fields such as
“name”, “address”, “qualifications” etc. To rename them with more descriptive
titles that reflect the content of the fields, select Format Rename Column from
the menu bar or highlight the column, right-click on it with the mouse, and
select Rename Column from the shortcut menu.
6
Development of an HMS
• Field Name: This is the name of the field and should represent the contents of
the field such as "Name", "Address", "Qualifications", etc. The name cannot
exceed 64 characters in length and may include spaces.
• Data Type is the type of value that will be entered into the fields.
Primary Key
Every record in a table must have a primary key that differentiates it from every other
record in the table. In some cases, it is only necessary to designate an existing field as
the primary key if you are certain that every record in the table will have a different
value for that particular field. An enrolment number is an example of a field that can
be used as primary key to table student.
Designate the primary key field by first selecting them (you may select a composite
primary key also) and then by right clicking on it and selecting Primary Key from the
shortcut menu or select Edit | Primary Key from the menu bar. The primary key field
will be noted with a key image to the left. To remove a primary key, repeat one of
these steps.
Hence in HMS the table Docdetails can be created using the above method. The
following tables should be created by you.
7
Application Development DocDetails
DocCode Text PK
DeptCode Text FK
DocName Text
Qualifications Text
Designation Text
Address Text
Patients’ Registration
Patient Regn No. Text PK
Date of Regn. Text
Name Text
Father’s/husband’s name Text
Age Number
Address Text
DocCode Text FK
Arrived in Text
Charges Number
Employee Details
EmpCode Text PK
Ename Text
Address Text
Gender Text
Date of Joining Date/Time
Salary Number
DeptCode Text FK
DeptDetails
DeptCode Text PK
DeptName Text
HOD Text
HOD Code Text
If none of the existing fields in the table will produce unique values for every record, a
separate field must be added. Access will prompt you to create this type of field at the
beginning of the table the first time you save the table and a primary key field has not
been assigned. The field is named "ID" and the data type is "autonumber". Since this
extra field serves no purpose to you as the user, the autonumber type automatically
updates whenever a record is added so there is no extra work on your part.
8
Development of an HMS
• To link fields in two different tables, click and drag a field from one table to the
corresponding field on the other table and release the mouse button. The Edit
Relationships window will appear. From this window, select different fields if
necessary and select an option from Enforce Referential Integrity if necessary.
These options give Access permission to automatically make changes to
referential tables if key records in one of the tables is deleted. Check the
Enforce Referential Integrity box to ensure that the relationships are valid and
that the data is not accidentally deleted when data is added, edited, or deleted.
Click Create to create the link.
9
Application Development
• A line now connects the two fields in the Relationships window.
Hence the relationship for the above is created using the process above. Now, use the
tables created by you about HMS and develop the following relationships.
DocDetails
DocCode Text PK
DeptCode Text FK
DocName Text
Qualifications Text
Designation Text
Address Text
Patients’ Registration
Patient Regn No. Text PK
Date of Regn. Text
Name Text
Father’s/husband’s name Text
Age Number
Address Text
DocCode Text FK
Arrived in Text
Charges Number
Employee Details
EmpCode Text PK
Ename Text
Address Text
Gender Text
Date of Joining Date/Time
Salary Number
DeptCode Text FK
DeptDetails
DeptCode Text PK
DeptName Text
HOD Text
HOD Code Text
10
Development of an HMS
4.0 DEVELOPING FRONT END FORMS
Forms can be developed using Microsoft Access form wizard and interfaces. But we
have selected Visual Basic here as it is one of the useful tools for form design. You
are however free to develop these forms using any other platform. We are providing a
very brief account of Visual Basic (VB) here, for more details you may refer for help
to VB or further readings.
VISUAL BASIC is a high level programming language evolved from the earlier DOS
version called BASIC. BASIC means Beginners’ All-purpose Symbolic Instruction
Code. It is a fairly easy programming language to learn. The codes look a bit like
English Language. Different software companies produced different versions of
BASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC, IBM BASIC and so
on.
VISUAL BASIC is a VISUAL and events driven Programming Language. These are
the main divergences from the old BASIC. In BASIC, programming is done in a text-
only environment and the program is executed sequentially. In VISUAL BASIC,
programming is done in a graphical environment. Because users may click on a
certain object randomly, so each object has to be programmed independently to be
able to respond to those actions (events). Therefore, a VISUAL BASIC Program is
made up of many subprograms, each has its own program codes, and each can be
executed independently and at the same time each can be linked together in one way
or another.
11
Application Development Now, click on the standard EXE icon to go into the actual VB programming
environment.
It also includes a Toolbox that consists of all the controls essential for developing a
VB Application. Controls are tools such as boxes, buttons, labels and other objects
drawn on a form to get input or display output. They also add visual appeal.
To the left of the screen there are a lot of little icons under the heading ‘General’.
These are called components - ActiveX controls. Basically, these can be added to your
program (technical description, that) - such as a button or picture. Let’s see one in
action.
These are the “properties” of the label. They tell it how to work - what text to display,
what colour it should be, whether it should have a border, and so on. These can be
easily changed by typing in or selecting new values from the list.
12
To move the label − Development of an HMS
13
Application Development Finally, let us add our first line of code: For this double click on the Command
Button.
The screen should look like this...
Private Sub Command1_Click()
<Cursor flashing here>
End Sub
Now type in.
MsgBox “Welcome to my First program!”
The “Private Sub Command1_Click ()” tells Visual Basic to run this line of code
when someone clicks on the Command Button called Command1. When they do, the
line of code runs - telling Visual Basic to display the designated sentence in a message
box (MsgBox). The “End Sub” merely tells VB to stop running the code.
We could then change its properties - perhaps alter its Caption to “Click Me You
Annoying Person!” or its Font property to Arial, Bold, Size 18. Open Visual Basic and
create a new Standard Exe. To do that:
To the left of your screen should be the control toolbox. Right now it’s displaying a
standard set of Visual Basic controls.
This is the Check Box control. Such boxes are checked and unchecked to set
various options. Functionality can be added to the applications. The Check Box
properties can be changed in the properties window. For instance, alter its Caption,
Forecolor or Font property with a few clicks of the mouse! Some of the properties are
hardly ever used or only required in very specific circumstances.
To run an application:
14
Every single control you add to a Form has a Name property − it should appear at the Development of an HMS
very top of the Properties window. Every time a control is added, an automatic name
is assigned – such as Command1. It can be renamed to give it a meaningful name.
The Visual Basic community also has their own “naming conventions” where, for
instance, Text Box names are preceded by “txt”. Therefore if a Text Box is added that
will hold a date, it is advisable to change its name to “txtDate”. Other prefixes
include:
Code Writing
Writing creative code is easy! Firstly, programmers often refer to “design-time” and
“runtime”. Design-time is the period when designing of Form or writing code is
done – basically any time your program isn’t running. And runtime is exactly the
opposite – the time when your program is running.
The properties of controls can be changed whilst you are in design-time. The
properties can also be whilst the program is running. For instance, perhaps you want
the Caption of your Form to change depending on which button the user clicks.
Any control can be added to the Form, including Command Buttons. You can easily
change the properties of such controls by altering values in the properties window.
A variable can be used in VB. This is explained with the help of the following
example.
Example: In order to display a number and increment it, every time the user clicks a
Command Button variables can be used.
Near the top of the code window, there are two drop–down boxes. The first specifies
an object, the second a procedure. In English, these are defined as:
Object the thing you want to work with, say cmdVariableCount.
15
Application Development Procedure denotes the ‘event’ for which the code is written. For instance, we may
write a code in the Click event to have it respond when the user clicks on a
cmdVariableCount. The Procedure drop-down can also display user-defined Subs and
Functions.
You should see a list of objects – probably Form, cmdVariableCount and (General).
The first two are objects − and you can add code to respond to their events. Select the
(General) section from the drop-down list.
You will notice that your cursor is not surrounded by any code as with usual event
procedures, such as “Public Sub cmdVariableCount_Click()”. That is because you
have reached a special place in your code window–the top–where you can create, or
“declare” variables.
To declare a variable you use the syntax:
Dim MyVariable as DataType
...where MyVariable is the name of your variable and DataType is the type of
information it should hold. Here is a short list of the most common DataType values:
String - If you are going to store text in the variable.
Integer - Used when storing a number.
Boolean - Used when storing a True/False value.
Date - Used when story a date value.
Decoding declarations...
A variable is just something in which we can store a certain type of information.
Loops
This Loop can be used to display a message box for every single doctor in HMS. Two
ways can be used – either manually code every single message box or use a loop that
says, in English, “display a message box for every Doc”.
Creating a loop is simple. Let us look at a sample piece of code.
For i = 1 to 5
16
Msgbox “I have looped round “ & i & “ times” Development of an HMS
Next i
The first line tells Visual Basic how many times it should loop around. It is
saying – loop around this bit of code five times and store the number of times it has
looped around in “i”. The second line is just a simple message box that shows
“i” – i.e., just the number of times it has looped around. The third line, “Next I” just
tells it to go back to the beginning until it is gone from one to five.
Do Until
“Do Until” loop is used when you may want to keep asking a user for a student name
until they type in "END" or something.
Dim Ename as String
Do Until Ename = “END”
Ename = InputBox (“Enter the Employee name, or type END to finish:”)
Msgbox “New employee is entered - ” & Ename
Loop
The first line merely declares a variable called “Ename” to hold a string (that is, plain
text). The second line tells Visual Basic to loop around until the “Ename” variable is
equal to “END”.
The third line introduces another function called the InputBox and is used to get
information from the user. On running this, a little box will appear for the user to enter
some information–in this case, an Employee name. Whatever they enter will be
assigned to “Ename”.
And the fourth line just says – loop back to the start again. And at the start, Visual
Basic asks the question once more − does “Ename” equal “END”? If it does, then
looping stops. If not – looping goes on!
Subs
What are subs, and how can they help VB coders simplify their source?
A sub is basically a piece of code separate from the rest. It is often used when you
have a piece of common code that you do not want to rewrite each time you use it.
You can get around these using subs. You can create a singular, generic chunk of code
that displays the message and then ends your program.
The ability to create such subs can be a real time saver! It avoids unnecessary code
and therefore cuts down on the size of your final program. It also saves on boring
maintenance time.
Exercise 3: Create forms for HMS. With this basic information you should try to
work with some of the components of Visual Basic by adding various components and
create the necessary forms. If you are unable to do so then you may either use
MS-Access forms or read through further readings. The forms for HMS that should be
designed by you can include:
1. Switch to the Database Window. This can also be done by pressing F11 on the
keyboard.
2. Click on the Reports button under Objects on the left side of screen.
3. Double click on Create Report Using Wizard.
4. On the next screen select the fields you want to view on your form. All of them
could be selected.
5. Click Next
6. Select if you would like to group your files. Keep repeating this step for as
many groupings as you would like.
7. Click Next
8. Select the layout and the paper orientation desired
9. Click Next
10. Select the style desired.
11. Click Next
12. Give a name to the report, and select Preview the Report.
13. Select Finish
14. The report is displayed. To adjust the design of report, press the design button
and adjust the report accordingly.
Mailing Labels can be created using Microsoft Access for the database. To do this:
1. Switch to the Database Window. This can be done by pressing F11 on the
keyboard.
2. Click on the Reports button under Objects on the left side of screen.
3. Click on New.
18
Select Label Wizard and the table from which the information is taken. Development of an HMS
Click OK
Select the layout of the labels
Click Next
Select the font size and required color on each label
Click Next
Select the layout of the label
Click Next
Select how you want your labels sorted
Name the label report and preview it
1. Choose the Reports Menu: Once a database is opened, the main database
menu as shown below is displayed. Click on the "Reports" selection and a list
of the various reports Microsoft included in the sample database is displayed.
Double-click on a few of these and get a feel for what reports look like and the
various types of information that they contain.
2. Create a new Report: Click on the “New” button and the process of creating a
report from scratch will start.
4. Choose a Table or query: Choose the source of data for the report. If the
information is to be retrieved from a single table, select it from the drop-down
box below. Alternatively, for more complex reports, the report can be based on
the output of a query previously designed. For example, all of the data we need
is contained within the Employees table, so choose this table and click on OK.
5. Select the fields to include: Use the ‘>’ button to move over the desired fields.
Note that the order in which the fields are placed in the right column determines
the default order they will appear in the report. For creating a simple report on
home phones of Hospital Employees, even the information contained in it is
kept simple -- the first and last name of each employee, the title and the home
telephone number. Select the fields and click the Next button.
5. Select the grouping levels: At this stage, you can select one or more
grouping levels to refine the order in which our report data is presented. For
example, we may wish to list home phone numbers of the employees by
department so that all of the members of each department are listed
separately. Click on the Next button to bypass this step and return here later.
7. Choose the sorting options: In order to make reports useful, results can be
sorted by one or more attributes. A list of Doctors or employees can be sorted
by the last name of each employee. Select this attribute from the first drop-
down box and then click the Next button to continue.
8. Choose the formatting options: In the next screen, some formatting options
are given. The default tabular layout can be accepted but can be changed
according to needs. Click the Next button to continue.
9. Select a report style: The next screen is to select a style for the report. Click
on the various options and a preview of the report in that style can be seen in the
left portion of the screen. Select the option and then click the Next button to go
to the next screen as given below.
21
Application Development
10. Add the title: Finally, a title is to be given to the report. Access will
automatically provide a nicely formatted title at the top of the screen, with the
appearance shown in the report style selected during the previous step. Enter
a title for the report, select the “Preview the report” option and then click on
Finish to see the report.
22
Development of an HMS
The final report should appear similar to the one presented above but having the data
you might input.
23
Application Development
Access contains wizards that make creating reports very easy. However, sometimes
wizards do not create the report the way you want it, or you may need to make
modifications. With this in mind, it is helpful to know a little bit about creating a
report from scratch.
1. Click on Reports, New, Design View. Click on the list arrow and choose the
First Period query. Click on OK.
2. The report will open in Design view. If the Toolbox does not appear, click on
the Toolbox icon (wrench and hammer).
24
Development of an HMS
3. At this point, the report has 3 sections: Page Header, Detail, and Page Footer.
Other sections that can be added include Report Header, Report Footer, Grouping
Header and Grouping Footer. The characteristics of each section vary:
Report Header
Appears at the top of the first page of the report. The title of the report usually goes in
the report header.
Page Header
Appears at the top of every page of the report. Column headings usually go in the page
header.
Grouping Header
If grouping has been selected, the grouping header may appear at the top of each new
group. Grouping headers are optional.
Detail Section
This section contains the field values from the table or query that the report is based
on.
Grouping Footer
If grouping has been selected, the grouping footer may appear at the bottom of each
new group. Grouping footers are optional. Subtotals might be placed in the grouping
footer.
Page Footer
Appear at the bottom of every page of the report. Page numbers usually go in the page
footer section.
25
Application Development Report Footer
Appear at the bottom of the last page of the report. A grand total should be placed in
the report footer.
Note that the sections at the far right side of this screen capture of a report. Grouping
footers and page footers were not used.
4. Click on View in the menu bar, then Report Header/Footer to add these sections.
We will not use the page footer, so move the mouse to the top of the report footer bar;
the mouse will change to a double-headed arrow.
Click and drag the report footer bar up until it meets the bottom of the page footer
bar. This closes the page footer bar so that it will not take up unnecessary space. Your
screen should look like this:
26
6. Close the Sorting and Grouping window. You should now have the following Development of an HMS
sections in your report: report header, page header, first period header (this is the
grouping header - it takes its name from the grouping field we selected), detail, page
footer (which we have closed up), and report footer.
7. Now we can begin adding the fields into the report. Click on the Field List icon,
and the list of available fields from our First Period query should appear. Click on the
first field, SSN, and drag it down into the detail section. Repeat for FirstName,
LastName and FirstPeriod. It really doesn’t matter where you put them, but try to line
them up something like this:
8. Close the Field List window. We have all of the fields we need.
9. The items in the detail section are called controls, and in this case, they are bound
controls. This means that they are directly linked to the underlying table or query.
Bound controls consist of two parts, the label box, which in our example is on the left,
and the text box which is on the right. You can change or delete the label box and the
report will still run. However, you cannot change the contents of the text box. If you
do this, you have changed the name of the field that the report is searching for in the
underlying table or query, and the report will not work.
10. Each set of controls can be moved separately or together. Click on the SSN text
box control (the one on the right). A set of handles should appear on that control, and
a single handle should appear on its associated label control. Notice that the first item
on the left of the formatting toolbar says SSN. This is the object list. Click on the list
arrow on the object list, and you’ll see all the other objects and their labels. Click on
some of the other objects and watch the handles move to select those objects. You
can use this object list as a visual clue to determine if you have a text box or a label
selected. Text boxes will show the field name in the object list; labels will show label
x (where x is some number). Hopefully, this will help to prevent you from making
changes to contents of the text box controls.
27
Application Development 11. Click back on the SSN text box control, then move your mouse to the top of the
text box control. Your mouse pointer should change to a hand. If you click and drag.
the hand allows both the text box and the label to move together, keeping their
relationship to each other. Now move the mouse to the larger handle located at the
top left of either the text box or the label. Your mouse should change to a pointing
finger. Clicking and dragging with the finger pointer will move only that control.
All of the labels in our report are at the top of the page in the page header, so we will
need to move them there. You can not click and drag across the boundaries of a
section, so we will cut and paste instead. You can select more than one control at a
time by clicking on the first control, then holding the Shift key down and clicking on
the other controls.
12. Click on the SSN label box, hold Shift down, and click on the other label boxes to
select them. When you finish, there should be handles on all of the label boxes.
Click anywhere in the Page Header section, then click on paste. The labels should
move to the Page Header section. Your screen should look something like this:
28
Click anywhere in the Page Header section to deselect the labels. Development of an HMS
13. By clicking and dragging, begin aligning the labels in the Page Header section
with the text boxes in the Detail section. You can check your progress by changing to
Print Preview and back to Design View. Use the first icon on the left to change
views.
This is a tedious process - sometimes you have to go back and forth many times to get
things aligned just right. Using the Grid dots is helpful, but the Align tool can help
speed up the process.
Select the SSN label in the Page Header, hold Shift down, then select the SSN text
box in the Detail section.
Click on Format, Align, and choose which side to align on. This will align both
controls at whatever side you choose.
14. You must leave enough space between the controls, especially in the detail
section, for the student’s entire SSN, first name and last name to show. This may
mean that you need to increase the size of the controls, which you can do by clicking
and dragging on the handle on the right side of a control. Change to the Print Preview
and scroll through the pages of the report. You can change from page to page by
clicking on the navigation buttons in the bottom left corner of Print Preview.
29
Application Development Notice that neither of the FirstPeriod controls is aligned. These will be moved
into the FirstPeriod Header in the next step.
Select the FirstPeriod label, which is now in the Page Header section. Cut and paste
it into the FirstPeriod Header section. Do the same for the FirstPeriod text box
that is in the Detail section. Position them at the far left of the grid, using
the Align command to adjust them vertically. Chances are they will paste right on top
of each other, so try to use the finger pointer to get them separated. Now the
Report Design looks something like this:
15. If you change to the Print Preview, alignment is probably close, but there is a great
deal of blank space in the report. In the next step, we will close up the sections to get
rid of the white space in the report.
Move your mouse to the top of the Page Footer bar; it should change to a double-
headed arrow. Drag the Page Footer bar up until it almost touches the bottom of the
text boxes in the Detail section. Repeat for at the top of the FirstPeriod Header bar -
drag it up until it almost touches the bottom of the label boxes in the Page Header
section. Your design should look something like as follows.
30
Development of an HMS
When you change to Print Preview, your report should have shrunk down to one
page. However, it is hard to distinguish where you change from one class to another,
so in the next step we will add a line and some formatting to make the report easier to
read.
16. If your toolbox is not visible on your screen, click on the toolbox icon. The
toolbox should appear as a floating toolbar.
The toolbox contains icons for many things, including some unbound controls such as
lines and labels. Unbound controls are in no way connected to the underlying table or
query. You can move them or change them to suit you without worrying.
Click on the line icon on the toolbar. Drag your mouse just under the controls in the
FirstPeriod Header to draw the line. If you hold Shift down while you drag, you will
be assured of a straight line.
Preview the report. The line helps, but it would be better if it were thicker, if the
controls were bold and if there was a space between First and Period. Go back to the
Design View to make these changes.
17. Point to the line and click to select it. If this proves too difficult, remember you
can use the Object list at the left side of the formatting toolbar. Click on the list arrow
and select the line (it should be the only one, so it doesn’t matter what number it is).
Once the line is selected, the handles should appear on the line.
Each control has properties. Click on the Properties icon on the Report Design
toolbar. Since the line was selected, the Line Properties window should appear.
31
Application Development Click on the Border Width box, then click on the list arrow that appears to the right,
and select 2 pt. This will make the line fatter and easier to see. Preview the report.
18. Change back to the Design View. Select both the FirstPeriod label and text box
(remember to hold shift down), then click on the Bold icon. Preview the report to see
the difference.
Change back to the Design View. If both the label and text box are still selected, click
somewhere to deselect. Then select only the label box (be sure about this!) We need
to put a space between First and Period. There are two ways to do this.
Either select the label box, click on Properties, then change the caption to read First
Period, or Click once on the label box, then click again (2 single clicks, not a double-
click). This should put you into the Edit mode for the label. Put a space between First
and Period.
Either method will work. It is important to remember, though, you are doing this on
the label, NOT the text box. If you ever should change a text box beyond repair,
delete the text box and its corresponding label, click on the Field List icon, and drag
the field back into the Report. You will then have to move and edit it back to its
proper location and formatting.
A good clue that you have changed a text box is this: when you preview the Report,
you get a Parameter message. This is usually what happens when Access cannot find
a bound control that is in the report, but not in the underlying table or query.
At this point, the report should look something like this in Print Preview:
If the lack of space between Chem and 101 or Eng and 101 bothers you, you would
need to go back to the Student Schedule table and change all the records, adding
spaces appropriately.
19. The report would look better with a title. We will put the title in the Report
Header, so that it will appear only once, at the beginning of the report.
Click on the Label tool on the Toolbox.
32
Drag and draw a box in the Report Header section. The size of the box really does not Development of an HMS
matter – we can adjust it later.
As soon as you leave your finger off the mouse, you will go right into editing the
contents of the label. Key in First Period Student Roster, then click some place
outside of the box to get out of the editing mode. Preview the report. The title needs
to be bigger and bolder. Go back to the Design View.
Select the label box; handles should appear around it. Select 18 pts and Bold on the
Formatting toolbar. However, as soon as you do this, the text becomes too big for the
box.
Click on Format, Size, To fit. This should adjust the box size, although you may have
to drag the handle at the far right to increase the length of the box.
While the box is selected, you could also change the font face, colour, fill or back
colour and alignment by using other icons on the formatting toolbar.
Move the label so that it aligns at the left side of the report. Check it with Print
Preview.
At this point, your report should look something like this:
20. The report fits all on one page and it is readable. Note how the students in each
class are grouped by the name of the class they are taking. This is the Grouping
Header (FirstPeriod Header) at work.
21. Our example report also had a counter in the Report Footer. This is a calculated
control. Calculated controls display values calculated from one or more fields in the
underlying table or query. To create a calculated control, use the text box icon on the
Toolbox, then key in the formula.
The section that a calculated control is placed in plays an important role. For instance,
if you wanted to find the sum of all invoices for companies in a particular state, where
the field is called Invoice, the formula = sum ([Invoice]) would be placed in a text
33
Application Development box in the Grouping header for State. Think of this as a subtotal. The exact same
formula placed in the Report Footer (the last thing in the Report) would produce a
grand total.
Formulas used for calculated controls are very similar to those used in Excel.
Our example has a calculated control in the Report Footer that counts the number of
students in our roster. To create this:
Click on the text box icon on the Toolbox, then click anywhere in the Report Footer
section. The text box and its accompanying label will appear. The text box has
“Unbound” in it.
Click on Unbound, then key in this formula = count ([LastName]). This will count
the number of last names in the detail section. Note the use of both the parentheses
and the brackets and their position. Also the name of the field must be spelled
identically to the way it is in the underlying table or query.
Select the text box (the one containing the formula), then click on the left align icon
on the Formatting toolbar. Numbers align to the right by default. By changing the
alignment to left, there will not be as big a gap between the label and the number.
Click in the label box, delete the contents, then key in Total Students.
Align the boxes at the left side of the report, then switch to Print Preview. The total
should be 10 and your finished report should look something like this:
34
If you get a message that some of the area of the report is outside the printable area, Development of an HMS
take a look at the width of the grid. It can be adjusted by dragging the right side of the
grid. You can also change the paper to landscape by clicking on File, Page Setup.
Watch out for typos in field names. Again, a big clue is the Parameter query message.
If that happens, go back and start looking at field names in the report.
If you try to open a report and you get a message that tells you the table or query that
the report is based on no longer exists or the name may have changed, stop and think.
Did you change the name of a table or query lately, or delete a table or query? If
that’s the case, the report will no longer function.
A lot of times, users see a typo in a table or query name, so they rename it, not
thinking about the consequences. If the table or query still exists in the database, try
renaming it to the previous name and see if that makes your report work.
23. Take time to further investigate the properties window. Each control has
properties, and there are many properties that can help you customize your report.
Exercise 4
Now you must create the reports for HMS. The sample reports that you may create
can be:
A query does not contain data. Rather, it is a set of instructions. Access uses these
instructions to select and display the appropriate records in a table. The query always
considers all the data in the table. If the new records meet the conditions of the query,
they will be included when the query results appear.
When a query is opened or run, a RecordSet appears. A RecordSet contains all the
fields and records that meet the conditions of the query. While the RecordSet is not a
table, it can be used under certain conditions to add and edit records in tables.
Access provides a Simple Query Wizard that guides through the steps to create a basic
select query. When the Simple Query Wizard is used, the table from which the data is
to be taken is selected as well as the fields to be displayed in the query. In the last
step, the query is named and then whether or not to display the results (the RecordSet)
of the query is chosen. To change the design of the query Design View can be used.
35
Application Development
Create a Query that shows the First Name, Last Name, and Workshop for
workshop participants.
1. Select the New button on the Database window toolbar. Select Simple Query
Wizard.
2. Select OK.
3. Select the Tables/Queries list.
4. Select the table or query you want to use as the basis for the query.
5. Use the single right arrow to add the field(s) you want to display in the query
from the Available Fields list box.
6. Select Next.
7. Type a name for the query.
8. Select Finish.
To create a query in Design view, the design grid is used to set up the query. The field
list of the required table appears in the top pane of Design view. The required fields
can be added to the design grid in the bottom pane of Design view, along with any
sort orders or criteria for selecting records.
36
Development of an HMS
Create a Query in Design view that shows the First Name, Last Name, and
Workshop for workshop participants.
To open a query, Access runs the instructions in the query and displays the resulting
RecordSet in Datasheet view. If new records have added, since the last execution of a
particular query, the new records will appear only if they meet the query criteria.
More than one table can be used in a query. The tables must be joined in order for the
query to give accurate results. If they are not joined, a join is created in the top pane of
Design view.
On adding more than one table, the field lists appear in the top pane of Design view. If
the tables are already related, join lines appear automatically.
Once a table is added to the query, the fields can then be added from the field list to
the design grid. The second row in the design grid is the Table row, which indicates
from which table the field originates.
37
Application Development When Design view is opened to design a new query, the Show Table dialog box opens
automatically so that multiple tables can be added. However, when Design view is
opened to modify an existing query design (i.e., to add a table), the Show Table
dialog box is to be opened manually.
In case of multiple tables query, the tables must be joined in order for the query to
give accurate and meaningful results. If Access does not know how to relate the data
between tables, it displays every combination of data between the two tables. For
example, if one table has 20 records and the other has 5, then the RecordSet will
contain 100 records and the results are virtually meaningless.
If the relationships have already been defined in the Relationships window, join lines
between the field lists display automatically in Design view. Therefore, if a related
table is added to a query, a join line appears automatically. Access also automatically
creates a join if there is a field with the same name in both tables. If there is no
predefined relationship or fields with the same name, such a relationship must be
created.
The join type defined in the relationship is particularly important to queries. The
default type is an inner join in which records are only included in the RecordSet if
there is matching data in the join fields for both tables. An outer join can also be
created in which all the records from the "one" table appear, even if there is no
matching data in the "many" table.
1. Select the join field in the field list of the first table.
2. Drag the field on top of the matching field in the field list of the second table.
3. Double-click the join line.
4. Select the desired join type.
5. Select OK.
38
Running a Query Development of an HMS
A query can be run directly from Design view to display the RecordSet. This option is
useful to test the design of the query to see if the resulting RecordSet contains the
required information.
Running a query does not save the design of the query. If the RecordSet is closed after
running a query, a message box opens, asking to save the changes.
Sorting a Query
When a query is run, the records in the RecordSet appear in the same order in which
they appear in the table. The records can be ordered by either sorting the RecordSet or
assigning a sort order in the query design. The RecordSet can be sorted just as a table.
However, this must be done every time the query is executed. If the sort order is
assigned in the query design, Access performs the sort automatically each time the
query is executed.
1. Select the Sort row under the field by which you want to sort.
2. Select the Sort list.
3. Select Ascending or Descending.
Data can be entered in the Criteria row of the query design grid to restrict the number
of records that are returned in a query.
To select records that meet a single value, enter the value in the Criteria row under
the appropriate field. Access automatically inserts quotation marks (" ") around
alphanumeric entries and number symbols (#) around date entries. If the entry is
numeric, the number appears without quotation marks. When the query is run, only
those records with values that match the criteria appear in the RecordSet.
39
Application Development
For Example, Create a Query that filters all University Employees from the
state of Virginia.
Write a new Query that includes the following fields: First Name, Last Name,
Address, City, StateorProvince, PostalCode.
For example, you may want to create a query to display customers in the Southeast
region. Therefore, you must add the Region field to the query in order to enter the
criteria to select only the Southeast region. However, you might not want the field to
appear in the RecordSet because you know that all the data in the Region field is the
same (Southeast). In this case, you may want to hide the field you used for the
criteria.
The design grid includes a Show row with a check box for each field. If the check box
is selected, the field will appear in the RecordSet. If the check box is deselected, the
field will be hidden in the RecordSet. All the Show field check boxes are selected by
default.
For example, the Orders and Customers tables are related. When you enter the
customer ID number in the appropriate field in the Orders table, Access completes
the customer name and address information automatically through the relationship
with the Customers table.
40
Development of an HMS
You can always edit information in queries based on a single table. In queries based
on related tables, Access must be able to determine the relationship type (i.e., one-to-
one or one-to-many), or you cannot edit information. In queries with tables that have a
one-to-one relationship, you can always edit data. In queries with tables that have a
one-to-many relationship, you may not be able to edit data at times. For example, this
can happen if the join field from the "many" table is not in the query, since Access
cannot create new records in the "many" table. For example, Access cannot create
new orders for a customer if the query is based on the Customers table unless the
Customer ID field from the Orders table is included in the query.
The RecordSet resulting from a query can be printed. It can be printed after running a
query, while it appears on the screen, or by printing it directly from the Database
window. If the RecordSet is printed from the Database window, Access runs the query
and sends the results to the printer, rather than to the screen.
Comparison operators can also be used to select a specific group of records in a table.
For example, if you want to find all customers with credit limits less than 1000, or all
customers with a contract date on or before January 2004, you can write an expression
that defines the criteria using a combination of comparison operators and field values,
such as <1000 or <=1/1/04. Comparison operators are symbols that represent
conditions recognised by Access. The available comparison operators are
Operator Description
< less than
<= less than or equal to
> greater than
>= greater than or equal to
= equal to
<> not equal to
Not reverse logic
41
Application Development A comparison operator can be used to compare a specified value with all the values in
a field. When the query is executed, only the records with values meeting the
specified criteria appear in the RecordSet.
Many times, a query requires more than one condition to obtain the desired result. For
example, if you want to find all customers in PA with sales to date over 10,000, you
would need two conditions: State=PA and Sales to Date>10000. The records must
meet both conditions in order to be included in the RecordSet. To combine two
criteria in this way, you use the And logical operator.
You can use the And operator in a single field or in different fields. In a single field,
you can use the And operator to find records that fall into a range. For example, to
find customers whose contract dates fall between 9/1/04 and 9/30/04, you type both
criteria on a single line in the Criteria row under the appropriate field (i.e., >=9/1/04
And <=9/30/04 in the Contract Date field).
The And operator also allows you to impose conditions in two different fields. For
example, to find customers in PA with sales to date over 10,000, you type each
criterion on a single line in the Criteria row under the appropriate fields (i.e., =PA in
the State/Province field and >10000 in the Sales to Date field).
Create a Query that lists University Employees from the Education Department AND
from the state of Maryland.
Using the University Employees database, create a new Query in Design view using
the University Employees Table. Add the following fields: First Name, Last Name,
DepartmentName, and StateorProvince.
42
1. Select the Criteria row under the DepartmentName field. Development of an HMS
2. Select the Criteria row under the StateorProvince field.
3. Switch to Datasheet view to view the Query results.
Using an OR Condition
Many times, a query requires more than one condition to obtain the desired result. For
example, if you want to find all customers in PA or all customers with sales to date
over $10,000, you would need two conditions: State=PA as well as Sales to
Date>10000. The records only need to meet one of the conditions in order to be
included in the RecordSet. To combine two criteria in this way, you use the Or logical
operator.
You can use the Or operator in a single field or in different fields. In a single field,
you type the criteria on two separate lines under the same field. In different fields, you
type the criteria on two separate lines under the appropriate fields. For example, to
find all customers with contract dates on or before 1/1/04 or credit limits above 3,000,
you type <=1/1/04 in the Criteria row under the Contract Date field and >3000 in the
or row under the Credit Limit field.
Create a Query that lists University Employees from the Psychology OR Music
department
Using the University Employees database, create a new Query in Design view using
the University Employees Table. Add the following fields: First Name, Last Name,
and DepartmentName.
There are several types of functions, the most common of which are listed in the
following table:
Function Description
Sum Sums the values in the calculated field
Average Finds the average value of the calculated field
Count Counts the number of records in the calculated field
Max Finds the highest value in the calculated field
Min Finds the lowest value in the calculated field
Creating a Parameter
A query can be executed with different criteria each time. To do this a parameter
query can be created. A parameter query is a query that prompts the user for
information when the query is run. Access then uses the information as the criteria and
runs the query. The resulting RecordSet only includes those records that meet the
criteria. This option allows one to avoid displaying the query in Design view each
time the criteria is changed.
You enter the text that will display in the prompt in the Criteria row under the
appropriate field in the design grid, followed by a colon (:) and enclosed in square
brackets ([ ]). You can set up a parameter query to prompt the user for more than one
piece of information as well.
The insertion point appears in the Criteria row under the appropriate field.
1. Type the desired text for the prompt, followed by a colon (:) and enclosed in
square brackets ([ ]).
2. Press [Enter].
45
Application Development and space characters appear between the field text strings. This concatenation process
can be performed by creating a query to combine two or more fields.
When typing expressions for concatenation, the first part of the expression defines
the name of the new field. The second part of the expression defines the fields
which are to be concatenated. These field names must be surrounded by brackets.
The ampersand (&) appears between the field name brackets. Any additional
characters that are to appear between the fields are surrounded by double quotes.
For example, the expression Names: [First Name] &" - "&[Last Name] creates a
new field called Names. The new field contains the first name and last name
separated by a space, a hyphen, and a space.
There may be instances when more than one table or query are added to a query. In
order to do this, ensure that the field lists are joined to each other with a join line so
that Access knows how to connect the information.
If tables in a query are not joined to one another, Access does not know which records
are associated with which, so every combination of records between the two tables
would appear in the query. Therefore, if each table had 50 records, the query’s results
would contain 2500 records (50×50), thereby rendering useless results.
Exercise 5
After doing so much about queries, you now create at least five queries relating to
HMS. Some sample queries are listed below:
46
Development of an HMS
7.0 SUMMARY
This block is an attempt to initiate you into the practical world of Database
Management System. We started the block with a brief but not sufficient description
of a Hospital Management System and have identified a few tables. You can
undertake a thorough study on this system such that you are able to design better
tables for this system. We have tried to explain the process of creating various tables,
their relationships, forms, reports and queries. However, you must use this
information to develop the required exercises for hospital management. Thus, the
block provides a flavor of various tools that are essential to build up a database
application. Some of the basic tools which have been introduced in this block are:
• MS-Access Reports
• MS-Access Query Generator
• Visual Basic – a very brief introduction
You must keep working on these tools to obtain expertise over a period of time.
However, it is strongly recommended that you also use certain open source tools like
MySQL and PHP.
47