7/30/2019 Vb 6.0 Tutorial
1/95
1
Meaning
of
Visual Basic
7/30/2019 Vb 6.0 Tutorial
2/95
VB as Programming Language
2
Visual Basic is a Programming tool that allows user to developWindows or
GUI ( Graphical User Interface ) applications. It means that rather
than writing numerous line for code to describe the appearance and
location for interface elements like C , C + + or Basic. User can simply
drag and drop pre-built objects into the form window on computer screen.
VB is a much enhanced version of BASIC programming language
( BASIC is Predecessor of VB ). The main difference is that the VB can
create windows programs by just Drag & Drop where as BASIC could onlycreate DOS programs or work only in CUI (Command User Interface ).
7/30/2019 Vb 6.0 Tutorial
3/95
Features of Visual Basic ( VB )
3
VB introduces the concept of Event Driven programming Model.
It has built in Database Handling Features of VB.
It incorporated the concepts of objects.
It has useful Error
Handling Features.
It has Front End Application Developer.
It also known as RAD ( Rapid Application Development )
7/30/2019 Vb 6.0 Tutorial
4/95
Event & Event Procedure
4
An event is an actionrecognized by an object, such
as clicking a mouse or pressinga key. User can write code torespond to that event. Eachobject has different eventsthat it recognizes.
An event procedure is a sectionof code that is executed when an
event (like clicking a commandbutton) takes place. Eventprocedures can be added to yourproject in the code editorwindow.
7/30/2019 Vb 6.0 Tutorial
5/95
Program Development Process
5
It is a process in which an ordered sequence of steps is defined to
develop an application. It includes :-
How the Program should Work
Plan about User Interface
Implement it
Test it
7/30/2019 Vb 6.0 Tutorial
6/95
6
Starting
Visual Basic
7/30/2019 Vb 6.0 Tutorial
7/95
STARTING VB APPLICATION
Opening a Project : -
To open a VB project , invoke VB6
shortcut on the Desktop or going throughthe pull-up menu select VB program .
Start Program Microsoft Visual
Basic 6.0 .
7
7/30/2019 Vb 6.0 Tutorial
8/95
Visual Basic I D E
8
When user choose any application platform, like any other window
application, VB consists of multiple window, which appear at start up. The
windows that are display when user start VB are collectively Known as
Visual basic Integrated Development Environment ( IDE ).
7/30/2019 Vb 6.0 Tutorial
9/95
Visual Basic I D E
9
ProjectWindow
PropertiesWindow
FormLayoutWindow
FormWindow
Toolbox
Toolbar
Title bar Menu bar
Visual Basic Integrated Development Environment
7/30/2019 Vb 6.0 Tutorial
10/95
Properties Window
10
The Properties window let user tochange the characteristics, or
property settings, of the form itselfand of visual interface elements on
the form. The Properties windowconsists of two columns: the first isthe name of the property, which usercannot change; the second column,which is the property setting thatuser can change. Any Change in the
property of the object will affectthe object on the form.
7/30/2019 Vb 6.0 Tutorial
11/95
Tool Box
11
The toolbox consists ofthe various objects thatuser will use to design theapplication. These objects,called controls, are objectsthat user see in allstandard Win applications,such as textboxes,command buttons, option
(radio) buttons,checkboxes, etc.
7/30/2019 Vb 6.0 Tutorial
12/95
Form Window
12
The Form window is thecentral area where usercan draw application. The
forms are windows whichholds control like Button ,Checkbox etc which makeuser application. The largearea of form is calledclient area for working
with different controls.
7/30/2019 Vb 6.0 Tutorial
13/95
Source Code Window
13
The Source Code window iswhere user type the codewhich VB execute. It has
two parts Control Box ,Event Box . The Heading ofwindow indicate whichevent the code isassociated . It is a windowwhere user write most of
the code .
7/30/2019 Vb 6.0 Tutorial
14/95
Source Code Window
14
It shows the control Boxwhich contains two controls( Command 1 & Form ).
7/30/2019 Vb 6.0 Tutorial
15/95
Source Code Window
15
It shows the Event Boxthat Selected Control canperform these event.
7/30/2019 Vb 6.0 Tutorial
16/95
Saving a VB program
16
Saving a Project : - After creating a Project , user need to save it . For
this click File menu and select save project. Then system asks : -
1. Provide a name for Form File ( . frm )
2. After that Provide a name for Project file ( .vbp )
7/30/2019 Vb 6.0 Tutorial
17/95
Running a program
17
Running a Project : - After saving the project , the next step is to
execute the program. Following are different ways to execute a
project.
1. Press F5 Key
2. On Menu Bar Click Run - Start
3. On VB Toolbar , Click Run Icon.
7/30/2019 Vb 6.0 Tutorial
18/95
Opening a vb program
After you created and saved a file , you
can open it at any time.
Click fileopenselect your vb program
from the open dialogok
Then you can get your saved vbapplivation.
18
7/30/2019 Vb 6.0 Tutorial
19/95
Steps in DevelopingApplication
1. Draw the user interface
2. Assign properties to controls3. Attach code to controls
7/30/2019 Vb 6.0 Tutorial
20/95
Draw the User Interface
Visual Basic operates in three modes.
Design mode - used to build application
Run mode - used to run the application Break mode - application halted and
debugger is available
7/30/2019 Vb 6.0 Tutorial
21/95
Getting Started
7/30/2019 Vb 6.0 Tutorial
22/95
7 Windows
1. Main Window
2. Form Window
3. Toolbox4. Properties Window
5. Form Layout Window
6. Project Window7. Code Editor Window
7/30/2019 Vb 6.0 Tutorial
23/95
Main Window
consists of the title bar, menu bar, and
toolbar.
7/30/2019 Vb 6.0 Tutorial
24/95
Form Window
is central to developing Visual Basic
applications. It is where you draw your
application.
7/30/2019 Vb 6.0 Tutorial
25/95
Toolbox
is theselection
menu for
controlsused in your
application.
7/30/2019 Vb 6.0 Tutorial
26/95
Properties Window
is used to
establish initial
property values
for objects.
7/30/2019 Vb 6.0 Tutorial
27/95
Form Layout Window
shows where (upon program
execution) your form will be
displayed relative to your
monitors screen.
7/30/2019 Vb 6.0 Tutorial
28/95
Project Window
displays a list of all forms
and modules making up
your application.
7/30/2019 Vb 6.0 Tutorial
29/95
Code Editor Window
Contains the actual Basic coding.
7/30/2019 Vb 6.0 Tutorial
30/95
Assign Properties to Controls
Naming Convention of Controls
Object Prefix Example
Form frm frmHello
Command Button cmd, btn cmdExit, btnStart
Label lbl lblStart, lblEnd
TextBox txt txtTime, txtName
Menu mnu mnuExit, mnuSave
Check box chk chkChoice
7/30/2019 Vb 6.0 Tutorial
31/95
Attach Code to Controls
Code in a Visual Basic application is
divided into smaller blocks called
procedures.
Event procedure syntax:
object name_event()
Ex. Command1_Click ()
7/30/2019 Vb 6.0 Tutorial
32/95
Private Sub object name_event()
End Sub
___________________________________
____Private Sub Command1_Click ()
End Sub
7/30/2019 Vb 6.0 Tutorial
33/95
Private Sub Command1_Click ()
object name.property= value
End Sub
_______________________________________
Private Sub Command1_Click ()
Text1.Text = "Hello, world!"
End Sub
7/30/2019 Vb 6.0 Tutorial
34/95
Variables
Variable is a data name that is used to
strore a value.
Syntax :
Dim variablename as datatype
Example :
Dim a as IntegerDim b as Integer,c as Double,d as Date
34
R l d i i i bl
7/30/2019 Vb 6.0 Tutorial
35/95
Rules used in naming variables
Variable name cannot exceed 255 characters.
They may include letters, numbers, and
underscore (_).
The first character must be a letter.
You cannot use a reserved word like
Dim,If,Else etc. Vb is not case sensitive.
7/30/2019 Vb 6.0 Tutorial
36/95
Data Types
VB supports all common data types such
as Bytes,Boolean,Date,Integer,String,
Double and variant etc.
Integer To store a integer value
String To strore a String (group of
characters).
DoubleTo store decimal value.
Visual Basic Controls
7/30/2019 Vb 6.0 Tutorial
37/95
Visual Basic Controls
Controls are the elements of the User Interface
with which the user can interact and
control the application. Form is the first and mainelement of the user interface which acts
as container for other elements. The basic
controls are displayed as icons on the toolboxwhich is a part of the interface during the design
time.
37
7/30/2019 Vb 6.0 Tutorial
38/95
TOOL BOX
38
7/30/2019 Vb 6.0 Tutorial
39/95
PICTURE BOX & IMAGE BOX
39
Displays a graphic image. Itcan also serve as acontainer for other controls
Displays a graphic image.Similar in appearance to thepicture control but with
different functionality.Main property:Strutch =true
7/30/2019 Vb 6.0 Tutorial
40/95
LABEL ,TEXT BOX & BUTTON
40
Displays text that the usercan edit.Main Properties :Multiline to enter multiple lines in text box.Password char to enter password format.Scollbar To make vertical or horisontal
Scrollbars.
Enables the user to initiate aprogram action.
To pass information to the user throughtext.
7/30/2019 Vb 6.0 Tutorial
41/95
FRAME & TIMER
41
Serves as a container for other controls.Provides a method for groupingcontrols. (To group controls in a frame, selectthe frame with a single-click first,
then draw a control in it.)
Provides a means for an action to be takenafter passage of a certain amount of time.
Main Property :Interval property integer value ranges from0 to 65535.A zero value disables thetimer.Positive value represents the number ofmilliseconds between timed events.
7/30/2019 Vb 6.0 Tutorial
42/95
CHECK BOX & OPTION BUTTON
42
Displays or enables input ofa two-part choice, such asYes/No or True/False.At a time more thanone check box can be selected.
Value 1=checked and 0=unchecked.
Displays or enables achoice among multipleitems. (Also known as a
radio button.).Only one option can be select at a time.True=option selected and False=Optiondeselected.
7/30/2019 Vb 6.0 Tutorial
43/95
OLE & DATA CONTROL
43
OLE=Object linking and embedding which is usedto place other files such as word,poerpoint,exceletc with the vb form.
Provides a link to database files.It act as amediator between front end (vb) and back end(database=access).That means it connects vb and access.
7/30/2019 Vb 6.0 Tutorial
44/95
COMBO BOX & LIST BOX
44
Enables the user to select anentry from a list or enter anew value.It is a combination of text box and list box.Value can to added to it through coding or by
using list property in pro.pert box
Displays a list of itemsfrom which the user can
select one or more entries.Value can to added to it through coding or byusing list property in pro.pert box
7/30/2019 Vb 6.0 Tutorial
45/95
HscrollBar & VscrollBar
45
Produces a numerical value
based on the scroll bar'svertical position.
Produces a numerical valuebased on the scroll bar'shorisontal position.
DriveListBox & Dir ListBox &
7/30/2019 Vb 6.0 Tutorial
46/95
DriveListBox & Dir ListBox &
FileListBox
46
Displays and enables a user to choose fromavailable disk drives on the computer.
Displays and enables a userto choose from availablesubdirectories on a drive.
Displays and enables a userto choose from availablefiles in a directory.
7/30/2019 Vb 6.0 Tutorial
47/95
LINES & SHAPES
47
Displays lines on the form
Displays geometric shapeson the form such as circle,rectangle,oval
etc.
7/30/2019 Vb 6.0 Tutorial
48/95
OPERATORS
VB contains the following operators.
Arithmetic operator
Assignment operator
Relational operator
Logical Operator
48
7/30/2019 Vb 6.0 Tutorial
49/95
ARITHMETIC OPERATOR
SYMBOL MEANING
+ Addition
- Subtration
* Multiplication
/ Common division operator that gives result
in real number (decimal number).
\ Used for integer division that gives result in
integer.^ Exponential (To get the power value.).
Mod To get the remainder.
49
7/30/2019 Vb 6.0 Tutorial
50/95
ASSIGNMENT OPERATOR
The = sign is used to assign a value to a
variable.
Example :
Dim a as Integer
a=10
50
7/30/2019 Vb 6.0 Tutorial
51/95
RELATIONAL OPERATOR
SUMBOL MEANING
= Equal Not Equal
< Less than
> Greater than
< Less than or equal to
> Greater than or equal to
51
7/30/2019 Vb 6.0 Tutorial
52/95
Logical Operator
The logical operators are
52
SYAMBOL MEANING
AND Both conditions must be true.
OR Either one condition is true.
NOT True changes to false case and vice versa
7/30/2019 Vb 6.0 Tutorial
53/95
VB Control Structures
In a program, statements may be executedsequentially, selectively or iteratively. Every
programming language provides constructs tosupport sequence, selection or iteration. Sothere are three types of programmingconstructs :
Sequential Constructs Selection Constructs
Iterative Constructs
53
CONTROL FLOW
7/30/2019 Vb 6.0 Tutorial
54/95
Sequential Construct
The sequential construct means the
statements are being executed
sequentially. This represents the default
flow of statements.
54
Stament 1
Stament 2
Stament 3
7/30/2019 Vb 6.0 Tutorial
55/95
Selection Construct
The selection construct means the
execution of statement(s) depending
upon the condition-test. If a condition
evaluates to true, a course-of-action (aset of statements) is followed otherwise
another course-of-action is followed. This
construct is also called decisionconstruct as it helps in decision making.
55
7/30/2019 Vb 6.0 Tutorial
56/95
Selection Construct
56
Condition? Statement 1 Statement 2
Statement 2
Statement 1
One course-of-action
Anothercourse-
of-action
true
false
7/30/2019 Vb 6.0 Tutorial
57/95
Iterative Constructs
The iterative or repetitive constructs
means repetition of a set-of-statements
depending upon a condition-test. A set-
of-statements are repeated again andagain till the condition or Boolean
Expression evaluates to true. The
iteration constructs are also called aslooping constructs.
57
7/30/2019 Vb 6.0 Tutorial
58/95
Iterative Construct
58
Condition?
Statement 2
Statement 1 The loopbody
false
The exitcondition True
7/30/2019 Vb 6.0 Tutorial
59/95
Selection Constructs
VB provides two types of selection construct :
1) If statement
2) Select Case statement
The If Statement : If statement of VB comes invarious forms & are given below:
1) If..Then Statement
2) If..Then..Else Statement
3) If..Then..ElseIf Statement
4) Nested Ifs
f S
7/30/2019 Vb 6.0 Tutorial
60/95
If..Then Statement
Def. : An If..Then statement tests a
particular condition; if the condition
evaluates to true, a course-of-action is
followed otherwise it is ignored. Syntax :
If (condition) Then
statements
End If
If Th S
7/30/2019 Vb 6.0 Tutorial
61/95
If..Then Statement
Example 1. :If (Num>0) Then
Print It is a positive number
End if
Example 2 :
If txtAge.Text>=18 ThenPrint You are eligible to vote
End if
If Th El St t t
7/30/2019 Vb 6.0 Tutorial
62/95
If..Then..Else Statement
If..Then..Else statement provides an alternatechoice to the user i.e. if the condition is truethen a set of statements are executedotherwise another set of statements are
executed. Syntax :
If (condition) Then
VB Statement(s)Else
VB Statement(s)
End If
f f
7/30/2019 Vb 6.0 Tutorial
63/95
Examples of If..Then..Else
Example 1 :
If txtAge.Text>=18 ThenPrint You are eligible to vote
Else
Print Sorry, You are not eligible to vote
End If
Example 2 :
If Num Mod 2=0 Then
Print It is an Even Number
Else
Print It is an Odd Number
End If
If Th El If S
7/30/2019 Vb 6.0 Tutorial
64/95
If..Then..ElseIf Statement
If..Then..ElseIf statement is used to test a number of mutuallyexclusive cases and only executes one set of statements for thecase that is true first.
Syntax :
If (condition 1) Then
Statement(s)ElseIf (condition 2) Then
Statement(s)
ElseIf (condition 2) Then
Statement(s)
:Else
Statement(s)
End If
E l f If Th El If
7/30/2019 Vb 6.0 Tutorial
65/95
Example of If..Then..ElseIf
If (Age
7/30/2019 Vb 6.0 Tutorial
66/95
Nested Ifs
A nested If is an if that has another If in its ifsbody or in its elses body. The nested if can haveone of the following three forms :
1. If (expresssion 1) Then
If (expression 2 ) Then
Statement 1[Else
Statement 2
End If
Else
body-of-else]
End If
N t d If
7/30/2019 Vb 6.0 Tutorial
67/95
Nested Ifs
2. If (expression 1) Then
body-of-if
Else
:
If (expression 2) Then
Statement-1
[Else
Statement-2]
End If
Nested Ifs
7/30/2019 Vb 6.0 Tutorial
68/95
Nested If s
3) If (expression 1) Then
:
If (expression 2) Then
Statement-1
[Else
Statement-2]
End IfElse
If (expression 3) Then
Statement-3
[Else
Statement-4]:
End If
End If
E l f N t d If
7/30/2019 Vb 6.0 Tutorial
69/95
Example of Nested Ifs
If Num>0 Then
Print It is a positive number
Else
If Num
7/30/2019 Vb 6.0 Tutorial
70/95
Select-Case Statement
Select-Case is a multiple branchingstatement and is used to executed a set of
statements depending upon the value of
the expression. It is better to use Select-Case statement in comparison to
If..Then..ElseIf Statement when the
number of checks are more. There are 3
different forms of using Select-Case
statements and are given below :
Diff t f f S l t C
7/30/2019 Vb 6.0 Tutorial
71/95
Different forms of Select-Case
1. Select Case : Simplest Form [Exact match]
Select Case Expression
Case Value
one or more visual basic statements
Case Value
one or more visual basic statements
Case Else :one or more visual basic statements
End Select
E l f F 1
7/30/2019 Vb 6.0 Tutorial
72/95
Example of Form 1
Select Case byMonth
Case 1,3,5,7,8,10,12
number_of_days=31
Case 2
number_of_days=28
Case 4,6,9,11
number_of_days=30
End Select
Syntax of Form 2
7/30/2019 Vb 6.0 Tutorial
73/95
Syntax of Form 2
Select Case : Second Form [Relational Test]
Select Case Expression
Case is relation :
one or more visual basic statements
Case is relation :
one or more visual basic statements
[Case Else :
one or more visual basic statements
End Select
Example of Form 2
7/30/2019 Vb 6.0 Tutorial
74/95
Example of Form 2
Select Case marksCase Is < 50
Result = Fail
Case Is < 60Result = Grade B
Case Is < 75
Result = Grade A
Case Else
Result = Grade A+
End Select
Third Form of Select Case
7/30/2019 Vb 6.0 Tutorial
75/95
Third Form of Select Case
Select Case : Third Format [Range Check]
Select Case Expression
Case exp1 To exp2:
one or more visual basic statements
Case exp1 To exp2:
one or more visual basic statements
[Case Else:
one or more visual basic statements
End Select
Example of Form 3
7/30/2019 Vb 6.0 Tutorial
76/95
Example of Form 3
Select Case Age
Case 2 to 4 : Print PreNursery
Case 4 to 6 : Print Kindergarden
Case 6 to 10 : Print Primary
Case Else : Print OthersEnd Select
Iterative Constructs (Looping
7/30/2019 Vb 6.0 Tutorial
77/95
( p g
Structures)
A loop is said to be the set of instructions
which are repeated again and again in aprogram.
TYPES OF LOOP
7/30/2019 Vb 6.0 Tutorial
78/95
TYPES OF LOOP
1) Sentinel-controlled Loop Structures :repeat statements until a special valuecalled sentinel value (or the terminating
value) is reached.2) Counter-controlled Loop Structures :
repeat the set of statements until thevalue specified by the counter variable
is reached.
78
Looping Structures
7/30/2019 Vb 6.0 Tutorial
79/95
Looping Structures
VB offers broadly following three types oflooping structures :
1. For..Next
2. Do Loopa) Do While..Loop
b) Do..Loop While
c) Do Until..Loopd) Do..Loop Until
3. While..Wend
F N t St t t
7/30/2019 Vb 6.0 Tutorial
80/95
For..Next Statement
This type of statement is used when theuser knows in advance how many times
the loop is going to be executed.
Syntax :
For = To
Step
One or more VB Statements
Next
Examples
7/30/2019 Vb 6.0 Tutorial
81/95
Examples
Example 1 : Generate natural nos from 1to 100
For I = 1 To 100
Print INext I
Example 2 : Generate first 20 even nos.
For E = 2 to 40 Step 2Print E
Next E
More Examples
7/30/2019 Vb 6.0 Tutorial
82/95
More Examples
Example 3 : Generate odd nos from 100to 30 in a list box.
For O = 99 to 31 Step -2
ListO.AddItem(O)Next O
Example 4 : Generate table of any number
N.For T = 1 To N
Print N; *; T; =; N*T
Next T
More Examples
7/30/2019 Vb 6.0 Tutorial
83/95
More Examples
Example 5 : Find factorial of a givennumber N.
:
Fact=1For I= 1 to N
Fact = Fact * I
Next IPrint Factorial of ; N; =; Fact
:
Do Loop Structures
7/30/2019 Vb 6.0 Tutorial
84/95
Do..Loop Structures Do While..Loop : Do While loop is an entry
controlled loop in which the condition isplaced at the entry point. This statementexecutes the statements specified in thebody of the loop till the condition evaluatesto true. The loop may not be executed atall the if the condition is initially false.
Syntax :
Do While
One or more VB Statements
Loop
Examples of Do While Loop
7/30/2019 Vb 6.0 Tutorial
85/95
Examples of Do While..Loop
Example 1 : Never executes loop
Dim A as ByteA=10
Do While A>10
A=A-1
Loop
Example 2 : Executes loop
Dim P as Byte
P=20Do While P>5
P=P-2
Loop
Do Loop While
7/30/2019 Vb 6.0 Tutorial
86/95
Do..Loop While
Do Loop While is an exit controlled loop asthe condition is placed at exit point. Thebody of the loop is going to be executed at
least once whether the condition evaluatesto true or false. Loop is executed as longas the result of the condition remains true.
Syntax :
Do
One or more VB Statements
Loop While
Examples
7/30/2019 Vb 6.0 Tutorial
87/95
Examples
Example 1 :Do
num = InputBox (Enter a number)
sum = sum + num
Loop While num < > 0
Here the statements inside the loop will be
executed once no matter what the
comparison test evaluates to.
Do Until Loop
7/30/2019 Vb 6.0 Tutorial
88/95
Do..Until Loop
Do Until loop is an entry controlled loop inwhich the condition is placed at the entry point.This statement executes the statementsspecified in the body of the loop till the
condition evaluates to false. The loop may notbe executed at all the if the condition is initiallytrue.
Syntax :
Do Until One or more VB Statements
Loop
Examples of Do Until Loop
7/30/2019 Vb 6.0 Tutorial
89/95
Examples of Do Until..Loop
Example 1 : Never executes loop
Dim A as ByteA=10
Do Until A
7/30/2019 Vb 6.0 Tutorial
90/95
Do..Loop Until
Do Loop Until is an exit controlled loop asthe condition is placed at exit point. Thebody of the loop is going to be executed at
least once whether the condition evaluatesto true or false. Loop is executed as longas the result of the condition remainsfalse.
Syntax :Do
One or more VB Statements
Loop Until
7/30/2019 Vb 6.0 Tutorial
91/95
Examples
Example 1 :Do
num = InputBox (Enter a number)
sum = sum + num
Loop Until num = 0
Here the statements inside the loop will be
executed once no matter what the
comparison test evaluates to.
While Wend
7/30/2019 Vb 6.0 Tutorial
92/95
While..Wend
While..Wend loop is functionally equivalentto the Do While..Loop. It executes a set of
VB statements till the condition evaluates
to true.
Syntax :
While
one or more vb statements
Wend
Examples
7/30/2019 Vb 6.0 Tutorial
93/95
Examples
Example 1 : Generate the sum of first 10natural nos
I = 1
While I
7/30/2019 Vb 6.0 Tutorial
94/95
Nested Loops
A loop within another loop is called asNested Loop.
Example :
For I = 1 to 5For J = 1 To 3
Print J
Next JPrint
Next I
Inner Loop Outer Loop
Working of Nested Loops
7/30/2019 Vb 6.0 Tutorial
95/95
Working of Nested Loops
In nested loops the inner loop is executedcompletely for one time execution of the
outer loop. In the Previous example the
Inner Loop will be executed three times forevery execution of the outer loop.
Nested loops are very useful when there is
a requirement to generate different kind ofpatterns as output.