0% found this document useful (0 votes)
62 views

Lab02 Excel Manual

This document provides an overview of 6 laboratory exercises for the ISOM2010 Introduction to Information Systems course. The labs cover topics including: 1. Using Excel functions like AND, OR, IF for business analytics 2. Creating and running macros and VBA programs in Excel 3. Building a user interface in Excel VBA using input boxes and message boxes 4. Manipulating and selecting data using looping and conditional statements in VBA 5. Introduction to basic database concepts and Microsoft Access 6. Creating and modifying tables and queries in Access

Uploaded by

Shenan Tam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Lab02 Excel Manual

This document provides an overview of 6 laboratory exercises for the ISOM2010 Introduction to Information Systems course. The labs cover topics including: 1. Using Excel functions like AND, OR, IF for business analytics 2. Creating and running macros and VBA programs in Excel 3. Building a user interface in Excel VBA using input boxes and message boxes 4. Manipulating and selecting data using looping and conditional statements in VBA 5. Introduction to basic database concepts and Microsoft Access 6. Creating and modifying tables and queries in Access

Uploaded by

Shenan Tam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ISOM2010 Introduction to Information Systems

Lab 02: Business Analytics using Microsoft Excel

Learning Objectives
1. Able to use Excel Built-in Functions for Business Analytics.
2. Able to use Conditional Formatting to highlight important information.

Introduction
Microsoft Excel is a desktop application for data organization and comparison. For example, you may use it for
monitoring your personal monthly expenses and checking the balance whether it is out of budget by the end of
month. Your personal data are formatted in terms of rows and columns in an Excel’s worksheet.

In this laboratory, you will learn how to use some advanced built-in functions to analyze presented data.

Part 1: Excel Built-in Functions


Function Description
AND Returns TRUE if all of its arguments are TRUE
OR Returns TRUE if any argument is TRUE
IF Use this function to return one value if a condition is true and another value
if it’s false
COUNTIF Counts the number of cells within a range that meet the given criteria
SUMIF Adds the cells in a range that meet the given criteria
VLOOKUP Use this function when you need to find things in a table or a range by row

1. AND Function
Syntax: AND(logical1, [logical2], …)
Example:

Function Result
=AND(TRUE, TRUE) TRUE
=AND(TRUE, FALSE) FALSE
=AND(A2=A4-A3, A3=2)
=AND(A5-A3=A3, A4=3, A2=A4)

2. OR Function
Syntax: OR(logical1, [logical2], …)
Example:

Function Result
=OR(TRUE, FALSE) TRUE
=OR(FALSE, FALSE) FALSE
=OR(A2=A4-A3, A3=2)
=OR(A5-A3=A1, A4=3, A2=A4)

1
ISOM2010 Introduction to Information Systems

Lab 03: Advanced Business Analytics 1

Learning Outcomes
1. Distinguish the relations between Excel Macros and Excel VBA.
2. Learn to construct VBA programs by Macro recording.
3. Learn to construct or modify VBA programs using VB Editor.
4. Identify the three ways to run VBA programs (or known as Macros).
5. Understand how VBA works with business applications.

Introduction
In this laboratory, you will learn VBA (Visual Basics for Applications) as an advanced way to reduce your
time and workload on repeated tasks. More specifically, you will learn the various ways to construct and
run a VBA program.

Task 1: Excel setting to enable Macros and VBA programs.


1. Select FILE > Options in Ribbon.
2. In Customize Ribbon, check Developer on the right hand side of the list box.

3. Click OK button to confirm.


4. Right-click on the status bar. Check Macro Recording on the popup menu.

1
ISOM2010 Introduction to Information Systems

Lab 04: Advanced Business Analytics II

Learning Outcomes
1. Handle interactions between user and VBA programs (also known as the construction of the user
interface) using input box for user data input and message box for information display to user.
2. Handle interactions between Excel worksheet cell values, input box and message box. And hence
make use of Excel worksheet cell values to run VBA programs.

Background Information
In this lab, we are going to construct a “user interface” in Excel VBA program.

Suppose we are going to input data to an Excel worksheet. Conventionally we select a cell, input the data
and go to the next cell. However, this means we have to make sure the correct cell has been selected
before we input the data. And for general users, sometimes they have no idea of where to input the data.
As a result, an input box from VBA program will be helpful.

Basic skills of using input box and message box


Syntax of input box:

Variable = InputBox("Prompt message", "Title of input box")

Here is a VBA program example that shows an input box:

Sub Input_Box_Demo()

End Sub

Running the VBA program will show the following box for user input.

Title

Prompt message

When the user clicks OK, contents input by the user will be stored in the variable price. But from the
user’s point of view, it seems nothing happened because the value stored will not be displayed. So we
also need an interface that can display values from VBA program to user.

Syntax of message box:

MsgBox "Output Message", message_box type, "Title of message box"

Here is a VBA program example that shows a message box:

Sub Message_Box_Demo()
MsgBox "The unit price has been saved.", vbInformation, "Price Saved"
End Sub

1
ISOM2010 Introduction to Information Systems

Lab 05: Advanced Business Analytics III

Learning Outcomes
1. Manipulate data using iterative (looping) statements.
2. Selection of data using conditional statements.

Iterative (looping) statements


In previous lab, we have made input and output interface of Excel worksheet using VBA. In this lab, we are
going to carry out some data processing.

Consider a list in Excel worksheet as follows.

The marked price is expected to be the sum of cost and profit. Besides copy and paste the sum formula
throughout the column, we may make use of VBA iterative (looping) statements to compute the marked
prices of the products.

Syntax of "Do Until" loop:

Do Until Condition_is_True
Statements_to_be_executed.
Loop

This is the VBA program that can compute the marked prices of the products:

Sub MarkedPrice()
Dim i As Integer
Dim count As Integer
Dim MarkedPrice As Double

i = 2
count = 0
Do Until IsEmpty(Cells(i, "A").Value)
MarkedPrice = Cells(i, "B").Value + Cells(i, "C").Value
Cells(i, "D").Value = MarkedPrice
i = i + 1
count = count + 1
Loop
MsgBox "Totally " & count & " items processed.", vbInformation, "Marked Price processed"
End Sub

1
ISOM2010 Introduction to Information Systems

Lab 06: Database using Microsoft Access

Learning Outcomes
1. Learn about the basic concepts in database.
2. Understand the concept of entity (table), entity instance (record), attribute (field) and identifier
(key field).
3. Experience how to create and modify tables for data storage.
4. Experience how to create and modify queries for data retrieval.

Introduction
Microsoft Access is a desktop application for creating and maintaining databases. A database is a
collection of information that is related to a particular subject or purpose, e.g., a database that maintains
patients’ records for a clinic. The purpose of using a database is to coordinate and organize the
information in a way that a centralized repository is transparent to the end users even the physical data
are spread over different locations.

Imagine that you store your friends’ phone numbers in a paper-based phone book as well as in your
mobile phone’s contact list. If some of your friends change their phone numbers, you would have to
update the information in both places. To avoid the trouble of updating information more than once, it is
better to store data in a central location. All data and any changes made to the data are entered into this
location only. This arrangement not only makes the maintenance of the database much easier, but also
guarantees the integrity of the data (i.e. same data will not have two different versions). To retrieve the
data, users must get access to the database. In practice, a computerized database management system
allows users to retrieve the information that they want and presents it in a nice format.

In this laboratory, you will learn some basic functions of Microsoft Access and a few key concepts in
database management systems.

Task 1: View table’s content in Datasheet View


1. Open the file labAccessDB.accdb which can be downloaded from CANVAS.
2. There is one table named Customer Information. Double click Customer Information to review
the table content.

This view is called Datasheet view in Access. Note that Access’ table looks very similar to Excel
worksheet and data are stored in rows (also known as records) and columns (also known as
fields). This table contains the information about the customers.

1
ISOM2010 Introduction to Information Systems

Lab 07: Database using Microsoft Access II

Learning Outcomes
1. Experience how to create and modify tables for data storage using SQL.
2. Experience how to create and modify queries for data retrieval using SQL.

Introduction
SQL (Structured Query Language) is a domain-specific language used in programming and designed for
managing data held in a relational database management system (RDBMS). It is particularly useful in
handling structured data where there are relations between different entities/variable of the data.

SQL: A language for “interacting” with database


- Add/Update/Delete certain records in database
- Select certain records in database

In this laboratory, you will learn some key SQL commands: INSERT, UPDATE, DELETE and SELECT.

1. INSERT Command
Syntax:
INSERT INTO table (field1, field2, …) VALUES (value1, value2, …)

Example: Add new product record in ProductInfo table


1. Open the file lab07_SQL_DB.accdb which can be downloaded from CANVAS.
2. There is one table named ProductInfo. Double click ProductInfo to review the table content.

3. We would like to add a new product record in the ProductInfo table using SQL.
4. Click Create Query Design, a new Query1 will appear.
5. Close the Show Table window.
6. Click View SQL View
7. Details of the new product:

PRO_ID PRO_Name PRO_Price PRO_Size PRO_Weight


300015 Tiramisu 298 7 inches 1.9 lbs

1
ISOM2010 Introduction to Information Systems

Lab 07: Task 1-5 Solutions

Task 1: Insert new record for new Member


Details of the new Member:

MEM_ID MEM_Last_Name MEM_First_Name MEM_Month MEM_Contact_No MEM_Regions


100041 Chan Olivia 6 23587653 New
Territories

How can we use INSERT command to add new member to MemberInfo table?

Answer:

INSERT INTO MemberInfo VALUES (100041, "Chan", "Olivia", 6, 23587653, "New Territories")

Task 2: Update product price for each product in ProductInfo table


As the cost of raw materials increased a lot, we would like to increase the selling price by $15 for each
product. How can we use UPDATE command to update new selling price in ProductInfo table?

Answer:

UPDATE ProductInfo SET PRO_Price = PRO_Price + 15

Task 3: Find out the distribution of Regions for all Member


You may fill in the blanks below:

SELECT ___________ FROM ___________ GROUP BY __________

Answer:

SELECT MEM_Regions, COUNT(*) As NoOfMember FROM MemberInfo GROUP BY MEM_Regions

Task 4: Retrieve Purchase Record (Member Name, Purchase Date, Product ID, Quantity) of those who
bought product in February
You may fill in the blanks below:

SELECT ________, ___________, _________, ________, ____________


FROM _____________________________________
WHERE ____________________________________ AND DatePart("m", PUR_Date) = 2
Answer:

SELECT MEM_Last_Name, MEM_First_Name, PUR_Date, PUR_PRO_ID, PUR_Quantity


FROM MemberInfo, PurchaseRecord
WHERE MEM_ID = PUR_MEM_ID
AND DatePart("m", PUR_Date) = 2

1
ISOM2010 Introduction to Information Systems

Lab 08: Database using Microsoft Access III

Learning Outcomes
1. Experience how to create and modify Forms.
2. Experience how to create and modify Reports.

Introduction
A form in Access is a database object that you can use to create a user interface for a database
application. A "bound" form is one that is directly connected to a data source such as a table or query,
and can be used to enter, edit, or display data from that data source. Alternatively, you can create an
"unbound" form that does not link directly to a data source, but which still contains command buttons,
labels, or other controls that you need to operate your application.

Reports offer a way to view, format, and summarize the information in your Microsoft Access database.
For example, you can create a simple report of phone numbers for all your contacts, or a summary
report on the total sales across different regions and time periods.

1. FORMS
There are three types of forms that you can create in Access: Form, Multiple Items, Split Form

Form: Display a single record at a time

Multiple Items: Display multiple records

1
ISOM2010 Introduction to Information Systems

Split Form: Display Form view and Datasheet view at the same time

You can use the datasheet portion of the form to quickly locate a record, and then use the form portion
to view or edit the record.

Example: Create a Form for MemberInfo table

Part I: Edit the Layout of the Form


1. Open the file lab08_Forms_Reports.accdb which can be downloaded from CANVAS.
2. There is one table named MemberInfo. Double click MemberInfo to review the table content.

3. We would like to create a user-friendly form for MemberInfo table, so that user can view, edit
and delete the record easily.
4. Click Create Form, MemberInfo form will appear.
5. In order to create a user-friendly form, we would need to change the text into an easy
understandable wording in the form within the Layout View.
MEM_ID MEM_Last_Name MEM_First_Name
Member ID Last Name First Name
MEM_Gender MEM_Month MEM_Contact_No
Gender Birthday Month Contact Number
MEM_Regions MEM_DM
Living Region Agree to receive Direct Marketing?

2
ISOM2010 Introduction to Information Systems

6. Now, we may redesign the Layout of the form by changing the Textbox to Option Group for the
MEM_Gender field.
6.1 Under Design View: Click Design Option Group, drag it next to the MEM_Gender Textbox

6.2 Option Group Wizard popped out, type the value as below:

6.3 Click “No, I don’t want a default.” and Next >


6.4 Click Next >

6.5 Click “Store the value in this field: MEM_Gender” and Next >
6.6 Click Option buttons and Next >
6.7 Leave the caption as defalut and click Finsih
6.8 Delete the MEm_Gender textbox, Caption and put the Option Group in the right place

3
ISOM2010 Introduction to Information Systems

7. We may also change the Textbox to Combo Box for the MEM_Regions field.
7.1 Under Layout View: Click Design Combox Box, place it next to the Living Region label.

7.2 Combo Box Wizard popped out, click “I will type in the values that I want.” and Next >
7.3 Type the values as below:

7.4 Click “Store that value in this field: MEM_Regions” and Next >
7.5 Leave the Combox Box label as default and click Finish
7.6 Delete the MEM_Regions textbox, Combox Box label and put the Combo Box as below
8. Layout of the MemberInfo Form:

4
ISOM2010 Introduction to Information Systems

Part II: Restrict the user input by Validation Rule


You can use the Validation Rule property and the Validation Text property of a form control to validate
data that is input to that control and to help users who input data that is not valid.

Here is the common Validation Rule:

Validation Rule Validation Text


>=0 You must enter a positive number.
<Date() Birth/Purchase date cannot be in the future.
M Or F Enter M for male or F for female.
LIKE "[A-Z]*@[A-Z].com" OR "[A-Z]*@[A-Z].net" Enter a valid .com, .net, or .org e-mail address.
OR "[A-Z]*@[A-Z].org"
Is Not Null Please enter the value.

1. Under Layout View: Click on the Data Tab in the Property Sheet

2. Set the Default Value OR Validation Rule with meaningful Validation Text for the below field:

Field Validation Rule Default Value


MEM_ID Like "1#####"
MEM_Last_Name Is Not Null
MEM_First_Name Is Not Null
MEM_Gender 1
MEM_Month In (1,2,3,4,5,6,7,8,9,10,11,12)
MEM_Contact_No Like "########"
MEM_Regions "Hong Kong Island"

Part III: Change the Theme OR Logo of the Form


1. Click Design Tab, change the logo, themes as you like.

2. New look of the MemberInfo Form:

5
ISOM2010 Introduction to Information Systems

Part IV: Add Button for Delete Record


1. Under Layout View: Click Design Button, place it under the MEM_DM check box.

2. Command Button Wizard popped out, choose the setting as below:

3. Choose “Text: Delete Record” and Next >


4. Name the Button as Btn_Delete and click Finsih

Part V: Add Button for Query


1. Create a query to retrieve the Member’s Favorite Product
2. Copy the below coding under the SQL view

SELECT PRO_Name, SUM(PUR_Quantity) AS Quantity Word Meaning


FROM MemberInfo, PurchaseRecord, ProductInfo [Forms] Form object
WHERE MEM_ID = PUR_MEM_ID AND PRO_ID = PUR_PRO_ID ! ‘s
AND MEM_ID = [Forms]![MemberInfo]![MEM_ID] [MemberInfo] Name of the Form
GROUP BY PRO_Name object
ORDER BY SUM(PUR_Quantity) DESC; [MEM_ID] The place that you
store the MEM_ID
value on the Form
3. Save the Query name as FavProduct
4. Under Layout View of Form: Click Design Button, place it above the Delete Record button.
5. Command Button Wizard popped out, choose the setting as below:

6
ISOM2010 Introduction to Information Systems

6. Choose FavProduct and click Next >


7. Choose “Text: Run Query” and Next >
8. Name the Button as Btn_FavProduct and click Finsih
9. New look of the MemberInfo Form:

2. REPORTS

Example: Create a Report to show Monthly Purchase Record for PurchaseRecord table
1. Click Database Tools Relationships

2. Open PurchaseRecord table Click Create Report


3. Delete the following columns from the report: PUR_ID, PUR_MEM_ID
4. Click Add Existing Fields Show all tables under Field List
5. Double click PRO_Name in ProductInfo table
6. Delete PUR_PRO_ID column
7. Click Group & Sort button
8. Click Add a group Group on PUR_Date, by month, with PUR_Quantity [Type: SUM; Show
subtotal in group header]

7
ISOM2010 Introduction to Information Systems

9. Click Add a group Group on PRO_Name with PUR_Quantity [Type: SUM; Show subtotal in
group header]

10. Sort by PUR_Date from oldest to newest


11. Add New label to show Subtotal and Total Quantity by clicking
12. Drag the SUM of PUR_Quantity to proper place
13. Report Outcome:

Summary
This is not a Design Course. No need to remember all the steps on how to create the Forms and Reports.
You just need to remember the basic concept of Forms and Reports and what we can do with them:

Change the layout of the Form


Restrict the user input by Validation Rule
Change the theme or logo of the Form
Add button for Delete Record
Add button for Run Query
Create Relationships before creating Reports
Group & Sort the field shown in the Reports

8
ISOM2010 Introduction to Information Systems

Task 5: Retrieve which customer has bought product in their Birthday Month
You may fill in the blanks below:

SELECT ________, ___________, _________, ________, ____________


FROM _____________________________________
WHERE ____________________________________
AND ________________________
AND DatePart("m", PUR_Date) = _______

Expected Result:

Answer:

SELECT MEM_Last_Name, MEM_First_Name, MEM_Month, PUR_Date, PRO_Name


FROM MemberInfo, PurchaseRecord, ProductInfo
WHERE MEM_ID = PUR_MEM_ID
AND PRO_ID = PUR_PRO_ID
AND DatePart("m", PUR_Date) = MEM_Month

2
ISOM2010 Introduction to Information Systems

8. Copy the below coding under the SQL View


INSERT INTO ProductInfo VALUES (300015, "Tiramisu", 298, "7 inches", "1.9 lbs")

9. Click Run button and you will see the below warning:

10. Click Yes and Refresh All, you will see the new record shown in the ProductInfo table.

Task 1: Insert new record for new Member


Details of the new Member:

MEM_ID MEM_Last_Name MEM_First_Name MEM_Month MEM_Contact_No MEM_Regions


100041 Chan Olivia 6 23587653 New
Territories

How can we use INSERT command to add new member to MemberInfo table?

2. UPDATE Command
Syntax:
UPDATE table SET newvalue WHERE criteria

Example: Update member record in MemberInfo table


1. Click View SQL View
2. Details of the member need to update:

MEM_ID MEM_Last_Name MEM_First_Name MEM_Month MEM_Contact_No MEM_Regions


100041 Chan Olivia 6 23587653 New
23587638 Territories

3. Copy the below coding under the SQL view


UPDATE MemberInfo SET MEM_Contact_No = 23587638 WHERE MEM_ID = 100041

4. Click Run button and you will see the below warning:

2
ISOM2010 Introduction to Information Systems

5. Click Yes and you will see the updated record shown in the MemberInfo table.

Task 2: Update product price for each product in ProductInfo table


As the cost of raw materials increased a lot, we would like to increase the selling price by $15 for each
product. How can we use UPDATE command to update new selling price in ProductInfo table?

3. Delete Command
Syntax:
DELETE field FROM table WHERE criteria

Example: Delete member record in MemberInfo table


1. Click View SQL View
2. Details of the member need to delete:

MEM_ID MEM_Last_Name MEM_First_Name MEM_Month MEM_Contact_No MEM_Regions


100041 Chan Olivia 6 23587638 New
Territories

* = all fields
3. Copy the below coding under the SQL view
DELETE * FROM MemberInfo WHERE MEM_ID = 100041

4. Click Run button and you will see the below warning:

5. Click Yes and you will see the member record is deleted in the MemberInfo table.

4. SELECT Command
Syntax:
SELECT field FROM table WHERE criteria

Example: Find out which members have a contact number starting with 9
1. Click View SQL View
2. Copy the below coding under the SQL view
SELECT * FROM MemberInfo WHERE MEM_Contact_No Like "9*"
OR
SELECT MEM_Last_Name, MEM_First_Name, MEM_Contact_No FROM MemberInfo WHERE
MEM_Contact_No Like "9*"

3
ISOM2010 Introduction to Information Systems

3. Click Run button and you will see the below result:

Example: Retrieve Purchase Information (Member Names, Product Name, Price and Quantity) of those
who had purchase product that is priced over 200 and purchase amount greater than 1
1. Click View SQL View
2. Copy the below coding under the SQL view
SELECT MEM_Last_Name, MEM_First_Name, PRO_Name, PRO_Price, PUR_Quantity
FROM MemberInfo, ProductInfo, PurchaseRecord
WHERE PRO_ID = PUR_PRO_ID AND PUR_MEM_ID = MEM_ID
AND PRO_Price > 200 AND PUR_Quantity > 1

3. Click Run button and you will see the expected result.

5. SELECT Command (Aggregate)


Syntax: FUNCTION = {COUNT, MAX, MIN, AVG, SUM}
SELECT FUNCTION(field) FROM table WHERE criteria [GROUP BY field]

Example: Find out how many members who born in March


1. Click View SQL View
2. Copy the below coding under the SQL view
SELECT COUNT(*) FROM MemberInfo WHERE MEM_MONTH = 3

3. You may revise the SQL so that it has a meaningful field name for COUNT.
SELECT COUNT(*) AS MarchMember FROM MemberInfo WHERE MEM_MONTH = 3

Example: Find out the distribution of Birthday Month for all Member
1. Click View SQL View
2. Copy the below coding under the SQL view
SELECT MEM_Month, COUNT(*) AS NoOfMember FROM MemberInfo GROUP BY MEM_Month

What will happen if you run the below SQL? ERROR WARNING MESSAGE
SELECT MEM_Month, COUNT(*) AS NoOfMember FROM MemberInfo

4
ISOM2010 Introduction to Information Systems

Example: Find out the most expensive product for each Member has bought
1. Click View SQL View
2. Copy the below coding under the SQL view
SELECT MEM_ID, MAX(PRO_Price) AS MaxPrice
FROM MemberInfo, ProductInfo, PurchaseRecord
WHERE PRO_ID = PUR_PRO_ID AND PUR_MEM_ID = MEM_ID GROUP BY MEM_ID

Task 3: Find out the distribution of Regions for all Member


You may fill in the blanks below:

SELECT ___________ FROM ___________ GROUP BY __________

Task 4: Retrieve Purchase Record (Member Name, Purchase Date, Product ID, Quantity) of those who
bought product in February
You may fill in the blanks below:

SELECT ________, ___________, _________, ________, ____________


FROM _____________________________________
WHERE ____________________________________
AND DatePart("m", PUR_Date) = 2

Task 5: Retrieve which customer has bought product in their Birthday Month
You may fill in the blanks below:

SELECT ________, ___________, _________, ________, ____________


FROM _____________________________________
WHERE ____________________________________
AND ________________________
AND DatePart("m", PUR_Date) = _______

Expected Result:

5
ISOM2010 Introduction to Information Systems

Task 2: View table’s structure in Design View


1. Click View Design View

Field Name: The column names in Datasheet View.


Data Type: A proper data type of each of the field should be set in order to maintain
data integrity, i.e. to make sure the data entered is accurate. The table below lists the
different data types used in Access 2016.
Primary Key: Customer ID is set as the primary key field in this table with an icon ( ). A
primary key denotes a field that can uniquely identify the records in a table.
Field Properties: List the properties of the corresponding field.

Data type Description

Short Text Use for text or combinations of text and numbers. 255 characters maximum

Long Text Memo is used for larger amounts of text. Stores up to 64,000 characters.

Number Allows numbers between -2,147,483,648 and 2,147,483,647

Date/Time Use for dates and times

Currency Use for currency. Holds up to 15 digits of whole dollars, plus 4 decimal places.

AutoNumber Unique value generated by Access for each new record.

Yes/No A logical field can be displayed as Yes/No, True/False, or On/Off.

OLE Object Pictures, graphs, or other ActiveX objects from another Windows-based
application.

Hyperlink Contain links to other files, including web pages.

Lookup Wizard Let you type a list of options, which can then be chosen from a drop-down list.

2
ISOM2010 Introduction to Information Systems

Task 3: Design the table of “Product Information”


1. Click Create Table Design, a new Table1 will appear.

2. Give each field a Name (PRO_ID, PRO_Name, PRO_Price, PRO_Size, PRO_Weight) and specify
their Data Type (as listed in the following table).

Field Name Data Type


PRO_ID *(Primary Key) Number
PRO_Name Short Text
PRO_Price Currency
PRO_Size Short Text
PRO_Weight Short Text

3. Designate the Primary Key Field: Right-click, and select Primary Key on the short-cut menu.
4. Click Save and name this table as Product Information.

Task 4: Data Entry


1. Switch to Datasheet View.

2. In the Datasheet View, enter the following data.

PRO_ID PRO_Name PRO_Price PRO_Size PRO_Weight


300018 Rainbow Cake 198 7 inches 1.3 lbs
300022 Rose’s Fruit Cake 138 4.5 inches 0.7 lbs
300029 Tiramisu 298 7 inches 1.9 lbs
300031 Mango Crispy 208 6 inches 1.4 lbs
3. Enter data as in EXCEL. Use the TAB key to move from field to another field.

3
ISOM2010 Introduction to Information Systems

4. To insert or delete a record, right-click on the record and then select New / Delete Record from
the short-cut menu.

Task 5: Create a Single Table Query


Query 1: Which customers have a contact number starting with 9?

1. On the window toolbar, click the Create Tab. Choose Query Design.

2. Select “Customer Information” table and click Add.

3. Close the Show Table window.


4. To display CUST_Last_Name as the first column, CUST_First_Name as the second column and
his/her contact number as the third column. Double click the field name or simply drag it to the
table.

4
ISOM2010 Introduction to Information Systems

5. Type Like "9*" in the criteria for CUST_Contact_No.

6. To run your query, press the button ( ). (Note: Make sure you have closed all the other tables
when perform the query.)

7. To modify your query, click the button ( ) to go back to the Design View of the query.
8. Save your query as “qryLike9”.

Task 6: Create a Multiple Table Query


Query 2: Retrieve Purchase Information (Customer Names, Product Name, Price and Quantity) of those
who had purchased a product that is priced over 200.

1. On the database window toolbar, click Create, and then choose Query Design.
2. Add Customer Information, Product Information and Purchase Record.

5
ISOM2010 Introduction to Information Systems

3. Link the CUST_ID field of Customer Information to the PUR_CUST_ID field of Purchase Record by
dragging CUST_ID to PUR_CUST_ID.
Link the PRO_ID field of Product Information to the PUR_Item_ID field of Purchase Record by
dragging PRO_ID to PUR_Item_ID.

4. Create a query that shows the following information ― i.e. CUST_Last_Name, CUST_First_Name,
PRO_Name, PRO_Price and PUR_Quantity ―which the item is priced over 200.

6
ISOM2010 Introduction to Information Systems

Furthermore, we may incorporate input box to allow user inputs. For example:

Sub SellingPrice()
Dim i As Integer
Dim count As Integer
Dim Discount As Integer
Dim DisRate As Double
Dim SellPrice As Double

Discount = InputBox("Enter the products' discount (% off)", "Discount")


Cells(1, "E").Value = "Selling Price"
Cells(1, "F").Value = Discount & "% off"
DisRate = 1 - Discount / 100
MsgBox Discount & "% off = " & DisRate & " discount rate.", vbInformation, "Convert"

i = 2
count = 0
Do Until IsEmpty(Cells(i, "A").Value)
SellPrice = Cells(i, "D").Value * DisRate
Cells(i, "E").Value = SellPrice
i = i + 1
count = count + 1
Loop
MsgBox "Totally " & count & " selling prices done.", vbInformation, "Selling Price done"
End Sub

Task A:
In the Excel worksheet "TaskAnB", design an interface to process the data in the table.

1. User should use input box to input the discount (in % off) of the products. The input will be
confirmed by a message box and then display in cell F1 of the table.

2. Calculate the Selling prices of the products. And store the value in appropriate cells.

3. Display a message box reporting the number of items processed at the end.

You may refer to the following variable list.

1. Discount (proper data type: Integer)

2. Selling price (proper data type: Double)

3. Counters i and count (proper data type: Integer)

Name your VBA program as TaskA().


(Hint: You will need 1 input box and 2 message boxes.)

2
ISOM2010 Introduction to Information Systems

Conditional statements
When we want to process the data differently according to some conditions, we can use conditional
statements If ... Then and If ... Then ... Else.

Syntax of "If-then" and "If-then-else" statement:

If Condition_is_True Then
Statements_to_be_executed.
End If

If Condition_is_True Then
Statements_to_be_executed_when_True.
Else
Statements_to_be_executed_when_False.
End If

Here are some simple examples:


Sub VIP_Coupon()
Dim Membership As String
Dim CouponAmt As Integer

CouponAmt = 50
Membership = InputBox("What is your membership?", "Input membership")
If Membership = "VIP" Then
CouponAmt = CouponAmt * 4
End If
MsgBox "You will get $" & CouponAmt & " coupon.", vbInformation, "Your coupon"
End Sub

Sub About_Today()
Dim Weather As String
Dim Feeling As String

Weather = InputBox(" ", "Input the weather")


If Weather = "Sunny" Then
Feeling = "Soooo Great!"
Else
Feeling = "Not Bad."
End If
MsgBox "Today is " & Weather & " day. " & Feeling, vbInformation, "All about today"
End Sub

Sub Your_Age()
Dim Age As Integer
Dim About_You As String

Age = InputBox("Enter your age", "About your age")

If Age < 25 Then


About_You = "Younger than UST!"
ElseIf Age < 65 Then
About_You = "Still a young guy/girl!"
Else
About_You = "Stay young."
End If
MsgBox "You are " & Age & " years young. " & About_You, vbInformation, "Age and you"
End Sub
3
ISOM2010 Introduction to Information Systems

Conditional statement within iterative statement:

Sub Classification()
Dim i As Integer
Dim Category As String
Dim CatCount As Integer
Dim NonCatCount As Integer

i = 2
CatCount = 0
NonCatCount = 0
Category = InputBox("Enter the counting Category", "Find your Category")
Do Until IsEmpty(Cells(i, "B").Value)
If Category = Cells(i, "A").Value Then
CatCount = CatCount + 1
Else
NonCatCount = NonCatCount + 1
End If
i = i + 1
Loop
MsgBox "There are " & CatCount & " items for " & Category & ". And " & NonCatCount & " items
for others.", vbInformation, "Final report"
End Sub

Task B:
In the Excel worksheet "TaskAnB", finish the followings making use of both iterative and conditional
statements.

1. User input the Category first by input box. Copy the input category to an appropriate cell. And then
the subroutine will go through the entries (records) in the product list table one by one.
(Hint: Using Do-Until Loop.)

2. If a record that matches the input category is found, copy the data (Product Name, Unit Price and
Pricing based on) to appropriate cells next to the list. Also increment the item counter by 1.

3. If a record does not match the input category, increment another item counter by 1.

4. Finally, display a message box (or two separate message boxes) reporting the number of items
belong to and not belong to the input category respectively.

You may refer to the following variable list.

1. Category (proper data type: String)

2. Counters i, j, CatCount and NonCatCount (proper data type: Integer)

Name your VBA program as TaskB().


(Hint: You will need 1 input box and 1 message box.)

4
ISOM2010 Introduction to Information Systems

Running the VBA program will show the following message box.

Title

MsgBox Type Output message

Display the user’s input message in the message box (make use of concatenation):

The following VBA program output shows how to include user’s input in the message box display.

This can be done by concatenating text strings of “fixed contents” with user’s input variable using “&”
operator.

Here is the VBA program in detail:

Sub Display_user_input()

MsgBox "The unit price $" & price & " has been saved.", vbInformation, "Price Saved"
End Sub

When the user input “3” and click OK in the input box. The user input will be displayed as part of the
message in the message box.

Declare variables

In the previous example, it seems VBA automatically recognize the variable “price”. However, we
should always declare a variable with meaningful name and proper data type before using.

Good examples of declare variables:

Dim Product_Name As String


Dim SubTotal As Double
Dim Quantity As Integer

Bad examples of declare variables:

Dim Product_Name As Variant


Dim SubTotal As String
Dim Quantity As Double
2
ISOM2010 Introduction to Information Systems

In light of this, the previous example should be modified as follows:

Sub Display_user_input_Modified()
Dim price As Double
price = InputBox("Enter the
MsgBox "The unit price $" & price & " has been saved.", vbInformation, "Price Saved"
End Sub

Variables for calculation results

Besides storing user inputs, variables are also very useful to store calculation results. For example:

Sub FX_USD_to_HKD()
Dim USD_Amt As Double
Dim HKD_Amt As Double
USD_Amt = InputBox("Enter the amount of USD", "USD to HKD")
HKD_Amt = USD_Amt * 7.8
MsgBox "USD$" & USD_Amt & " = HKD$" & HKD_Amt, vbInformation, "Currency Exchange"
End Sub

Task 1:
Making use of input boxes, design an interface for user to input the followings:

1. Product Name (proper data type: String)

2. Unit Price (proper data type: Double)

3. Quantity Sold (proper data type: Integer)

Then calculate the revenue generated by the relation “revenue = Unit Price * Quantity Sold”. Making use
of message box, display a mess age including all the user inputs. The VBA program output looks like t his:

Name your VBA program as Task_01().


(Hint: You will need 3 input boxes, 4 variables and one message box in the VBA subroutine.)

3
ISOM2010 Introduction to Information Systems

Interactions between input / output values and Excel worksheet cells in VBA
In Excel VBA we can access a certain Excel worksheet cell with the “Cells” keyword.

The following VBA program shows how to assign a user input value to a certain cell.

Sub Store_user_input_Cell()
Dim price As Double

Cells(1, "A").value = price


End Sub

On the other hand, the following VBA program shows how to display a certain cell’s value using message
box.

Sub Display_Cell_Value()
Dim price As Double
Dim output As Double

Cells(1, "A").value = price


output = Cells(1, "A").value
MsgBox "The unit price in cell A1 is $" & output, vbInformation, "Price in A1"
End Sub

Task 2:
Make a copy of VBA program named Task_01() and renamed it as Task_02(). With reference to
the contents in worksheet named Task02. Modify VBA program Task_02() as follows:

1. After the user input the product name, unit price and quantity sold, contents will be stored in
appropriate cells of the table.
2. The calculated revenue will also be stored in appropriate cell.
3. The number of items input will be updated in cell B1.

(Hint: Besides what you need in task 1, you will also need to make use of the value in cell B1.)

4
ISOM2010 Introduction to Information Systems

Task 2: Construct a VBA program by Macro Recording; Run a VBA program by Run Macros
By using Excel Macro Recording, user interactions will be recorded as VBA program code. And the VBA
program (or known as Macro) can be replayed later. So it is very useful if the interactions have to be
repeated many times. Now you are going to record a Macro that can plot a stock chart.

1. Open the file lab03_stock_slot.xlsm that can be downloaded from CANVAS.

There are two worksheets, namely 3988.HK and 0005.HK, we will record the steps on 3988.HK
worksheet and later we will replay the steps for 0005.HK.

2. Select 3988.HK worksheet.


3. Click Record Macro button ( ) in the status bar to begin the recording process.
4. Type My_First_Macro as the Macro name in the popup dialog box. And then click OK button to
confirm.
5. Change the names in Row 1 as below:
i) Open → opening price
ii) High → high price
iii) Low → low price
iv) Close → closing price
6. Select Columns A to E.
7. Click INSERT > Insert Line Chart
8. Click Stop Recording button ( ) in the status bar to finish the recording process.

Now, we are going to replay the steps.

9. Delete the chart.


10. Select DEVELOPER > Macros.

11. Choose My_First_Macro and click Run button to replay the Macro.

2
ISOM2010 Introduction to Information Systems

Task 3: Construct a VBA program by VB Editor; Run a VBA program by clicking a button
In this part, you are going to develop a VBA program that calculates the daily stock closing price change.

1. On the same stock data worksheet, select DEVELOPER > Visual Basic in the Ribbon.

A new window pops up, and this window is called Visual Basic Editor (VBE). In VBE, you can
create your own programming code for a button, list box, menu, etc.

2. In VBE, select Insert > Module in the menu.


3. Now you can develop a VBA program in the Module window. As a beginner, copy the following
VBA program code in the VBE Module window:

Public Sub Calculate_Change()


Dim i As Long

Cells(1, "H").Value = "Change"

i = 2
Do Until IsEmpty(Cells(i + 1, "G"))
Cells(i, "H").Value = Cells(i, "G").Value - Cells(i + 1, "G").Value
i = i + 1
Loop
End Sub
4. Go back to the stock data worksheet window.
5. Insert a Button to call the VBA program in Step 3.
i) Select DEVELOPER in Ribbon.
ii) Select Insert > Button.

iii) Use the mouse to drag a rectangular shape in order to create a button.
iv) Choose Calculate_Change and click OK button.
6. Click the button to run the VBA program. You will have the similar result below.

3
ISOM2010 Introduction to Information Systems

Task 4: A Simple Game – Slot Machine


In this part, you are going to develop a simple slot machine game in VBA programming code. In this
game, the user wins when s/he gets three identical pictures.

Sample outlook:

Instruction
1. Select the Slot Machine worksheet.
2. Copy the VBA program code (listed in the next page) to a Visual Basics Editor Module window.
3. Insert a button and assign the PlaySlotMachine VBA program to the button.

4
ISOM2010 Introduction to Information Systems

VBA Code
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
' Between each draw, stop (sleep) for some time to let people see the flashing
' effects.

Public Sub PlaySlotMachine()


'Define the variable here
Dim num1 As Integer, num2 As Integer, num3 As Integer
Dim count As Integer
Randomize Timer

'draw the slot machine for 50 times


For count = 1 To 50

' Randomly generate a number from 0-1 to num1 then *3 +1


num1 = Int(Rnd() * 3 + 1)

' If num1=1 then return happy face in cell B2, elseif num1=2
' then return normal face in cell B2. '
' All other value will return unhappy face in cell B2

If num1 = 1 Then
Cells(2, "B").Value = "J"
ElseIf num1 = 2 Then
Cells(2, "B").Value = "K"
Else
Cells(2, "B").Value = "L"
End If

num2 = Int(Rnd() * 3 + 1)

If num2 = 1 Then
Cells(2, "C").Value = "J"
ElseIf num2 = 2 Then
Cells(2, "C").Value = "K"
Else
Cells(2, "C").Value = "L"
End If

num3 = Int(Rnd() * 3 + 1)

If num3 = 1 Then
Cells(2, "D").Value = "J"
ElseIf num3 = 2 Then
Cells(2, "D").Value = "K"
Else
Cells(2, "D").Value = "L"
End If

'The closer to the end of the draw, the slower are the flashing effects
Sleep count * 4
Next count

'If num1=num2=num3, then will display message box


If (num1 = num2 And num1 = num3) Then
MsgBox "Congratulation! You are so lucky today"
End If
End Sub

If you are using Mac version of Excel, please make the below two statements as comment:

'Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

'Sleep count * 4

These two lines are used to perform slow animation in Windows version Excel.

5
ISOM2010 Introduction to Information Systems

Challenge Questions [Optional]


Q1. Try to modify the VBA program of the slot machine so that:

1. The display message will be different for 3 happy faces, 3 normal faces and 3 sad faces.
2. The program will display another message when all three pictures are different.

Q2. Do we have to modify the VBA program if we use 3 other different pictures?

Summary

Two ways to construct a VBA program (or known as Macro)


1. Macro recording, the steps involved will be recorded as a VBA program.
2. Write the codes within Visual Basics Editor.

Three ways to run a VBA program (or known as Macro)


1. Select the intended VBA program from the Macro Menu and click RUN. (As in Task 2.)
2. Assign the intended VBA program to a button first, then click that button. (As in Tasks 3 and 4.)
3. In Visual Basics Editor, place the cursor within the intended VBA program code. Then click the
Editor’s play button.

6
ISOM2010 Introduction to Information Systems

3. IF Function
Syntax: IF(logical_test, [value_if_true], [value_if_false])
Example:

Function Result
=IF(B2<>"", "No Error", "Missing Category") No Error
=IF(C2>0, "No Error", "Invalid Price") No Error

Q1. How to change the IF Function so that it gives out the same result as “=IF(C2>0, "No Error", "Invalid Price")”?
Hint: =IF(_________, "Invalid Price", __________)
Ans:

Q2. How to combine the below two IF Functions into one Function?
Function Result
=IF(B5<>"", "No Error", "Missing Category") Missing Category
=IF(C5>0, "No Error", "Invalid Price") Invalid Price
Hint: Using 1 AND Function and 2 IF Function
Expected Result:

Ans:

4. COUNTIF Function 5. SUMIF Function


Syntax: COUNTIF(range, criteria) Syntax: SUMIF(range, criteria, [sum_range])
Example:

Function Result
=COUNTIF(B2:B7, "Stationery") 4
=COUNTIF(B2:B7, B4)
=COUNTIF(C2:C7, ">=150")

=SUMIF(A2:A7, "Desk", C2:C7) 550


=SUMIF(B2:B7, B2, C2:C7)

2
ISOM2010 Introduction to Information Systems

Default value of range_lookup is TRUE


6. VLOOKUP Function
Syntax: VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])
Example:

Function Result
=VLOOKUP(600, A2:D4, 2, TRUE) Silver
=VLOOKUP(600, A2:D4, 2, FALSE) #N/A
=VLOOKUP("Gold", B2:D4, 2, FALSE)

Q1. How to write the VLOOKUP Function to find out the corresponding Member Type of Kitty?
Ans:

Q2. If the member is new member, he/she can enjoy the New Member Discount. How to write the VLOOKUP
Function to find out the Discount Rate of Tony? (Hint: Using IF Function)
Ans:

Part 2: Conditional Formatting


Conditional Formatting quickly highlights important information in a spreadsheet. You may use the built-in
formatting rules or add your own formula to a conditional formatting rule.

Built-in formatting rules:

3
ISOM2010 Introduction to Information Systems

Your own formula:

Highlight Cells Rules example:

Data Bars example:

Challenge Questions [Optional]


Q1. How to write the IF Function so that it gives out the below expected result?
Expected Result:

Ans:

4
ISOM2010 Introduction to Information Systems

Q2. How to write the IF Function so that it gives out the below expected result?
Hint: Using Array Formula, Press Ctrl+Shift+Enter to enter the formula
Expected Result:

Ans:

You might also like