0% found this document useful (0 votes)
4 views32 pages

Visual Basic Module

The document provides an overview of programming fundamentals, including algorithms, flowcharts, pseudocode, and data handling. It explains the importance of logical thinking in programming and outlines various programming languages, data types, and operators. Additionally, it discusses conditional and loop statements, along with the Visual Basic development environment and commonly used terms.

Uploaded by

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

Visual Basic Module

The document provides an overview of programming fundamentals, including algorithms, flowcharts, pseudocode, and data handling. It explains the importance of logical thinking in programming and outlines various programming languages, data types, and operators. Additionally, it discusses conditional and loop statements, along with the Visual Basic development environment and commonly used terms.

Uploaded by

system servicing
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

V/B Module No. 1 III.

FLOWCHART – the graphical representation of an


algorithm. Flowchart is the pictorial description of
FUNDAMENTALS OF PROGRAMMING sequenced activities and logic to be performed by the
computer for carrying out data processing. It uses special
Programming – This is the process of giving instructions. symbols, which represent a specific function.
Program – This is the instructions provided. Flowcharts are used by programmers before the
actual start of any programming activity.
Logic formulation and problem solving are the
fundamentals of programming. It requires an intricate SYMBOLS USED IN FLOWCHART
process of critical and analytical thinking, combined with SYMBOL NAME FUNCTION
the precision and keenness to details. The planning stage Represents the
is very important because it serves as the backbone of Terminal beginning and
any program. the end of the
program. It
I. ALGORITHM – a logical procedure or set of steps to be contains the
followed to arrive at a solution to a problem. words “Begin” or
An algorithm is the breaking down of a problem “Starts” and
into simple steps in order to find the right answer. “End” or “Stop”.
Represents a
Examples: Process or Action step or
a. Recipe in a cookbook – a complicated dish is instruction such
broken down into simple cooking instructions. as arithmetic
b. Choreography of a dance – a difficult dance is operations and
broken down into a succession of basic steps. comparisons.
Ex. Gathering all
Steps in solving a problem: materials in a
box; Add 5 Add 7
1. Study the problem Represents the
2. Gather pertinent information Input or Output steps wherein
3. Process the information information such
4. Arrive at the solution as letters and
numbers are
II. PSEUDOCODE – an algorithm in English statements in entered by the
an outline form. This is used to conceptualize the flow of user or produced
the program before coding it in a programming language. by the process.
Ex. Input 3
Example: Numbers; Get
Pseudocode for Making a Soap A,B,C; Print A
1. Prepare your work space Indicates the
2. Cut and weigh the soap base Arrow or Flow flow of direction
3. Melt the soap base Direction or the next
4. Add the fragrance or essential oil Indicator activity to be
5. Add color to the melted soap done.
6. Stir the melted soap
7. Pour the soap into the mold
8. Unmold the soap

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 1
Denotes a
Decision decision to be
made. It involves
two course of
action that is
answerable by Example:
either “Yes/True”
or “No/False” Flowchart to Access the Internet
wherein one has
to be chosen.
The symbols BEGIN
represents the
“If….Then” and
“For…. Next”
statements.
Type Internet username
Represents the and password
Preparation process that will
change the
direction of
initialization of
any value or
execution of a Connect
variable. username Access the
and internet
Indicates a password
Predefine submodule or
Process or subflowchart
Subroutine embedded
within a larger
process. It can be
described in END
detail in a
separate
flowchart.
Indicates that
On-page the process is
Connectors continued where
the matching on-
page connector
is placed (either
on the same
page or at a
different page)
Ex.
A

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 2
are more efficient compared to those written in high-
level language.

Advantages of Flowchart

1. The flow of the program is easily understood.


2. It determines the validity of the process involved
in the program.
3. Debugging or connecting of errors becomes less
complicated.
4. It produces effective program documentation.

Guidelines for Preparing a Flowchart

1. Determine the following:


a. Data to be entered
b. Information to be produced
c. The way data will be entered
d. Point when data will be given
e. The point when information will be produced.
2. There should only be one Start/Begin and
Stop/End processes.
3. Concentrate on the logic of the program
4. Choose only the important steps. No need to
represent all the steps in the flowchart.

Two (2) Classifications of Programming Languages:

High-level Programming – language resembles human


language. This corresponds to several machine language
instructions. A compiler is needed to translate this
language understood by computers. Using such, a
programmer can easily write programs and save time
since he will only concentrate on the problem that the
program is designed to solve or rather than on how the
computer will carry out the program.

Low-level Programming – language or machine


language describes the process exactly to be carried out
by the computer’s central processing unit. Machine
languages consist entirely of numbers. It tells the
computer what to do in detail. It translates each
statement into a single machine instruction. This kind of
language is difficult and tedious to write. Programs
written in low-level language are compact, run faster and

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 3
many data. A variable allocates a portion of the memory
depending on the kind of data.

A program should consider the following in allocating


memory:
1. How much space will the data consume?
V/B Module No. 2 2. Where in the memory was each variable stored
For Example, the memory allocation for variable A that
DATA HANDLING IN PROGRAMMING holds a character data type is 1 byte.

I. DATA TYPES – A program handles different data to be III. CONSTANT – are values that do not change during
processed by the computer. These data are classified the execution of the program.
into different types. Ex.
Pi = 3.1416
1. Numeric Data – These are numbers, whole or real X = 100
a. Integer – whole number; 0 – 9
Ex. 20, 105, 289067 IV. OPERATORS – are symbols that indicates the
operation to be performed on the data.
b. Float or Double – Number with decimal
Ex. 5.19, .069, 99.99, 150.9678 Classification of Operators
1. Arithmetic Operators – operators that represent
2. Aphanumeric Data – These are numbers, alphabets or mathematical operations.
special characters.
a. Number – numbers that cannot be used for FUNCTION OPERATOR EXAMPLE RESULT
mathematical operations. Addition + 8+1 9
Ex. 1, 36, 500 Subtraction - 8-1 7
Multiplication * 8*8 64
b. Character – a single letter or special character Division / 8/4 2
Ex. A, L, c, #, $, @3. Exponentiation ^ 8^2 64
Modulus* Mod 8 mod 5 3
c. String – A combinations of characters Negation** - -8 -8
Ex. 5th, Add_Num, 3A, #5 Anahaw St.,
Anna_Reyes *Modulus returns the remainder of a division
Examples: 7 mod 2 result: 1
3. Date and Time – These hold the day, month and the 6 mod 2 result: 0
year for the date and hour, minute and second for the **Negation stands for the negative value
time. Examples: -8
Ex. 4/14/1983, 12:45:30 -50

4. Logical Data – logical data type has only two (2) values; 2. Relational Operators – operators that are used to
True or False. False is equivalent to zero while True holds compare two values based on certain condition.
any other values. FUNCTION OPERATO EXAMPLE RESULT
Ex. True, False R
Greater than > 8>5 True
II. VARIABLE – Different types of data are stored in
Less than < 8<5 False
variables. A variable is a named area in the memory,
Equal to = 8=8 True
which holds temporary data. A program can handle
Not equal <> 8<>5 True

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 4
Greater than >= 8>=10 False
or equal
Less than or <= 8<=9 True
equal

3. Logical Operators – operators that determine if a


particular condition is satisfied.

FUNCTION OPERATOR EXAMPLE RESULT


AND AND 8>5And8<10 True
OR OR 8<5OR8<6 True
NOT NOT Not (2<8) False

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 5
V/B Module No. 3 Flowchart:

CONDITIONAL AND LOOF STATEMENTS Begin

I. CONDITIONAL STATEMENTS – is one of the vital


components in programming. It enables a program to
respond in a different manner every time a program is
executed depending on the data entered. Integer
Numbe
The commonly used conditional statements are: r
1. If Statement
2. If…Then…Else Statements
3. Select Case Statements Input Number

A. If Statements
This is used to evaluate a condition or check a value as
“true” or “false”. If the condition is not met, the
succeeding statements will be executed. Numbe
r =5?
Syntax: If Condition
Statement
End If

Example: MsgBox “WELCOME”

1. Input a number, if the number is 5, display the


message “WELCOME”
END
Algorithm:
Integer
Begin
Input Number
If Number = 5 B. If ….Then….Else Statements
MsgBox “WELCOME” This is use to evaluate a condition or check a value as
End If “true” or “false”. If the condition is not met, the
End statement or set of statements after will be executed.

Syntax: If condition then


Statement 1
Else

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 6
Statement 2
End if

Example:
C. Case Statement
1. Input a grade. If the grade is greater than or equal to The Case statement is similar to the use of If…Then…Else
75, display the message “You passed the subject”, statement. It is used when a variable is compared to
otherwise display “Needs of Improvement”. different values or expressions.

Algorithm: Syntax:
Integer Grade
Begin Select Case test expression
Input Grade Case Expresionlist1
If Grade >= 75 Statement Block1
Display “You passed the subject” Case Expresionlist2
Else Statement Block1
Display “Needs of Improvement”
End If End Select

Example:
Flowchart:
Conduct Grade Equivalent
1 – Excellent
Begin 2 – Very Good
3 – Good
4 – Satisfactory
5 – Needs Improvement

Integer 1. Input a conduct grade and display the equivalent


Numbe rating.
r
Algorithm:
Character Conduct
Input Number Begin
Input Conduct
Select Case Conduct
Case “1”
Print “Excellent”
Numbe Case “2”
Print “Very Good”
r =5?
Case “3”
Print “Good”
Case “4”

MsgBox
Source: Visual “WELCOME”
Basic (An Introduction to OOP)
By: Emily F. Trajano Page 7
END
Print “Satisfactory”
Case “5”
Print “Needs Improvement”

End Select
End Sub

Flowchart:

Begin

Character
Conduct

Input Conduct

Number MsgBox
=5? “WELCOME

END

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 8
Next Counter
End

II. LOOP STATEMENT


Flowchart:
Loop statement is the execution of sequence of
Begin
statements repeatedly until a particular condition is
“True” or “False”.

A counter is placed on the program to keep track of the


number of times the program segment has repeated. Integer
Counter =
The commonly used loop statements are:
1
1. For…. Next Statement.
2. Do…. While Statement.
3. Do…. Until Statement. Print “Count the
Number of loops”
A. For… Next Statement

This is used to execute a given action in a specified


number of times. The number of executions depends on
Counter = Counter + 1
the first and the final value assigned to variable counter.

Syntax:
For counter is = start to end (Step Increment)
Statements
Next Counter Counte
r=5
Example:

1. Display the sentence “Count the number of


loop.” Five times. END

Algorithm:
Integer Counter
B. Do… While Loop
Begin
Counter = 1
This is used to evaluate a condition if “true” or “false”. If
For Counter = 1 to 5
the condition is true, the statements given are executed.
Print “Count the number of loops.”
If false, the loop will stop and the action is terminated.
Counter = Counter + 1

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 9
In this kind of loop, the statements will only be executed
while the condition is true.

Syntax: C. Do… Until Loop


Do Until Condition
Statements(s) This is used to evaluate a condition if “true” or “false”.
Loop Unlike Do…While Loop, if the condition is false the
statements given are executed. If true, the loop will stop
and the action is terminated.

Example: In this kind of loop, the statements will only be executed


while the condition is false.
1. Input an age. If the age is above or equal to 18,
display the message “You can vote”. The Syntax:
program will only end if the user is a minor Do Until condition
Statement(s)
Algorithm: Loop

Integer Age
Begin
Do While Age >= 18
Input Age
Print “You can vote”
Loop
End

Flowchart:

Begin

Integer
Age

Input Age

Age Print “You can


> vote”
=5

END
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 10
Examples: clicking of a button, clicking of a menu, loading
of form.

8. Even Driven – an operation is executed as the result of


some kind of event.

9. Form – The first object you will see on the screen


when you open Visual Basic. It is where all the controls
are placed. It is also where you will enter data and see
the results.
V/B Module No. 4
THE VISUAL BASIC DEVELOPMENT ENVIRONMENT
10. Controls – the objects you put on a form.
Examples: text box, label, command button.
COMMONLY USED TERMS
There are many technical terms that you will encounter
11. Code – Computer instruction written by programmer.
as you go along studying Visual Basic. You must be very
These will indicate what the action or result will be when
familiar with these terms to easily understand the
an event occurs.
instructions that will be given in the next lessons to
come.
12. Script – Other name for code.
1. Application – collection of objects that work together
13. Design Time – a time when you visually design and
to accomplish certain task/s for its users.
layout the forms and the objects on it.
Examples: Microsoft Word, Adobe Photoshop,
Macromedia.
14. Run-time – a time when the program is executed.
2. Project – The term used in Visual Basic pertaining to
THE VISUAL BASIC INTEGRATED DEVELOPMENT
application.
ENVIRONMENT (IDE)
Examples: Payroll System, Quiz Bee Scoring Program.
The visual basic IDE will be your working area. It is a
3. Object – a piece of software that has properties and
graphical environment which allows you to easily design,
functions that can be managed or controlled.
run and debug the program you are creating.
Examples: a window, dialog box
Parts and Functions:
4. Property – characteristic of an object.
Examples: color, size, background
1. Title Bar – Shows the tile
2. Menu Bar – consist of different menus that hold
5. Methods – Functions of an object that can be
different functions.
manipulated.
3. Tool Bar – Consist of several buttons for commonly
Examples: opening, resizing, moving (of a window)
used commands.
4. Project Window – Contains the project files and
6. Object Oriented Environment (OOP) – a place
groups them by type.
wherein application is created using objects and
combining them to produce an output.
Examples: Visual Basic environment, Visual Studio
environment.

7. Event – an action that happens.

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 11
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 12
Project Window Buttons: Picture Displays images
Box
a. View Code Button – opens the window for writing
visual basic code. TextBox Accepts input given by the
b. View Object Button – displays the form user
c. Toggle Folders Button – Shows or hide the content of Displays text
the Forms folder. Command Creates a button that when
Button clicked, will perform a
CONTROLS ON THE TOOL BOX specific task.
Option Creates a button wherein
Control Button one button can only be
Button Name Function selected. Also called “radio
Label Displays text that cannot be button”
edited by the user of the
program. List Box Gives a list of items to
Frame Groups other control. choose from.
VScrollBar Shows a vertical scrollbar
Check Box Provides the option check or
uncheck.
DriveListBox Access the disk drives (Drive
A, C, D, etc.)
Combo Box Provides a list of choices for
FileListBox Access file inside a
the user to select from.
dicrectory
HScroll Bar Creates a horizontal scroll
Line Draws a line
bar.
Data Connects the program to a
Timer Counts the time intervals for
data base.
a specification action to take
place.
DirListBox Access directories.

Shape Draws rectangles, squares,


ovals, circles, rounded
rectangles and rounded
squares.
Image Shows images

OLE Relates with other


application

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 13
Do While Condition
Statement 1
Statement 2 Statement to be performed repeatedly,
Statement 3 as long as the condition is evaluated to
true
Statement n
Loop

Do-While-Statement
Example:
1. Design and develop a simple looping statement
program that generates the given sequence numbers
V/B Module No. 5 using the Do-While-Loop syntax. Follow the given figure
USING CONTROLS WITH LOOPING STATEMENTS in the designing and developing the application system.

Kinds of Looping Statements:


1. For… Next Statement
2. Do…While Statement
3. Do…Until Statement Text Box

Do Loop – statement execute a block of statement an


indefinite number of times. The Do Loops work well Command
when we don’t know how many times we need to Button
execute the statement in the loop.

Different Variations of Do Loop Statements:


Text Box
Do While condition
Note: When the user clicks the ENTER command button,
Statements ‘Statements that do something repetitively
a message box “You can vote” or “You are a minor” will
be generated.
Loop
When the VB executes a Do-While-Loop, it first
Procedure:
evaluates the condition. If the condition is False (0), it
1. Start a new project and select the Standard EXE on the
will now execute all the statements within the body of
given displayed items, then click the open command
the loop. If it’s True (1), VB executes the statements and
button. Set the caption of the Form to Do-While-Loop.
then goes back to the Do While statement and evaluates
2. Click a command button in the Toolbox to add it into
the condition again.
the form. Position it properly and adjust its width
appropriately.
Do Until condition
3. Click now the textbox in the Toolbox to add it into the
Statements ‘Statements that do something repetitively
form. Position it properly and adjust its width
appropriately.
Loop
4. Double-click the command button to display the code
In a Do-Until-Loop Statement, it Loops, zero or more
editor. Embed the following code to its respective
times.
procedures:
How The Do-While-Loop Iterates
Do-While-Loop Code:
Here are the graphical representations on how the
computer performs the iteration process:

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 14
Private Sub Command1_Click() 4. Double-click the command button to display the code
Do While [Link] >= 18 editor. Embed the following code to its respective
MsgBox ("you can vote") procedures:
Loop
MsgBox "You are a minor" For-Next-Loop Code:
End
End Sub Private Sub Command1_Click()
Dim nCounter As Integer
For nCounter = 1 To 5
Print "Loop", nCounter
Next nCounter
End Sub
How The For-Next Loop Iterates
For counter = start To end [step increment/decrement]
Statement 1 Saving a Project
Statement to be performed repeatedly,
Statement 2 until the end is reached
Statement 3 The For-Next-Loop application is now complete. Before
Statementn starting a new Visual Basic project or quitting Visual
Next Counter Basic, you should save your work.
Visual Basic projects are saved as set of files.
For-Next Statement Forms are saved as files with a file name and an .frm
Example: (form) extensions. If the form contains graphics, an
1. Design and develop a simple looping statement additional file name and an .frx (form extension)
program that generates the given sequence numbers extension is saved. In addition to form files, Visual Basic
using the For Next Syntax. Follow the given figure in creates an overall project file. This has a file name and a
designing and developing the application system. .vbp (Visual Basic Project) extension.

Command
Button

Procedure:
1. Start a new project and select the Standard EXE on the
given displayed items, then click the open command
button. Set the caption of the Form to For-Next-Loop.
2. Click a command button in the Toolbox to add it into
the form. Position it properly and adjust its width
appropriately.

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 15
Syntax:
[Link] index

Clear – clears the contents of List box or Combo Box


control.

Syntax:
[Link]

Example No. 1
Design and develop a simple program that demonstrates
how to preload a collection of items into the list box. The
user should be able to remove a selected item on the list,
one at a time. Follow the given figure below in designing
V/B Module No. 6 and developing the application program.
COMMON PROPERTIES FOR LIST BOX AND COMBO BOX
CONTROL

The List – returns or sets of items in a controls list Label


portion. The list is a string array in which each element is
a list item.
List Box
Syntax:
[Link](index) [=string]

Where:
Object either list box or combo box
Index the position of the item in the list
String express specifying the list item

Common Methods for List Box and Combo Box Control


Note: When the user selects and double-clicks an item at
Methods are written in a different way than properties. the List box, that particular item must be deleted from
the displayed list.
Syntax:
[Link] Code:
Private Sub Form_Load()
Three Commonly Used Methods [Link] "Kare-kare"
[Link] "Inihaw na Liempo"
AddItem – adds an item to a list box or combo box [Link] "Adobong Manok"
control. [Link] "Bicol Express"
[Link] "Nilagang Baka"
Syntax: [Link] "Paksiw na Bangus"
[Link] item[index] [Link] "Menudo"
[Link] "Sinigang na Hipon"
RemoveItem – removes an item from a List Box or End Sub
Combo Box control.
Private Sub List1_Click()

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 16
[Link] [Link] End Sub
End Sub
Example No.3
Example No. 2 Design and develop a simple program that demonstrates
Design and develop a simple program that demonstrates how to preload a collection of items in to the List Box.
how to preload a collection of items into the List box. The program should be able to display one or more
The program should be able to remove a selected item selected items on another list box. Follow the given
on the list, one at a time but with a confirmation using a figure below in designing and developing the application
command button. Follow the given figure below in program.
designing and developing the application program.
Note: When the user selects and double-clicks an item at
the List Box 1, the particular item must be displayed at
the list box 2.
Note: The user should select first an item at the List Box
by highlighting it. Before the user finally remove it by
clicking the remove command button. Label

Label List Box 2

List Box

Command
Button List Box 1

Code:
Private Sub Form_Load()
[Link] "Kare-kare"
[Link] "Inihaw na Liempo"
Code: [Link] "Adobong Manok"
Private Sub Command1_Click() [Link] "Bicol Express"
[Link] [Link] [Link] "Nilagang Baka"
End Sub [Link] "Paksiw na Bangus"
[Link] "Menudo"
Private Sub Form_Load() [Link] "Sinigang na Hipon"
[Link] "Kare-kare" End Sub
[Link] "Inihaw na Liempo"
[Link] "Adobong Manok" Private Sub List1_Click()
[Link] "Bicol Express" 'Additem to List Box 2
[Link] "Nilagang Baka" [Link] [Link]
[Link] "Paksiw na Bangus" End Sub
[Link] "Menudo"
[Link] "Sinigang na Hipon" Example No.4

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 17
Design and develop a simple program that demonstrate ‘Remove the Item from the List Box
how to preload a collection of items into the List Box The [Link] [Link]
program should be able to transfer one or more selected End Sub
items on another List Box Example No.5
Design and develop a simple program that demonstrates
Note: When the user selects and double-clicks an item at how to preload a collection of items into the List Box.
the List Box 1, the particular item must be displayed at The Program should be able to get the value of the
the list box 2. And when the user selects and double- currently selected item in the List Box. Follow the given
clicks an item at the List Box 2, the particular item will figure below in designing and developing the application
return to List Box 1. program:
Note:
When the user clicks one of the items in the List Box, the
description of that item is displayed at the Textbox.

Label List Box 1

List Box 1 Label

Text Box 1
List Box 2

Code:
Private Sub Form_Load()
Code: [Link] = ""
Private Sub Form_Load() [Link] "Germany"
[Link] "Java" [Link] "Philippine"
[Link] "C++" [Link] "Singapore"
[Link] "Perl" [Link] "Japan"
[Link] "Visual Basic" [Link] "Alaska"
[Link] "Visual FoxPro" [Link] "California"
[Link] "Turbo Pascal" [Link] "Italy"
[Link] "COBOL" End Sub
[Link] "HTML" Private Sub List1_Click()
End Sub If [Link] = "Germany" Then
Private Sub List1_Click() [Link] = ""
'Additem to the other List Box [Link] = "Germany is in Europe"
[Link] [Link] ElseIf [Link] = "Philippine" Then
‘Remove the Item from the List Box [Link] = ""
[Link] [Link] [Link] = "Philippines is in Asia"
End Sub ElseIf [Link] = "Singapore" Then
Private Sub List2_Click() [Link] = ""
'Additem to the other List Box [Link] = "Singapore is in Asia"
[Link] [Link] ElseIf [Link] = "Japan" Then

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 18
[Link] = "" (only Price 3 is declared as single; Price 1 and
[Link] = "Japan is in Asia" Price 2 are considered variants because they are
ElseIf [Link] = "Alaska" Then not explicitly declared)
[Link] = "" 2. Implicit Declaration – Variables is not formally
[Link] = "Alaska is in America" declared. It is a result from other variable declaration.
ElseIf [Link] = "California" Then Dim Num1 as Integer Explicit Declaration
[Link] = "" Dim Num2 as Integer Explicit Declaration
[Link] = "California is in America" Num3 = Num1 * Num2 Implicit Declaration
ElseIf [Link] = "Italy" Then
[Link] = "" In Implicit Declaration, the readability and clearness of
[Link] = "Italy is in Europe" the code is sometimes sacrificed. For VB consider implicit
declarations as error, you can use Option Explicit
End If clauses.
End Sub Option Explicit
V/B Module No. 7 Dim Num 1 as Integer, Gender as String
VARIABLES AND CONSTANT
Scope of Variables
VARIABLE
Variable is a sign or name that stands for a particular The scope indicates where the variable can be used only
value in a program. It may also store information while in the procedure, certain form or modules inside the
the program is running. application.

Conventions in Naming a Variable 1. Public Statement – variable can be used in several


1. A name should be descriptive of the data it forms of the project.
holds. It should be unique.
2. A variable must be given with a letter. 2. Private Statement – variable can only be used in the
3. Name can be up to 255 characters long. form where it is declared.
4. Must not contain a space or character such as !#
%&@ a. Module-Level – variables declared in the
5. Do not use dash to avoid confusion with the declaration section of the module. It is a variable
minus sign (-). Instead, use underscore_ only for the control in the form.
b. Procedure-Level – variables declared inside a
Declaring Variables procedure. It is only a variable when the
procedure is called.
We are now going to assign a name to a variable. These
are the 2 ways: ANALYZE THE CODE:

1. Explicit Declaration – variable is declared at the


beginning of the procedure in the Declaration Section
Dim Num_1 as Integer
Dim Age as String
Dim Reg_DOB as Date
Dim Name as String, Address as String, Num2 as
Integer
(3 variable declaration in one statement)
Dim Price1, Price2, Price3 as Single

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 19
1. Intrinsic Constant – defined by Visual Basic
vbTrue
Declaration Section vbBlack
- The list of intrinsic constants are found inside the
Object Browser
Procedure for
Command Button 1 2. User-Define Constant – defined by programmers
that writes the code
Const Pi = 3.1416
Procedure for Const Max_Num = 100
Command Button 2

Option Explicit – tells that no implicit declaration n is


allow.
Dim Sum as Integer – declared the variable Sum as
Integer.
-Sum can be used by any control in the form.

Private Sub Command1_Click()


Dim Num1 as Integer – Num1 is only declared for the
procedure.
Static Product as Integer – Instead of Dim, Static is used
to declare a variable that can be used by other
controls in the form (another way of declaring a
module-level variable, only it is inside a procedure
and not the declaration section)
Num1 = Num1 * 10 the value of Num1 is to be multiplied
by 10
- once the variable is called, it will go back to its
original value
Product = Num1 * 5 – the value of Product is multiplied
by 5 and will retain its value
End Sub

B. CONSTANT
A constant stores value that does not change during the
execution of the procedure.

2 Types of Constant

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 20
Frame

B. IMAGE
Image control displays picture on a form.

Image

V/B Module No. 7


OTHER VISUAL BASIC CONTROLS

A. FRAME
Frame is used to group several controls. When you move
a frame, all the controls inside will be move together
with the frame. You have to create the frame first and
create the controls inside after it.
C. OPTION BUTTON
The common controls placed inside a frame are the Option buttons are also called radio buttons. It is used to
option buttons and the check boxes. allow users to select only one button at a time in a group
of options.
Frame:

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 21
Label

Frame

Option
Button
Code:
Option Explicit
Private Sub Option1_Click()
Dim opt1 As String
If [Link] = True Then
[Link] = "FRESHMAN"
Private Sub Check1_Click()
End If
If [Link] = 1 Then
End Sub
opt1 = "Java"
Else
Private Sub Option2_Click()
opt1 = ""
If [Link] = True Then
End If
[Link] = "SOPHOMORE"
End Sub
End If
End Sub
Private Sub Check2_Click()
If [Link] = 1 Then
Private Sub Option3_Click()
opt2 = "Visual Basic"
If [Link] = True Then
Else
[Link] = "JUNIOR"
opt2 = ""
End If
End If
End Sub
End Sub
Private Sub Option4_Click()
Private Sub Check3_Click()
If [Link] = True Then
If [Link] = 1 Then
[Link] = "SENIOR"
opt3 = "Data Structure"
End If
Else
End Sub
opt3= ""
End If
End Sub
C. CHECK BOX
Private Sub Check4_Click()
Check boxes allow users to select several boxes at a time
If [Link] = 1 Then
in a group of options. Check boxes are grouped and
opt4 = "HTML"
usually placed inside a frame.
Else
opt4 = ""

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 22
End If Menus contain group of related commands like the one
End Sub in the menu bar of MS-Word or MS-Excel. They have
properties and events.
Private Sub Check5_Click()
If [Link] = 1 Then Menus are created using the menu editor. Menu Editor is
opt5 = "Pearl" also used to set the properties and events of the menu
Else or menu items.
opt5 = ""
End If Ways to open the Menu Editor
End Sub
 Click Tools Menu Menu Editor
Private Sub Check6_Click()  Ctrl + E
If [Link] = 1 Then  Click the Menu Editor button on the toolbar
opt6 = "Java"
Else
opt6 = ""
End If
End Sub

Private Sub Command1_Click()


[Link] = opt1 + ""
End Sub

Menu Editor

Parts of the Menu Editor

Name Description
Caption Name of the menu that will appear
on the form
Name The name called by Visual Basic
when the program runs
V/B Module No. 7
Prefix:
MENUS & TOOL BARS
Good descriptive mnu
names: mnuFile, mnuEdit, mnuHelp
MENU
Shortcut Sets the shortcut key for the menu

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 23
items
Checked Puts a check mark before the menu
control
Enable Allows the menu or menu items to
respond to events
Visible Shows the menu or menu items on
the screen
Window List Determines if menu control have a
list of open MDI (Multi-Document
Interface) child forms in the MDI
application
Next button Used to add or edit a new menu or
menu item
Insert button Creates a new menu or menu item
above a selected menu or menu
item
Delete button Removes a selected menu or menu
item
Arrow buttons Indents to create submenus or menu
items and position them according
to level

Source: Visual Basic (An Introduction to OOP)


By: Emily F. Trajano Page 24
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 25
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 26
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 27
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 28
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 29
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 30
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 31
Source: Visual Basic (An Introduction to OOP)
By: Emily F. Trajano Page 32

You might also like