Sunteți pe pagina 1din 56

IT

Presentat
Series
ion
Overview on
VISUAL BASIC
6
Part III

IT Training Module 3 VISUAL BASIC 6 3


Variables,
Operators &
Functions

IT Training Module 3 VISUAL BASIC 6 4


A. Variables

IT Training Module 3 VISUAL BASIC 6 5


Variable
•A sign or a name that stands for a
particular value in a program. It
may also store information while
the program is running.

•Variables are assigned a data


type that indicates what kind of
data it will store and the amount of
memory it will use.
IT Training Module 3 VISUAL BASIC 6 6
Conventions in Naming a Variable

1.A name should be descriptive of the


data it holds. It should be unique.

2.A variable must begin with a letter.

3.Name can be up to 255 characters


long.
IT Training Module 3 VISUAL BASIC 6 7
Conventions in Naming a Variable

4. Must not contain a space or


characters used to identify data type
such as ! # % & @

5. Do not use dash to avoid confusion


with the minus sign (-). Instead, use
underscore _
IT Training Module 3 VISUAL BASIC 6 8
Data Types
• Byte • Currency (scaled
• Boolean integer)
• DecimalDate
• IntegerLong (long
integer) • ObjectString (variable-
length)
• Single (single-
• String (fixed-length)
precision floating-
point) • Variant (with numbers)
• Double (double- • Variant (with characters)
precision floating- • User-defined (using
point) Type)
IT Training Module 3 VISUAL BASIC 6 9
Declaring Variables

1.Explicit Declaration - variable is


declared at the beginning of the
procedure in the Declaration Section

Examples:
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 declarations in one statement)
Dim Price1, Price2, Price3 as Single
(only Price 3 is declared as Single; Price1 and Price2 are
considered Variants because they are not explicitly declared)
IT Training Module 3 VISUAL BASIC 6 10
LET’S
ANALYZE
THE
CODE!!!
Declarations Section

Procedure for
Command button 1

Procedure for
Command button 2
IT Training Module 3 VISUAL BASIC 6 11
Option Explicit – tells that no implicit declaration is
allowed
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 this
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
declarations section)
IT Training Module 3 VISUAL BASIC 6 12
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

Private Sub Command2_Click()


Dim Num1 As Integer – Num 1 is declared as Integer
Num1 = Num1 + 1 – the value of Num1 is
incremented by 1
End Sub
IT Training Module 3 VISUAL BASIC 6 13
B. Constants

IT Training Module 3 VISUAL BASIC 6 14


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

IT Training Module 3 VISUAL BASIC 6 15


2 Types of Constants

1. Intrinsic Constants – defined by VB


examples: vbTrue, vbBlack

2. User-defined Constants – defined


by programmers who writes the code
examples: Const Pi = 3.1416
Const Max_Num = 100

IT Training Module 3 VISUAL BASIC 6 16


Object Browser
The Object Browser displays all
intrinsic constants that are
related to the properties of the
controls you created, including
the procedures and modules you
define for the project.
IT Training Module 3 VISUAL BASIC 6 17
To open:
Click the Object Browser button on
the Standard Toolbar
or
Press F2 function key on the keyboard

Description of intrinsic
constant is found below the
IT Training Module 3 window.
VISUAL BASIC 6 18
C. Operators

IT Training Module 3 VISUAL BASIC 6 19


Operators – are symbols that
represent a specific action

Types of Operators:

1.Mathematical & Text Operators –


performs mathematical operations on
values and variables
IT Training Module 3 VISUAL BASIC 6 20
OPERATOR DEFINITION EXAMPLE RESULT
^ Exponent 5^2 25
(power of)
*  Multiplication 5*3 15

/  Division 15 / 5 3
+  Addition 15 + 4 19
-  Subtraction 15 - 5 10
Mod Remainder of 15 Mod 4 3
division
\ Integer 25 / 4 1
division
&  String “Hi ” & Hi User!
concatenation “User!”
The order of evaluation:
Parenthesis ( ), ^, *, /, \, Mod, +, -
IT Training Module 3 VISUAL BASIC 6 21
Types of Operators:

2.Relational Operators – compares


two values and returns a logical result
of true or false

IT Training Module 3 VISUAL BASIC 6 22


OPERATOR DEFINITION EXAMPLE RESULT
=  Equal to  10 = 8 False
>  Greater than  10 > 8 True
<  Less than  10 < 8 False
>=  Greater than or 10 >= 8 True
equal to
<=  Less than or 10<=10 True
equal to
<> Not equal to 10<>5 True
AND Logical AND  (10=10) AND True
(5>4)
OR  Logical OR  (10=5) OR True
(10>2)

IT Training Module 3 VISUAL BASIC 6 23


Types of Operators:

3.Logical Operators – applies a logical


expression to one or more logical
values

IT Training Module 3 VISUAL BASIC 6 24


OPERATOR DEFINITION EXAMPLE RESULT
NOT Negation 10 = NOT 5 TRUE
AND Logical AND  (10=10) AND True
(5>4)
OR  Logical OR  (10=5) OR True
(10>2)

IT Training Module 3 VISUAL BASIC 6 25


D. Functions

IT Training Module 3 VISUAL BASIC 6 26


Functions – are procedures that return
a result when called. There are many
functions by type of statements. We will
only cover the commonly used ones.

Most functions are by a pair of


parenthesis

They often appear on the right side of


an equal sign
IT Training Module 3 VISUAL BASIC 6 27
Examples:

Num1 = SQR(16)

Cur_Date = Date

IT Training Module 3 VISUAL BASIC 6 28


Commonly Used Functions:
1. String Functions
Len Returns the Len(string)
number of
characters of a
string
Example: Result:
Dim Name as String, Result as 6
String
Name = “Monica”
Result = Len(Name)
Left Returns a Left(string,length)
specified number
of characters from
the left side of the
string

Example: Results:
Dim Message as String, Result as Share
Right Returns a specified Right(string,length)
number of
characters from the
right side of the
string
Example: Results:
Dim Message as String, Result as blessings
String sings
Message = “Share your blessings.” INGS
Result = RightMessage,9)
Result = Right(Message,5)
Result = UCASE(Right(Message,3))
Mid Returns a number of Mid(string,starting
characters of a position,length)
string starting from
at a position
indicated
Example: Results:
Dim Quote as String, Result as String Patience
Quote = “Patience is a Virtue” To
Result = Mid(Quote,1,8) Virtue
Result = Mid(Quote,10,2)
InStr Returns the position InStr(string1,string1
of string2 in string1 )

Example: Results:
Dim Result as String, 4
Result = InStr(1002, 2)
UCase Changes all UCase(String)
lowercase letters
into uppercase
letters
Example: Result:
Dim Warning as String, Result as String WET FLOOR
Warning = “Wet Floor”
Result = Ucase(Warning)

LCase Changes all LCase(String)


uppercase letters
into lowercase
letters
Example: Result:
Dim Warning as String, Result as String slippery when wet
Warning = “Slippery When Wet”
Val Converts values to Val(String)
numbers

Example: Result:
Dim Num as String, Result as String 5
Num = 5 (as a number)
Result = Val(Num)
Commonly Used Functions:
2. Date Functions
Now Returns both the Now()
current date and
time from the
system clock
Example: 10/16/2005
Lbl1.Caption=Now() 6:30:23 PM
Date Returns the Date()
current date of
the computer
being used
Example: Result:
Dim Cur_Date as String 10/15/2000
Cur_Date = Date
Time Returns the Time()
current time of
the computer
being used
Example: Result:
Dim Cur_Time as String 2:08:34 PM
Cur_Time = Time
Format Returns a Format(string,
formatted string “specified
according to user- format”)
defined format

Example: 2006-10-16
Dim Cur_Date as Date
Lbl1.caption= Format(Cur_Date,
“yyyy-mm-dd”)
Commonly Used Functions:
Formatting Characters for Date:
Formatting Description Example Formatting Descriptio Example
Characters Characters n

yy Year 06 dd Day of the 09


without month
century with zero

yyyy Year with 2015 dddd Name of Friday


century the day of
the week

m Number of 11 h Hour 8
month without
zero
mmm Short name nov hh Hour with 08
of month zero
mmmm Long name Novembe mm Minutes 30
of month r
d Day of the 9 ss Seconds 26
month
without zero
Commonly Used Functions:
3. Number Functions
Format Returns a Format(string,
formatted string “specified format”)
according to user-
defined format
Example: 0245.60
Dim Result as Single $245.60
Result = 245.6
Lbl1.caption= Format(Result,
“0000.00”)
Lbl1.caption= Format(Result,
“$##00.00”)
Formatting Characters for Numbers:

Formattin Description
g
Character
s
0 Represents a digit, with non-significant
leading and trailing zeros
# Represents a digit, without non-significant
leading and trailing zeros
. Decimal placeholder
, Thousands separator
$+-() Literal character; displayed as typed
space
Commonly Used Functions:
3. Input/Output Functions
InputBox Displays a dialog InputBox(“prompt
box with a prompt ”)
and a textbox with
the OK and
CANCEL button

Example:
InputBox ("Type your name:")
MsgBox Displays a pop-up MsgBox(“prompt”
box with a )
message and
waits the user to
click a button.
Example:
InputBox() FUNCTION

InputBox() function displays a


message box where user can input
data.
Syntax
:
variable=InputBox(Prompt, Title,
default_text, x-position, y-position)
Prompt - The message displayed prompting
what is to be entered

Title - The title of the Input Box.

default-text - The default text that appears


inside the input box

x-position and y-position - the position or the


coordinates of the input box
Examp
le:

Co
de

At Run-
time
MsgBox() FUNCTION

MsgBox function produces a pop-up


message box and prompts the user
to click on a command button before
going to the next instruction.
Syntax
:
variable=MsgBox(Prompt, Style
Value, Title)
Prompt - The message displayed prompting
what is to be entered

Title - The title of the Input Box.

default-text - The default text that appears


inside the input box

x-position and y-position - the position or the


coordinates of the input box
Style
ValuesNamed Constant Displayed Button/s
Style
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
The style value can be used instead of the named constant
Examples: like in the example below.
Smenu=MsgBox( "Click OK to Proceed", 1, "Startup Menu")
  Smenu=MsgBox("Click OK to Proceed". vbOkCancel,"Startup Menu")
Style
Values
The style value can be used instead of
the named constant like in the example
below.
Examples:
Smenu=MsgBox( "Click OK to Proceed", 1, "Startup
Menu")
  Smenu=MsgBox("Click OK to Proceed“,
vbOkCancel,"Startup Menu")
The variable holds values that are returned by the
MsgBox ( ) function. The values are determined by the
type of buttons being clicked by the user. It has to be
declared as Integer data type in the procedure or in
the general declaration section.
Return Values and Command
Buttons
Value Named Constant Clicked Button
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
Time Allotment:
HANDS-ON EXAMPLE 1 20 minutes

1.Encode the following codes.


2.Run the program.

IT Training Module 3 VISUAL BASIC 6 48


Code
At run-time

At Run-Time When OK button is


clicked

When CANCEL
When VALIDATE
button is clicked
button is clicked
Time Allotment:
HANDS-ON EXAMPLE 2 20 minutes

1.Encode the following codes.


2.Run the program.

IT Training Module 3 VISUAL BASIC 6 51


Icons can be placed inside the
message box to add more distinction
to the message it wants to convey.

ICON
S
Named Icon
Value Constant
16 vbCritical
32 vbQuestion
48 vbExclamation
64 vbInformation
Code
Message Box with Icon
Time Allotment:
HANDS-ON ACTIVITY 30 minutes

1. Create a currency converter program.


2. The user must input the following:
-the exchange rate per P1.00
-the amount that will be converted
3. The program should display how much the
converted money will be.
4. Design or user interface.

Project: Currency Converter File: Currency

IT Training Module 3 VISUAL BASIC 6 55


End of Part III

IT Training Module 3 VISUAL BASIC 6 56

S-ar putea să vă placă și