Visual Basic Tutorial
Visual Basic Tutorial
"he code attached to this button3s 'lic. event is a simple loop that evaluates a function
at several values.
+rivate *ub 'ommand-9'lic.()
)im I As !nteger, ? As !nteger
I H G
)o
? H ,cn(I)
I H I ( -
5oop 8hile I XH 6G
End *ub
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
:@
"his code begins with an I value of G and computes the ? value using the general integer
function ,cn. !t then increments I by - and repeats the 5oop. !t continues looping 8hile I is
less than or equal to 6G. "he function ,cn is computed using<
,unction ,cn(I As !nteger) As !nteger
,cn H '!nt(G.- J I M 6)
End ,unction
"his code doesn3t do much, especially without any output, but it ma.es a good example for
loo.ing at debugger use. *et up the application and get ready to try debugging.
Using the Deugging $ools
Y "here are several debugging tools available for use in Visual 1asic. Access to these tools is
provided with both menu options and buttons on the )ebug toolbar. "hese tools include
brea.points, watch points, calls, step into, step over, and step out.
"he simplest tool is the use of direct prints to the immediate window.
Printing to the Immediate (indow.
?ou can print directly to the immediate window while an application is running. *ometimes,
this is all the debugging you may need. A few carefully placed print statements can sometimes
clear up all logic errors, especially in small applications.
"o print to the immediate window, use the +rint method<
)ebug.+rint Z5ist of variables separated by commas or semicolons[
)ebug.+rint Example<
+lace the following statement in the 'ommand-9'lic. procedure after the line calling
the general procedure ,cn<
)ebug.+rint I0 ? and run the application.
Examine the immediate window. Lote how, at each iteration of the loop, the program
prints the value of I and ?. ?ou could use this information to ma.e sure I is
incrementing correctly and that ? values loo. acceptable.
=emove the )ebug.+rint statement.
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
:A
&rea:points.
!n the above examples, the program ran to completion before we could loo. at the debug
window. !n many applications, we want to stop the application while it is running, examine
variables and then continue running. "his can be done with brea.points.
A brea.point is a line in the code where you want to stop (temporarily) the execution of the
program that is force the program into brea. mode. "o set a brea.point, put the cursor in the
line of code you want to brea. on. "hen, press X,E\ or clic. the 1rea.point button on the
toolbar or select "oggle 1rea.point from the )ebug menu. "he line will be highlighted.
8hen you run your program, Visual 1asic will stop when it reaches lines with brea.points and
allow you to use the immediate window to chec. variables and expressions. "o continue
program operation after a brea.point, press X,@\, clic. the =un button on the toolbar, or
choose *tart from the =un menu.
?ou can also change variable values using the immediate window. *imply type a valid 1asic
expression. "his can sometimes be dangerous, though, as it may change program operation
completely.
&rea:point Example.
*et a brea.point on the I H I ( - line in the sample program. =un the program.
8hen the program stops, display the immediate window and type the following line<
+rint I0?
"he values of these two variables will appear in the debug window. ?ou can use a
question mar. (]) as shorthand for the command +rint, if you3d li.e. =estart the
application. +rint the new variable values.
"ry other brea.points if you have time. 4nce done, all brea.points can be cleared by
'trl(*hift(X,E\ or by choosing 'lear All 1rea.points from the )ebug menu.
!ndividual brea.points can be toggled using X,E\ or the 1rea.point button on the
toolbar.
Viewing Variales in the Locals (indow.
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
:B
"he locals window shows the value of any variables within the scope of the current procedure.
As execution switches from procedure to procedure, the contents of this window changes to
reflect only the variables applicable to the current procedure. =epeat the above example and
notice the values of I and ? also appear in the locals window.
(atch Expressions.
"he Add 8atch option on the )ebug menu allows you to establish watch expressions for your
application. 8atch expressions can be variable values or logical expressions you want to view
or test. Values of watch expressions are displayed in the watch window.
!n brea. mode, you can use the Nuic. 8atch button on the toolbar to add watch expressions
you need. *imply put the cursor on the variable or expression you want to add to the watch list
and clic. the Nuic. 8atch button.
8atch expressions can be edited using the Edit 8atch option on the )ebug menu.
(atch Expression Example.
*et a brea.point at the I H I ( - line in the example.
*et a watch expression for the variable I. =un the application. Lotice I appears in the
watch window. Every time you restart the application, the value of I changes.
At some point in the debug procedure, add a quic. watch on ?. Lotice it is now in the
watch window.
'lear the brea.point. Add a watch on the expression< I H ?. *et 8atch "ype to ^1rea.
8hen Value !s "rue.3 =un the application. Lotice it goes into brea. mode and displays
the watch window whenever I H ?. )elete this last watch expression.
Call Stac:.
*electing the 'all *tac. button from the toolbar (or pressing 'trl(5 or selecting 'all *tac.
from the View menu) will display all active procedures, that is those that have not been exited.
'all *tac. helps you unravel situations with nested procedure calls to give you some idea of
where you are in the application.
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
:D
Call Stac: Example.
*et a brea.point on the ,cn H 'int() line in the general function procedure. =un the
application. !t will brea. at this line.
+ress the 'all *tac. button. !t will indicate you are currently in the ,cn procedure
which was called from the 'ommand-9'lic. procedure. 'lear the brea.point.
Single Stepping 5Step Into6.
8hile at a brea.point, you may execute your program one line at a time by pressing X,D\,
choosing the *tep !nto option in the )ebug menu, or by clic.ing the *tep !nto button on the
toolbar.
"his process is single stepping. !t allows you to watch how variables change (in the locals
window) or how your form changes, one step at a time.
?ou may step through several lines at a time by using =un "o 'ursor option. 8ith this option,
clic. on a line below your current point of execution. "hen press 'trl(X,D\ (or choose =un "o
'ursor in the )ebug menu). the program will run through every line up to the cursor location,
then stop;
Step into Example.
*et a brea.point on the )o line in the example. =un the application.
8hen the program brea.s, use the *tep !nto button to single step through the program.
At some point, put the cursor on the 5oop 8hile line. "ry the =un "o 'ursor option
(press 'trl(X,D\).
Procedure Stepping 5Step )ver6.
8hile single stepping your program, if you come to a procedure call you .now functions
properly, you can perform procedure stepping. "his simply executes the entire procedure at
once, rather than one step at a time.
"o move through a procedure in this manner, press *hift(X,D\, choose *tep 4ver from the
)ebug menu, or press the *tep 4ver button on the toolbar.
Step over Example.
=un the previous example. *ingle step through it a couple of times.
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
:E
4ne time through, when you are at the line calling the ,cn function, press the *tep over
button. Lotice how the program did not single step through the function as it did
previously.
"unction Exit 5Step )ut6.
8hile stepping through your program, if you wish to complete the execution of a function you
are in, without stepping through it linebyline, choose the *tep 4ut option. "he function will
be completed and you will be returned to the procedure accessing that function.
"o perform this step out, press 'trl(*hift(X,D\, choose *tep 4ut from the )ebug menu, or
press the *tep 4ut button on the toolbar. "ry this on the previous example.
CL+SSES )" D+$+ $/PES
-umeric data t!pes
"hese are those data types that will store numbers or numeric values. "hey are computed
mathematically.
!ntegers F store numbers.
1yte F true or false values between G and 6@@.
=eal (double) F store D bytes of numeric data.
'urrency F stores monetary values.
)ate and time F store and time format.
*ingle F store ; bytes of numeric data.
5ong F stores ; bytes of numeric data.
)ecimal F stores -6 bytes of numeric data.
Character data t!pes
"hey are data types that stores characters or strings of characters.
*tring F fixed length. *tores - to A@,;GG characters.
*tring F variable length. *tores G to 6 billion characters.
)ate F /anuary -,-GG to )ecember EEEE
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;G
1oolean F stores true or false values.
4b7ect F stores any embedded ob7ect.
Variant F numeric. Any value as large as double.
Variant F text. *ame as variable length.
&oolean data t!pes
*tores 1oolean data types. 1oolean is represented by either the .eyword true or false or non
Cero or Cero value. 1oolean are stored in two bytes and have no type declaration character.
User defined data t!pes
Visual basic lets the user create his own data type. "his is done when you wor. with mixed
data types.
Creating user defined data t!pes
8hen creating user defined data types use the .ey word _$!pe` statement in declaration
section of the form. )eclare variables associated with the new data types.
Example 0
Private type employee
#ame as strin"
Dateo$birt! as ate
Hireate as ate
Salary as currency
Gener as strin"
En type
$HE VISU+L &+SIC C)-$,)L S$,UC$U,ES
A control structure regulates the flow of a visual basic program, given a condition. )etermine
the output depending on the conditions used in the program. !t performs an indicated action
only when the condition is false or true.
$!pes of control structures
"here are three main control structures in visual basic<
Selection
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;-
"hey are those control structures that provides options choose from depending on the
conditions given for example !f, if#then else, switch, and select case.
Iteration
"hese are control structures that repeat statements until a given condition turns true of
false for example while#wend, do#until, do#loop while, for#next, do#loop until.
Se<uence
A control structure that executes statements in a program procedurally0 one after
another.
If7then selection control structure
"his is a control structure that is used to choose among alternative action to ta.e.
Example 0
)etermining the age of students in a class.
Interface
Pseudo code7+lgorithms
)eclare the variables i.e. age and message
!nitialiCe variable age
!f age is greater than or equal to -D then
)isplay _you are a grown up. 5earn to be responsible.`
End if
Code
+rivate sub cmddisplay9clic.()
)im age as integer
)im message as string
AgeHvalue(txtage.text)
!f txtage.text\H-D then
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;6
Enter the age
2essage
Displa! the age
"xtmessage.textH`?ou are a grown up. 5earn to be responsible`
End if
End sub
"lowchart
"he above flowchart represents if#then selection control structure. !t illustrates a single
selection control structure. !t performs the indicated action only if the condition is true
otherwise it is aborted.
If7then7else selection control structure
"he if#then#else control structure allows a programmer to specify that a different action can be
performed when the condition is true. !t tsts for true statements in a program.
Example 0
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;:
*tart
!f
age\H-D
)isplay the message
*top
A student sat for a )!" !! exam an d score the below mar.s0
*A)H BG
+!H AG
+!!H DG
2athsH ;G
,e<uired.
'reate a visual basic program that calculates the average mar.. %se the average mar. and
assign the student the grade according to the grading system below
=e! to grading s!stem.
A H DG to -GG
1 HBG to BE
' HAG to AE
) H@G to @E
E H;G to ;E
, HG to :E
Interface
Pseudo code7algorithms
)eclare variables
!nitialiCe the variable that we input
'alculate the average
!f average is greater than or equal to DG then
)isplay _A`
Else if average is greater than or equal to BG then
)isplay _1`
Else if average is greater than or equal to AG then
)isplay _'`
Else if average is greater than or equal to @G then
)isplay _)`
Else if average is greater than or equal to ;G then
)isplay _E`
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;;
*ad
+!
+!!
2aths
Average
$rade
'alculate
Else if average is less than ;G then
)isplay _,`
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;@
Code
+rivate sub cmdcalculate9clic. ()
)im *A), +!, +!!, maths as integer
)im average as single
)im grade as string
*A)Hval(txtsad.text)
+!Hval(txtpi.text)
+!!Hval(txtpii.text)
mathsHval(txtmaths.text)
txtaverage.textH val(txtsad.text)( val(txtpi.text)( val(txtpii.text)( val(txtmaths.text)#;
if txtaverage.text\HDG then
txtgrade.textH`A`
Else if txtaverage.text\HBG then
"xtgrade.textH`1`
Else if txtaverage.text\HAG then
"xtgrade.textH`'`
Else if txtaverage.text\H@G then
"xtgrade.textH`)`
Else if txtaverage.text\H;G then
"xtgrade.textH`E`
Else if txtaverage.textX;G then
"xtgrade.textH`,`
End if
End sub
Switch selection control structure
"his ia s control structure that is related to if#then#else control structure. Each argument passed
to switch control structure is either a condition or a value. !f a condition is true the value
associated with the condition is returned.
Example -
A student sat for a )!" !! exam an d score the below mar.s0
*A)H BG
+!H AG
+!!H DG
2athsH ;G
=equired<
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;A
'reate a visual basic program that calculates the average mar.. %se the average mar. and
assign the student the grade according to the grading system below
=e! to grading s!stem.
A H DG to -GG
1 HBG to BE
' HAG to AE
) H@G to @E
E H;G to ;E
, HG to :E
Interface
Code
+rivate sub cmdcalculate9clic. ()
)im *A), +!, +!!, maths as integer
)im average as single
)im grade as string
*A)Hval(txtsad.text)
+!Hval(txtpi.text)
+!!Hval(txtpii.text)
mathsHval(txtmaths.text)
txtaverage.textH val(txtsad.text)( val(txtpi.text)( val(txtpii.text)( val(txtmaths.text)#;
txtgrade.textHswitch(txtaverage.text\HDG,`A`
txtaverage.text\HBG,`1`
txtaverage.text\HAG,`'`
txtaverage.text\H@G,`)`
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;B
*ad
+!
+!!
2aths
Average
$rade
'alculate
txtaverage.text\H;G,`E`
txtaverage.textX;G,`E`)
end sub
VISUAL BASIC 6.0 TUTORIAL
PREPARED BY: KABA N. DANIEL
;D