Visual Basic Procedures
Visual Basic Procedures
Visual Basic offers different types of procedures to execute small sections of coding in applications.
The various procedures are elucidated in details in this section. Visual Basic programs can be broken
into smaller logical components called Procedures. Procedures are useful for condensing repeated
operations such as the frequently used calculations, text and control manipulation, etc. The benefits of
using procedures in programming are:
It is easier to debug a program, a program with procedures, which breaks a program into discrete
logical limits.
Procedures used in one program can act as building blocks for other programs with slight
modifications.
Sub Procedures
A sub procedure can be placed in standard, class and form modules. Each time the procedure is called,
the statements between Sub and End Sub are executed. The syntax for a sub procedure is as follows:
arglist is a list of argument names separated by commas. Each argument acts like a variable in the
procedure. There are two types of Sub Procedures namely general procedures and event procedures.
Event Procedures
An event procedure is a procedure block that contains the control's actual name, an underscore(_), and
the event name. The following syntax represents the event procedure for a Form_Load event.
General Procedures
A general procedure is declared when several event procedures perform the same actions. It is a good
programming practice to write common statements in a separate procedure (general procedure) and
then call them in the event procedure.
The Code window is opened for the module to which the procedure is to be added.
The Add Procedure option is chosen from the Tools menu, which opens an Add Procedure
dialog box as shown in the figure given below.
The name of the procedure is typed in the Name textbox
Under Type, Sub is selected to create a Sub procedure, Function to create a Function
procedure or Property to create a Property procedure.
Under Scope, Public is selected to create a procedure that can be invoked outside the module,
or Private to create a procedure that can be invoked only from within the module.
We can also create a new procedure in the current module by typing Sub ProcedureName, Function
ProcedureName, or Property ProcedureName in the Code window. A Function procedure returns a
value and a Sub Procedure does not return a value.
Function Procedures
Functions are like sub procedures, except they return a value to the calling procedure. They are
especially useful for taking one or more pieces of data, called arguments and performing some tasks
with them. Then the functions return a value that indicates the results of the tasks complete within the
function.
The following function procedure calculates the third side or hypotenuse of a right triangle, where A
and B are the other two sides. It takes two arguments A and B (of data type Double) and finally
returns the results.
The above function procedure is written in the general declarations section of the Code window. A
function can also be written by selecting the Add Procedure dialog box from the Tools menu and by
choosing the required scope and type.
Property Procedures
A property procedure is used to create and manipulate custom properties. It is used to create, read
only properties for Forms, Standard modules and Class modules.Visual Basic provides three kind of
property procedures-Property Let procedure that sets the value of a property, Property Get procedure
that returns the value of a property, and Property Set procedure that sets the references to an object.
CONTROL STRUCTURES
CONTROL STRUCTURES
If…then
If…then…else
Multiple If Statements
Select…case
LOOP STRUCTURES
While loop
Do while
Do until
For…next
Exit
If…then
Syntax:
If condition then
Statements
Else
Statements
End if
Example:
Msgbox (“Greater”)
If…then..Else
Syntax:
If condition then
Statements
Else
Statements
End if
Example:
Msgbox (“Greater”)
Endif
Multiple If Statements
If condition Then
………
………
Else ………..
End If
Example:
tSal = 2* Bsal
End If
Select…case
Syntax:
Case 0
Statements
Case 1
Statements
End select
Example:
AvgNum = total / n
………
End Select
Syntax:
While (condition)
Statement
Wend
Example:
Dim I as integer
I=0
While i<10
Wend
Do while loop statement (do while statement)
It is used to execute statements first and test the condition after each execution.
Syntax:
Do
Statement
Example:
Dim I as integer
I=0
While (i<10)
Do Until statement
Syntax:
Do while/until (condition)
Statement Loop
Example:
Dim I as integer
I=0
Do while/until (i<10)
Msgbox (i)
I=i+1
Loop
o Initialization
o Condition check
o Incrementation
Syntax:
Next
Example
Dim I as integer
For I = 0 to 10 step 2
Msgbox (i)
As the number of variables is increasing, complexity of the program also increases and
programmers get confused with the variable names.
There may be situations in which we need to work with a large number of similar data
values. To make this work more easily, by using a concept called "Array".
Array
An array is a variable which can store multiple values of the same data type at a time.
"Collection of similar data items stored in contiguous memory locations with single
name".
Syntax:
Example:
Types of arrays
2. Dynamic array
Fixed size array
Fixed arrays can be declared by giving a name to the array with the upper limit in the
parenthesis.
In the above declaration creates an array element with index numbers running from 0 to 8
Dim lengths (1 to 9 ) as integer
In the above statement an array of 9 elements is declared but indexes running from 1 to 10
Multidimensional array
For example, to hold the student registration number and marks of the student we need to
mention its x and y coordinates
Declaration of multidimensional array
The three dimensional array with defined lower limits are given below
Dynamic arrays
There will be a situation when the user may not know the exact size of the array at design
time
Under such circumstances, a dynamic array can be initially declared and the user can then
add elements when needed instead of declaring the size of the array at design time
You can declare the array as dynamic by giving it an empty dimension list Dim newarray
()
Use the Redim statement to allocate the actual number of elements Redim newarray
(y+1)
Redim is an executable statement and can appear only in a procedure It can be used for
fixed arrays is used for declaring ReDim statements
The lower and upper limits for each dimension can also be specified explicitly as in a fixed
size array Redim firstarray (4 to 12)
Each time an executing the ReDim statement, the current data stored in the array is lost
and the default value is set
But if we want to change the size of the array without losing the previous data, we have to
use the