Visual Basic Notes-1
Visual Basic Notes-1
Visual Basic Notes-1
Control Structures
Control Structures allow us to regulate the flow of our program’s execution. Using
control Structures we can write Visual Basic code that makes decisions or that
repeats actions. Other Control Structures let us guarantee disposal of a resource or
run a series of statements on the same object reference.
Decision Controls
IF (Condition) THEN
Condition
Statement False
ENDIF True
Statement to be
executed
STOP
Essentially if the expression evaluates to true then the code in the body of the
statement is executed. If, on the other hand, the expression evaluates to false the
code is skipped.
Visual Basic
Page 1
IF…..THEN…….ELSE: In IF….THEN…..ELSE if the expression evaluates to true then
Statement associated with IF is executed. IF the expression evaluates to false then
the statement associated with ELSE part gets executed. So in this type of structure if
the condition is true then a specific task is performed and if the condition is false
then other task is performed.
START
The syntax is as:
IF (condition) THEN
Statement
Condition
ELSE
Statement Statement to be
Statement to be
executed
ENDIF executed
Stop
Visual Basic
Page 2
Looping Structure: A loop is a special type of statement that is used to execute
a statement repeatedly. For example if we have to print Hello 10 times , the first way
is to write hello statement 10 times. Another way is to use a loop and to write print
statement only once. The loop will run 10 times and will print the message 10 times.
Visual Basic
Page 3
The syntax is as:
FOR Counter= Start To End { increment / Decrement}
Statement(s)
Next
e.g. For a=1 To 10
Print a
Next
Function: A function is a special type of procedure that returns a value every time
it is called. Visual Basic provides a rich collection of inbuilt functions. A user can also
define a function according to need those functions are called userdefined functions.
The main advantages of a function are reusability and modularity. Duplication of
code is reduced by using functions. We can call the function in two ways- call by
value and call by reference. In Call by value method a copy of the variable is passed
and original values are not modified. In call by reference a variable a variable itself
is passed and the original value is modified.
User Defined Functions: A function that does not exist but is created by a user to
perform some task is called user defined functions. A function must return a value. A
user can define a function either parameterized or without parameter.
Library Functions: Library functions are also called system functions or inbuilt
functions. These are the functions that have been defined by a programming
language. We can only use them but can’t change their definition. A library function
can be used many times in a program. Some functions are:
String Functions
Space(number) : it is a library function related to string which is used to put ‘n’
number of spaces where ‘n’ is a value of argument being passed. The number is an
argument to be passed to this function. It should be a positive number.
Chr ( ): The chr function returns the string that corresponds to an ASCII.
e.g. chr (65) will return capital A because ASCII value of A is 65
Visual Basic
Page 4
Str(number): This string related function is used to convert a numeric value into
string value. This function is important when we need to perform mathematical
operations.
Right(String, length): This string function is used to extract the right portion of a
phrase. The numbers of characters to be returned are mentioned as well.
Syntax: Right(Phrase,n)
Where n is the starting position from the right of the phrase where the portion of
the phrase is going to be extracted. E,g
Right(Visualbasic,4) = asic
Left (string, Length): This string related function is used to extract the leftmost
characters from a string. The numbers of characters to be returned are mentioned
as well. Syntax:
Left(phrase,n)
Where n is the starting position from the left of the phrase where the portion of the
phrase is going to be extracted. E,g
Left(Visualbasic,4) = Visu
Mid (string, Start, length): The Mid function extracts a substring from the
original phrase or string. It takes the following format:
Mid(Phrase,position,n)
Where position is the starting position of the phrase from which the extraction
process will start and n is the number of characters to be extracted. Example
Mid(“VisualBasic”,3,6) = ualBas
Visual Basic
Page 5
Len(string): The length function returns an integer value which is the length of a
phrase or a sentence including the empty spaces. The syntax is :
Len (phrase)
Example:
Len(visualbasic)
It will return 11.
Ltrim(string): The Ltrim function trims the empty spaces of the left portion of the
phrase. Syntax
Ltrim(Phrase)
Example
Ltrim (“ VisualBasic”,4)=VisualBasic
Rtrim(string): The Rtrim function trims the empty spaces of the right portion of
the phrase. Syntax:
Rtrim (Phrase)
Example:
Rtrim (“ VisualBasic “ )= VisualBasic
Trim(string): This function is used to remove the spaces on both sides of the
string.
Ucase(string): This string function is used to convert a given string from
lowercase to uppercase.
e.g. Dim a as string
a=Ucase(apple)
now a = APPLE
Visual Basic
Page 6
Time Functions
Now( ): this is a time related function which is used to return the current systems
date and time . syntax:
Now( )
Time( ):this is a time related function which is used to return only current systems
time. Syntax: Time( )
Minute ( ):This is a time related function which is used to return current systems
minutes. Thus the value returned is always between 1 and 60.
Month ( ): this function is used to return the current month of the system.
Number Functions
Sgn(number): it is a numeric function which returns sign of a number passed to it.
If the number passed is a positive number the function returns 1, if the number
passed is a negative number the function returns -1. Syntax:
Sgn(number)
Val(string): This is a numeric related function which is used to convert a string
into a number.
Int( ): This is a numerical related function which is used to return the integer part
of a number. If the number is negative the function returns a number less than or
equal to the number.
Example:
Int(2.9) It will return 2
Int(-8.4) it will return -9
Abs( ): This is a numeric function which is used to return the absolute value of
numeric expression .i.e a number without sign(+ or -) e.g Abs(-8) =8
Visual Basic
Page 7
Fix( ):this is a numeric related function which is used to return the integer part of a
number. Even though if the number is negative the function returns a number
greater than or equal to a number. Int(-8.4) . it will return -8
Sqr ( ): This function is used to return the square root of a number passed to it.
Example:
Sqr(9)
It will return 3
Round (n, m ): Round is the function that rounds up a number to a certain number
of decimal places. The format is round(n, m) which means to round a number n to m
decimal places. E.g. round(7.2567,2) = 7.26
MsgBox( )
This function is used to display a message in a dialog box. It waits for a user to click
a button and returns a value indicating which button the user clicked. The returned
value is stored in a variable for further use. Syntax:
Msgbox(prompt[,buttons ][,title][,helpfile, contect])
Prompt: A string expression displayed as the message in the dialog box. The
maximum length is about 1024 characters
Buttons: used to specify the number and type of buttons to display.
Title: the string displayed on the title bar of the message box
Help file: a string expression that identifies the help to be used for the dialog box.
Context: numeric expression that identifies the help file number.
Visual Basic
Page 8
Input box function
This function is is used to collect information from the user and returns a string
containing the contents of the text box which can be collected in a variable or some
other container. Syntax:
Inputbox(prompt[,title[,default[,xpos[,ypos][,help file,context])
Prompt: A string expression displayed as the message in the dialog box.
Title: String expression displayed in the title bar of the dialog box.
Default: a string expression displayed in the in the text box as the default response
if no other input is provided.
Xpos: the distance in pixels of the left edge of the dialog box from the left edgeof the
screen.
Ypos: The distance in pixels of the upper edge of the dialog box from the top of the
screen.
Help file: a string expression that identifies the help to be used for the dialog box.
Context: numeric expression that identifies the help file number.
Form: A form is an object which acts as a container for controls. It can hold one or
more controls like buttons, check boxes etc. A form is the basic work area in Visual
Basics which acts as a background for other objects placed on it to form user
interface. It acts as a visual interface for an application.
Visual Basic
Page 9
Types of Forms:
1. Single Document Interface
2. Multiple Document Interface
Single Document Interface: A single document Interface or SDI is an application
primarily made of a form equipped with a menu. SDI is stand alone ordinary
window or form that exists independently of each other. Notepad is the example of
Single document interface. SDI application allow only one open document frame
window at a time. It is made up of one or more independent windows which
appears separately on the window desktop. Each window of the application holds a
single document. If the user wants to open more documents with that application
then he must open a new window. SDI applications tend to be more robust and bug
free than MDI applications.
Multiple Document Interface: The Multiple Document interface (MDI) was
designed to simplify the exchange of information among documents all under the same
roof. With the main application we can maintain multiple open windows but not the
multiple copies of application. Data exchange is easier when we can view and compare
many documents simultaneously. The main form or MDI form is not duplicated but it
acts as a container for all the windows and it is called parent window. The windows in
which the individual documents are displayed are called child windows. MDI application
must have at least two forms, the parent form and one or more child forms. Each of these
forms has certain properties. There can be many child forms contained within the parent
form but there can be only one parent form. To create an MDI application, we must
follow these steps:
1. Start a new project and then choose Project >>> Add MDI Form to add the parent
Form.
4. Make this Form as child of MDI Form by setting the MDI Child property of the SDI
Form to True. Set the caption property to MDI Child window.
Visual Basic automatically associates this new Form with the parent Form. This child
Form can't exist outside the parent Form, it can only be opened within the parent Form.
Visual Basic
Page 10