Prog2 Module Midterm
Prog2 Module Midterm
CHAPTER 1
INTRODUCTION TO VISUAL BASIC
Microsoft.
It is almost similar to Visual Basic 6.0, but has added many new features.
Visual Basic has gone through many phases of development since the days of
Microsoft launched the first graphical BASIC, Visual Basic Version 1 in 1991.
Language; it has caught up with other OOP languages such as C++, Java, C# and
others
Page 1 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Recent/Older pane shows the list of projects that you have created recently.
Open a local folder option to open your code in Visual Studio to develop
The Continue without code option will open the Visual Studio
You might choose this option to join a Live Share session or attach to a
Page 2 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
The Create a new project window opens and shows several project
templates. A template contains the basic files and settings required for a
given project type. Here, you can search, filter, and pick a project template.
The Create a new project window also shows a list of your recently used
select Windows Forms Application. In the Configure your new project window,
you can change the default project name and location, and then select Next.
In the Additional information window, ensure that .NET 8.0 appears in the
Framework dropdown menu, and then select Create. The project will then be
Page 3 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Form-The Form is the first place to build your application. It is the place to
and other components that you can easily browse and access.
Properties Window- This is the place to set the properties of the objects in
your application. The objects include the default form and the controls you
place in the form. We will learn more about setting properties later.
full Object Oriented Programming Language while VB6 may have OOP
called classes. Each class contains data as well as a set of methods, which
manipulate the data. The data components of a class are called instance
allows the structure and methods in one class to be passed down the
hierarchy.
about objects to be created whose exact type is not known until runtime.
Page 4 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Page 5 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
In this example, we will create a simple program that will display a welcome
message when you load the form. First, change the properties of the form as
follows:
PROPERTY VALUE
Name Form1
Backcolor (Select a background color of your choice)
Font Microsoft Sans Serif Size 10 and Bold
Forecolor White (The color of the displayed text)
Maximizebox False (Cannot be maximized)
Text Visual Studio 2022
Next, insert a label into the form and set its properties as follows:
PROPERTY VALUE
AutoSize False
Name Msglbl
BackColor (Select a background color of your choice)
BorderStyle FixedSingle
Font Microsoft Sans Serif Size 10 and Bold
ForeColor Black
Size 239, 32
Text (Remove or Blank It)
Page 6 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
You can also change the properties of the object using code. The syntax to
For Example:
. The formula to assign the RGB color to the form is [Link] (RGB
Page 7 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
CHAPTER 3
WRITING THE CODE
more.
Page 8 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Now you are ready to write the code for the event procedure so that it will do
something more than loading a blank form. The structure of the code takes the
following form:
You have to enter the code between Private Sub and End Sub.
Me is the name given to the Form1 class. We can also call those lines as
Statements. Therefore, the actions of the program will depend on the statements
Page 9 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
CHAPTER 4
MANAGING [Link] DATA
Numeric data types are types of data that consist of numbers, which you can
examination marks, your height and your weight, the number of students in a class,
Page 10 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
using standard arithmetic operators. The non-numeric data comprises text or string
data types, the Date data types, the Boolean data types that store only two values
Literals are values that you assign to a data. In some cases, we need to add a
suffix behind a literal so that [Link] can handle the calculation more accurately.
Page 11 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Lesson 2: Variables
variables are areas allocated by the computer memory to hold data. Like the
mail boxes, each variable must be given a name. To name a variable in Visual Basic,
Variable Names
No spacing is allowed
Examples of valid and invalid variable names are displayed in Table below:
VALID INVALID
My_Name [Link]
ThisDate 1NewDate
Long_Name_Can_beUSE His&Her
DECLARING VARIABLES
In Visual Basic, one needs to declare the variables before using them by assigning
names and data types. If you fail to do so, the program will show an error. They are
normally declared in the general section of the codes' windows using the Dim
statement.
Example:
Page 12 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
You may also combine them in one line, separating each variable with a comma, as
follows:
For string declaration, there are two possible formats, one for the variable-length
string and another for the fixed-length string. For the variable-length string, just use
the same format as example 4.1 above. However, for the fixed-length string, you
Page 13 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
After declaring various variables using the Dim statements, we can assign
Page 14 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Constant
Constants are different from variables in the sense that their values do not
Example:
Page 15 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
CHAPTER 5
MATHEMATICAL OPERATIONS AND STRING MANIPULATIONS
Example:
In this program, you need to insert two Textboxes, four labels and one
button. Click the button and enter the code as shown below. When you run the
program, it will perform the four basic arithmetic operations and display the results
Page 16 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
to process data that come in the form of non-numeric types such as name, address,
Strings can be manipulated using the & sign and the + sign, both perform the
string concatenation which means combining two or more smaller strings into a
larger string. For example, we can join "Visual" and "Basic" into "Visual Basic" using
Page 17 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
This code will produce an error because of data mismatch. However, using &
instead of + will be all right. You can also combine more than two strings to form a
Page 18 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Len Function
The length function returns an integer value that is the length of a phrase
or a sentence, including the empty spaces. The format is: Len (“Phrase”)
Example:
The output:
Page 19 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Right Function
The Right function extracts the right portion of a phrase. The format for
Visual Basic 6 is: Right (“Phrase”, n) where n is the starting position from the
right of the phase where the portion of the phrase is going to be extracted.
For example:
The reason of using the full reference is because many objects have
the Right properties so using Right on its own will make it ambiguous to
[Link]. The program below will return four right most characters of the
Left Function
The Left function extract the left portion of a phrase. The format is:
left of the phase where the portion of the phrase is going to be extracted.
For example:
Page 20 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
CHAPTER 6
[Link] - BASIC CONTROLS
form by using a toolbox control. In fact, in Visual Basic, the form itself is an object.
All the Visual Basic Objects can be moved, resized or customized by setting
Properties can be set at design time by using the Properties window or at run
where
For
example:
Page 21 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
If you are using a control such as one of those provided by the Toolbox, you
can call any of its public methods. The requirements of such a method
If none of the existing methods can perform your desired task, you can add a
method to a class.
For example, the MessageBox control has a method named Show, which is called in
occurred. For example, when a user clicks a control on a form, the form can raise a
Click event and call a procedure that handles the event. There are various types of
events associated with a Form like click, double click, close, load, resize, etc.
You can see this code by double clicking the code which will give you a complete list
Page 22 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Load event. Similar way, you can check stub code for click, double click. If you want
to initialize some variables like properties, etc., then you will keep such code inside
Form1_Load() subroutine. Here, important point to note is the name of the event
handler, which is by default Form1_Load, but you can change this name based on
[Link] provides a huge variety of controls that help you to create rich user
interface. Functionalities of all these controls are defined in the respective control
TOOL DESCRIPTION
Forms The container for all the controls that make up the user
interface.
TextBox It represents a Windows text box control.
Label It represents a standard Windows label.
Button It represents a Windows button control.
ListBox It represents a Windows control to display a list of items.
ComboBox It represents a Windows combo box control.
RadioButton It enables the user to select a single option from a group of
choices when paired with other RadioButton controls.
CheckBox It represents a Windows CheckBox.
PictureBox It represents a Windows picture box control for displaying an
image.
ProgressBar It represents a Windows progress bar control.
ScrollBar It Implements the basic functionality of a scroll bar control.
Page 23 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This program consists of four (4) Common Controls in the Toolbox with
Page 24 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
the btnClear button is clicked. It clears the text from the TextBox
using either the Clear method or the Null String (“”). It then sets
Page 25 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This program is a simple pizza order form that allows the user to select
the variety, size, and crust style of a pizza. The selected options are then
Page 26 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
RadioButtons for crust style, then clears the text of the lblCrust label.
This program uses different visual basic tools that can be used when
dealing with Date and Time. It consists of four separate sub-programs that will
be discussed below.
Page 27 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
dtpBday - This is the DateTimePicker control where the user selects a date.
lblBday - This is the label control where the formatted date will be displayed.
When the user selects a new date in the DateTimePicker, the label will be
Page 28 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
When the selected date range in the calendar changes, it updates two labels
to display the start and end dates of the selected range. The
lblFrom. This label displays the start date of the selected range.
lblTo. This label displays the end date of the selected range.
When the user selects a new date range in the MonthCalendar, the lblFrom
label is updated with the start date of the selection, and the lblTo label is
This code snippet handles the Tick event of a Timer control. It updates a label
to display the current date and time every time the timer ticks. Unlike the
other tools, the Timer is not visible in the Form during the design and the
runtime. This is because [Link] controls like the Timer runs in the
background. As shown in the figure below, the Timer control appears in the
Page 29 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Page 30 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
property. Then updates the text of a label to show the current date and time.
regular intervals.
lblDateTime. This is the label control that will display the current date and
time.
string. By default, Format will use the general date/time pattern (short
Page 31 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Page 32 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This last code snippet handles the event of a button that sets the System Date and
Time
"MM/dd/yyyy".
[Link]. This retrieves the text from the txtTime textbox, which
programmatically requires
Page 33 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Page 34 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This code snippet provides functionality that allows the user to browse for an
clicked. It opens a File Dialog (ofdPicture) to allow the user to select an image
file. After the user selects an image file, the ImageLocation property of the
Page 35 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
figure.
This code snippet handles the Scroll events of Horizontal and Vertical Scroll
Bars to dynamically adjust the Width and Height of the PictureBox. When the
user scrolls the Horizontal Scroll Bar (hsbWidth), the width of the PictureBox
Similarly, when the user scrolls the vertical scroll bar (vsbHeight), the height
scroll bar. If [Link] is 300, the height of picProfile will be set to 300
Page 36 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
pixels. This functionality allows the user to dynamically resize the PictureBox
Page 37 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
CHAPTER 7
FUNCTIONS
function is to accept a certain input and return a value, which is passed on to the
main program to finish the execution. There are two types of functions, the built-in
functions (or internal functions) and the functions created by the programmers. The
The objective of MsgBox is to produce a pop-up message box and prompt the
user to click on a command button before he /she can continues. The format is as
follows:
The first argument, Prompt, displays the message in the message box. The Style
Value determines the type of command buttons appear on the message box, as
shown in Table below. The Title argument will display the title of the message
board.
STYLE
NAME CONSTANT BUTTONS DISPLAYED
VALUE
0 vbOkOnly Ok button
1 vbOkCancel Ok and Cancel buttons
2 vbAbortRetryIgnore Abort, Retry and Ignore buttons
3 vbYesNoCancel Yes, No and Cancel buttons
4 vbYesNo Yes and No buttons
5 vbRetryCancel Retry and Cancel buttons
Page 38 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
We can use named constant in place of integers for the second argument to make
Page 39 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
In fact, VB will automatically shows up a list of names constant where you can select
and
Note: yourMsg is a variable that holds values that are returned by the
MsgBox() function. The type of buttons being clicked by the users determines
the general declaration section. The table below shows the values, the
STYLE
VALUE NAME CONSTANT BUTTON CLICKED
1 vbOk Ok button
2 vbCancel Cancel button
3 vbAbort Abort button
4 vbRetry Retry button
5 vbIgnore Ignore button
6 vbYes Yes button
7 vbNo No button
To make the message box looks more sophisticated, you can add an icon besides the
message. There are four types of icons available in [Link] as shown in Table below.
16 vbCritical
32 vbQuestion
48 vbExclamation
64 vbInformation
Page 40 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
Page 41 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
userMsg is a variant data type but typically it is declared as string, which accepts the
default-text - The default text that appears in the input field where the user
x-position and y-position - the position or the coordinates of the input box.
Example:
Page 42 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
The Mid function is to retrieve a part of text from a given phrase. The
where:
position is the starting position of the phrase from which the retrieving
process begins.
Example:
Page 43 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
The Right function extracts the right portion of a phrase. The format is:
where n is the starting position from the right of the phase where the portion
Example:
The Left function extracts the left portion of a phrase. The format is:
where n is the starting position from the left of the phase where the portion of
Page 44 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
The Trim function trims the empty spaces on both side of the phrase. The format is:
For example:
The Ltrim function trims the empty spaces of the left portion of the phrase. The
format is:
For example:
Page 45 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
The Rtrim function trims the empty spaces of the right portion. The format is:
For example:
The InStr function looks for a phrase that is embedded within the
Where n is the position where the Instr function will begin to look for the
The Ucase function converts all the characters of a string to capital letters. On
the other hand, the Lcase function converts all the characters of a string to
small letters.
For example:
Page 46 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Page 47 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
The Chr function returns the string that corresponds to an ASCII code
corresponding ASCII code. ASCII stands for “American Standard Code for
Information Interchange”. Altogether there are 255 ASCII codes and as many
ASCII characters. Some of the characters may not be displayed as they may
sound.
The Format function is a very powerful formatting function that can display
the numeric values in various forms. A pre-defined format function is built into the
often exist to carry out common tasks, such as: finding an average number or
determining the length of a string. The format of the pre-defined Format function is:
Where n is a number and the list of style arguments is given in the table below.
STYLE
DESCRIPTION EXAMPLE
ARGUMENT
Page 48 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
Page 49 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Other than using the Format function, [Link] has introduced the ToString
method to format output. It is used together with the standard numeric format
specifiers such as “c” which stand for currency. Some of the most common numeric
FORMAT
SPECIFIER DESCRIPTION EXAMPLES
- Displays a currency value. The
default is the US currency $ and Dim myNum as Single
in two decimal places.
- To display other currency, add =2011.123456
a culture code that specifies a
"C" country. For example, for Great [Link]("C")= $2011.12
Britain, you add en-GB using
the keyword [Link]("C4")=
“[Link]
re” $2011.1234
- Displays number of decimal
digits by placing the digit after [Link]("C3",
C, for example, C4 for decimal CultureInfo.
places. CreateSpecificCulture(”en-
GB”))=
£2011.123
Express a Number with in integer
Dim myNumber As Integer =
"D" or "d" form with specified number of
2012.2344
digits. For example, D4 means
[Link]("D4")=2012
four-digit integer.
Express a number in exponential
Dim myNumber As Double =
"E" or "e" form
2012.2344
with specified number of
[Link]("e3")=
decimal places.
2.012e+003
Mutiply a number by 100 and Dim myNumber As Double =
"P" or "p"
displayed with a percentage 0.23456 [Link]("P2")=
symbol %. 23.46%
Dim myNumber As Double=0.23456
"F" or "f" Specifies number of decimal [Link](“F”)=0.23
points. [Link](“F3”)=0.235
The ToString method together with the currency specifier “C” displays the output
with the currency sign $ and in two decimal places. The default currency is the
currency used by your computer system; in this case, it is the US currency. If you
Page 50 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
are not sure of what default currency your computer uses, you can add the keyword
below:
Page 51 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
CHAPTER 8
CONDITIONAL STATEMENTS
help solve practical problems intelligently so that it can provide useful output or
feedback to the user. For example, we can write a program that can ask the
computer to perform certain task until a certain condition is met, or a program that
will reject non-numeric data. In order to control the program flow and to make
decisions, we need to use the conditional operators and the logical operators
The Conditional Operators are powerful tools that can compare values and
then decide what actions to take, whether to execute a program or terminate the
program and more. They are also known as numerical comparison operators.
Normally we use them to compare two values to see whether they are equal or one
value is greater or less than the other value. The comparison will return true or false
OPERATORS MEANING
= Equal To
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less Than or Equal To
<> Not Equal To
OPERATORS MEANING
AND Both conditions must be True
OR One condition must be True
NOT Negates Truth
Page 52 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Lesson 1: If … Then
true. However, when the condition is false, no action will be performed. The
Example:
Page 53 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This code snippet handles the FormClosing event of a form. When the form is about to
close, it
displays a message box asking the user for confirmation. If the user chooses
This functionality is useful for preventing accidental closure of the application, giving
Using just If...Then statement is not very useful in programming and it does
not provides choices for the users. In order to provide a choice, we can use the
If...Then...Else Statement. This control structure will ask the computer to perform a
certain action specified by the VB expression if the condition is true. When the
condition is false, an alternative action will be executed. The general format for
Page 54 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
This code snippet handles the CheckedChanged event for a CheckBox (chkEULA). If
the user checks the CheckBox, the Button (btnSubmit) is enabled, allowing the user
to proceed with submission. Otherwise, if the user unchecks the checkbox, the
If there are more than two alternatives, using just If...Then...Else statement
will not be enough. In order to provide more choices, we can use the If...Then...ElseIf
Statement or the Ladderized If’s. The general format for the if...then... Else
statement is:
Page 55 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Example:
Page 56 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This code snippet handles the Click event of a button. When the button is clicked, it
checks the value entered. The If, ElseIf, and Else statements are used to check the
value of the TextBox. Depending on the value, the Label is set to the corresponding
day of the week. However, if the value is not between 1 and 7, a message box with
Lesson 4: Nested If
statement. This allows the program to evaluate multiple layers of conditions and
execute the corresponding actions based on whether the conditions are true or
Page 58 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
conditions depend on each other. This control structure enables the programmer to
Example:
eligibility for an incentive based on user input. In the outer If Statement, it checks if
the text in the ComboBox. If the condition is true, it proceeds to the nested If
statement. Otherwise, it displays a Message Box stating that the user is not eligible
for the incentive. Inside the outer If block, it checks the next condition. If true, it
displays a Message Box stating the user is eligible for the incentive. If false, a
Page 59 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
The revised code below utilizes Exit Sub to avoid duplicate Message Boxes for not
being eligible. After displaying the Message Box stating that the user is eligible for
the incentive, it then exits the subroutine using the Exit Sub. However, if one of the
conditions are not met, it does not do anything and proceeds to the next part of the
Alternatively, the Nested If can be converted into a simpler If Statement through the
use of the Logical Operators. The code below checks both conditions at the same
time using the AND operator. Using this operator, both the conditions must be met
The Select Case control structure is slightly different from the If...ElseIf
control structure. The difference is that the Select Case control structure basically
only make decision on one expression or dimension while the If...ElseIf statement
control structure may evaluate only one expression. Each If...ElseIf statement may
Page 60 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Page 61 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
Case is preferred when there exist many different conditions because using
If...Then...ElseIf statements might become too messy. The Select Case ...End Select control
structure is:
Example:
Page 62 of 63
Republic of the Philippines
President Ramon Magsaysay State University
COLLEGE OF COMMUNICATION AND INFORMATION TECHNOLOGY
This code snippet uses a Select Case statement to show the equivalent description
of a value entered in the TextBox. It compares the value of the expression against
each Case condition in the list. If the value does not match any of the specified
Here’s another example of using the Select Case Statement with range of values:
Page 63 of 63