Академический Документы
Профессиональный Документы
Культура Документы
Q1: What is Visual Basic, and what are the major components
of visual basic IDE.
Visual Basic
Visual basic is a Microsoft tool, which is used to create windows
based applications.
For example Notepad, Calculator, Window media player, Internet
Explorer etc
Visual basic is both a tool and a language. The tool is the Visual Basic
Integrated Development Environment. It provides facilities for writing,
debugging and running programs in one environment. It is used to
develop GUI (Graphical User Interface) of the program. The language is
used to write the code that execute behind the GUI of the program.
2) Properties Window
The characteristics of any thing are called property. In
Visual Basic each control has a unique set of properties. Some
properties are common to all the controls such as Height, width, name,
caption etc, while other properties are unique to each control. When a
control is selected, its respective properties are displayed in the
properties window.
4) Toolbox
Toolbox contains controls that are used on the form
window to develop an application. Toolbox contains most of the tools
are usually used in application programs. However new controls can
also be added.
3) Enable
It is a Boolean property that is either True or False. This
property specifies the functionality of object on the form during run
time.
4) Visible
This property specifies that whether an object is to be seen
on the form or not. This property can be either true or false.
5) Left and Top
These properties specify the distance of the object from
the upper left corner of the window. It is measured in twips. One
twip is equal to 1/20th of the point. In one inch there are 72 points.
So 1 inch = 1440 twips.
6) Height and Width
These properties specify the size of the object. It is
measured in twips.
7) BackColor
This property is used to identify the background color of
the object. The color setting is defined in hexadecimal code. Since it
is difficult to remember the code, therefore VB IDE provides the
color palette to select a color.
8) ForeColor
This property is used to specify the foreground color of the
object. The foreground color is the color of the text that is displayed
on the object.
9) Appearance
This property is used to specify the look of the object on
the computer screen. It has two pre-defined values.
0- Flat Look (Specifies Flat Look)
1-3D Look (Specifies 3D Look)
10) Font
The format, style, type of the text is specified in this property.
the form then user can click, double click, enter text in the text box. All
these events are associated with the specified object that is text box.
(i) Events Triggered By Mouse
The most common events that are triggered with mouse are
1) Click, Db Click
The click event takes place when the user presses the left
mouse button. The double click event takes place when the user
presses the left mouse button twice in quick succession.
1) KeyDown
This event occurs when the key is pressed on the keyboard.
2) KeyUp
The keyup event occurs when a key is released.
3) KeyPress
Keypress event take place when the key is pressed. Keypress
differs from keydown because keypress cannot detect Shift, Alt, Ctrl
keys.
Q3: Discuss Form in VB; also discuss its properties, methods and
events.
Form
Form is the most important object of GUI. The GUI application is
created on this object through placing different controls on it and result
can also be displayed on this form.
11) Font
The type, style, size of the text that appears on the form is
specified in this property.
6) Resize Event
Resize event occur when the size of the form is changed.
Command Button
Command button is the most important control of GUI. It is
used to execute an action, when it is clicked.
Label Control
A label box control is used to display information for
various controls on the form.
Properties
1) AutoSize This property can be either True or False
If True, the label is resized to fit the text specified in the caption
property.
If False, the size of the label remains fixed as defined at design time.
Events
1) Click This event is triggered when the user clicks on the
label
2) DbClick This event is triggered when the user double clicks
on the label
Timer Control
The timer control is used to generate an event at regular
interval. The event generated is called the timer event.
The timer event is used to move graphics or to change the color at
regular at regular interval. Application program also used this event to
save document files at regular interval.
Properties
1) (1) Interval Interval property specifies the interval between each
timer event. It is measured in milliseconds. Thus 1000 intervals are
equal to 1 second.
2) Enabled This property switches the timer On or OFF
Text Box
The text box control is used to get and display information on the
form. The information in the text box can be entered at design time.
The user can enter the text at run time through the code. It is the most
commonly used control of the window user interface.
Properties
1) MaxLength
MaxLength property specifies the maximum number of character
to be displayed in the text box. The 0 value indicate unlimited length.
2) Multiline
Multiline property specifies whether the textbox displays single
line or multi line. The True value specifies multi line and False value
specifies single line.
3) PasswordChar
It specifies the character that is displayed in place of characters
that are entered in the text box. Usually a * is used to get password
from the user in this property.
4) ScrollBars
It specfies the display of scrollbars. This property works in
conjunction with multi line property. The scrollbars are displayed when
the MultiLine property is set to True
5) SelLength
This property returns the length of the selected text in the text
box. It is used during run time only.
6) SelStart
It returns the starting position of the selected text in the text
box. It is also used during run time only.
7) SelText
It returns the selected text in the text box. It is also used during
run time only.
8) Text
It set or returns the text in the text box.
9) Locked
If this property is set to True then the text in the text box
becomes read only
Events
1) Change
This event occurs every time the Text Property in the
text box changes.
2) KeyPress
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 10
Methods
1) SetFocus
This method places the cursor in the specified text box.
CheckBox
Check boxes are used when one or more options are
selected from a list of given options. An option is selected by clicking
on the check box. When an option is selected, a tick mark appears in
the text box, which indicates that the option is selected.
When an already selected checkbox is again clicked, then the tick
mark disappears which is also called deselecting.
Properties
1) Caption
It specifies the text that appears next to the checkbox
2) Value
It indicates the state of check box. Three states are possible,
unchecked return 0, checked return 1 and grayed out state return 2.
Events
1) Click
This event occurs when the check box is clicked. When the
checkbox is clicked, then its value property is automatically changed
by the visual basic.
Option Button
Option Buttons are also called radio buttons. Option
buttons are used when only one option is to be selected from a list of
given options. These options are such that if one option is selected,
other option cannot be selected; these options are therefore mutually
exclusive.
Properties
(1) Caption
It specifies the text that appears next to the option button.
(2) Value
It indicates the state of option button. If the button is selected
then it has True value otherwise False value.
Events
(1) Click
This event occurs when the option button is clicked. When the
option button is clicked, then its value property is automatically
changed by the visual basic.
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 11
Q7: Discuss List Box and Combo box control with their
properties, methods and events.
Properties
1) List
This property adds a list of items at design time in the list box.
The following procedure is adopted to add items in the list box at
design time.
• Select the List property and click the arrow at the right and an empty
box will appear.
• Type the items that is to be added and press Ctrl+Enter to type the
next item.
• To complete the entry and to close the list property box
press Enter key and the entered items will be added to the
list box.
2) MultiSelect
This property specifies how items may be selected. Any one of
three values may be selected.
0- No Multiple selection allowed
1- Multiple selection allowed
2- Group selection allowed
3) Sorted
This property is used to display the items sorted in
ascending order if the property is set to True otherwise items appear
in the order in which they are inserted.
4) Style
This property is used to specify the manner in which the items
are displayed and selected in the list box.
The list items can be displayed either as normal text or a check box
is placed before each item.
(5) List Count
This property returns the total number of items in a list. It can be
used during run time only.
(6) List Index
This property specifies or returns the index value of the most
recently selected item in the list box. If no item is selected then the
ListIndex = -1 will be returned. It is also used during run time only.
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 12
(7) Text
This property is used to return the text of the most recently
selected item. It can be used during run time only.
(8) Index
This property returns the index value of the item most recently
added item in the list box. It can be used during run time only.
Events
Change This event occurs whenever the drive selection is changed.
Events
Change This event occurs whenever the directory selection is
changed.
Q9: Discuss Scroll bar and frame controls with their properties,
methods and events.
Frames
Frames are used for grouping related controls on the form.
Since a frame is used to contain other control on the form, therefore it
is also called container control.
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 15
Frame Property
(1) Caption It specifies the title of the frame that is displayed at its top.
Message Box
The Message Box is used to provide information to the user. It
is usually used to display error messages or to provide information to
the user.
Syntax
Msgbox “Message”,VbButtons + Icon, “Title”
Example
Msgbox “Are You Sure You Want To Exit”,VbYesNo +
VbInformation,”Exit Application”
Syntax
Variable = InputBox (“Prompt”, Title, “Default” )
Example
Var = InputBox ("Enter Number", "Enter Values", "Here Type
Numeric Values")
Q11: What is Error, what are the types of Error and what are
Error Handling Techniques.
Error or Bug
Whenever a problem, error occur in a system, or when
application does not produce correct or required result or when
application halts unexpectedly or when application converts into
infinite loop is called bug
Types of Error
There are three main types of errors.
1) Syntax Error
Error in the syntax of the program is called syntax error. For
example
Far I= 1 To 10
In the above example the word “Far” in used instead of word “For”.
3) Logical Error
Error in the logic of the program is called logical error. For
example instead of using the “*” operator programmer use “+”
operator, which causes an incorrect result.
Error Handling
Error handling is a process to recover and remove errors
from a program. In VB two statements are used for error handling.
These are
1) On Error Goto Statement
2) On Error Resume Next
__________
Label:
Error Handling Code
Example
Private Sub Command1_Click ( )
On Error Goto abc
Dim a, b, c as integer
a = 10
b=0
c = a/b
Print c
Print “Above is Output”
abc:
Print” Division by Zero Error”
End Sub
Output
Division by Zero Error
Methods
The commonly use methods are:
1) Navigate Method
It is used to display the Web page into the Web Browser control.
Syntax
Object.Navigate URL
2) GOBack Method
The WebBrowser control and the Internet Object automatically
maintain the list of visited URLs. This list is called the history. This
method is used to display one previous item in the history list of the
URLs that have already have been displayed.
Syntax
Object.Goback
3) GoForward Method
This method is used to display the next page in the history list of
URLs that have already been accessed. It works opposite to the
GoBack method. This method is available only if the GoBack Method
has been used to display a previous page
Syntax
Obect.GoForward
4) GoHome Method
5) Refresh Method
6) Stop Method
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 19
WebBrowser Properties
The commonly used properties of the WebBrowser control are:
1) Busy Property
2) LocationName Property
This property is used to return the title of the web page that is
displayed on the WebBrowser control.
3) LocationURL Property
This property is used to return the URL address of the Web Page
that is displayed on the WebBrowser control.
WebBrowser Events:
The commonly used events of the WebBrowser are
1) DownloadBegin Event
2) DownloadComplete Event:
3) ProgressChange Event
4) TitleChange Event
This event occurs when the title of the current document or page
changes. When a
Page begins to download; The WebBrowser also returns its title.
After the title of the new document is displayed, the title change
event is triggered .This event is normally used to change the
caption of the form
Private Sub WebBrowser1_TitleChange (ByVal text as string)
End sub
Internet Explorer
The Visual Basic also has Internet Explorer Object that supports
the same methods, properties and events as the WebBrowser Control.
But it also has some extra methods, properties, and events. The
instance or variable of Internet Explorer type is created in programs
codes and its methods and properties are used to build the Browser
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 21
Declaration of Variables
Specifying the name and data type is known as the declaring of
variable.
When a variable is declared, a certain number of bytes in the
memory are allocated to the variable name. The amount of memory is
allocated according to the type of data that is to be stored in it.
There are three ways of declaring a variable in VB.
(i) Implicit Declaration
(ii) Explicit Declaration
(iii) Default Declaration
Scope Of Variables
The scope of variable specifies the range in the
applications within which the value of the variable can be accessed
and used. There are different levels of scope in VB. These are
(i) Procedure Level
These are also called local variables. The variables defined
within a procedure are procedure level variables. They can be used in
that procedure and cannot be used in any other procedure.
These are declared using the Dim Statement. For example
Dim xyz As Integer
Constant
A quantity that cannot be change during the execution of
program is called constant.
Types of Constants in VB
(i) User Defined Constant
A variable name can also be assigned a value in such a manner
that its value does not change in the program. It behaves as a constant
value and is called user defined constant. The keyword const is used
to define the user defined constant.
For example
Const PI = 3.14159
them. These are called symbolic constant and each represent a unique
value. All symbolic constant in VB start with vb.
For example
Form1. BackColor = &HFF0000
So using VB symbolic constant
Form1. BackColor = vbBlue
Assignment Statement
The assignment statement is used to assign a value to a
variable is called assignment statement. The assignment operator “=”
is used to assign a value to a variable.
Syntax
Variable-name = Expression
Example
a = 10 + 20
< A<B
If A is less than B, value returned by the expression
will be TRUE, otherwise it will be FALSE
>= A>=B
If A is greater than or equal to B, value returned by
the expression will be TRUE, otherwise it will be
FALSE
<= A<=B
If A is less than or equal to B, value returned by the
expression will be TRUE, otherwise it will be FALSE
= A=B
If A is equal to B, value returned by the expression
will be TRUE, otherwise it will be FALSE
<> A<>B
If A is not equal to B, value returned by the
expression will be TRUE, otherwise it will be FALSE
Or Exp1 Or Exp2
OR The output will be True if, both Exp1 and Exp2 are true. Or either
Exp1 or Exp2 is true
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 27
Fix (Number)
For Example
Int (11.2) will returns 11
Fix (11.2) will returns 11
Int (-11.2) will returns 12
Fix (-11.2) will returns 11
(3) CInt Function
This function is used to convert the value to an integer by
rounding off the value. For example
CInt (11.67)will returns 12
String Functions
(1) Val & CVal Functions
These functions are used to convert a string consisting of
numeric digits into numeric value. Its syntax is
Val (String)
CVal (String)
For Example
Val (“1122”) or CVal (“1122”) will returns 1122
Example
BirthDay = #05/04/1984#
Print “Day = “; Day (BirthDay)
Print “Month = “; Month (BirthDay)
Print “Year = “; Year (BirthDay)
1) If.......Then Structure
Syntax
If (Condition) Then
Statement(s)
End If
Example
If (Marks >= 33) then
Print “ You are Pass”
End If
2) If.......Then.......Else Structure
In this structure one condition and two statements or two blocks
of statements are given. When the condition is true, the first statement
is executed and the second statement is ignored. When the condition
is false, the first statement is ignored and the second statement is
executed.
Syntax
If (condition)
Statement(s)
Else
Statement(s)
End if
Example
Private Sub Command1_click ()
Dim marks as integer
Marks = Inputbox(“Enter Your marks”)
If (marks is >=33) then
Print “You are Pass”
Else
Print “You are Fail”
End if
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 32
End sub
Example
Private Sub Command1_click ()
Dim marks as integer
Marks = InputBox (“Marks Obtained”)
Select Case marks
Case 0 to 32
MsgBox “Fail”
Case 33 to 49
MsgBox “Third Division”
Case 50 to 59
MsgBox “Second Division”
Case 60 to 100
MsgBox “First Division”
Case Else
MsgBox “Not valid marks percentage”
End Select
End Sub
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 34
Loop Structure
A structure in which a set of statements are executed repeatedly
are called loop structure. In VB there are different types of loop
structure
1) For loop:
This loop repeats a set of statements for a specified number of
times. It is also called counter loop.
Syntax
For Initial value To Final value
VB Statement(s)
Next [Variable]
Example
Private Sub Command1_click ( )
Dim n as integer
For n = 1 to 10
Print n
Next n
End sub
Example
5) Do……Loop While:
The Do......Loop While is similar to the Do......While Loop except
that, in this syntax, the condition is tested at the end of the loop
structure.
Syntax
Do
VB Statement(s)
Loop While (Condition)
Example
Private Sub Command1_click ()
Dim n as integer
n=1
Do
Print n
n = n+1
Loop While (n <= 10)
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 36
End Sub
Do……..Loop Until:
This loop structure is also similar to the Do......Until Loop except
that the condition is tested at the end of the loop structure. The loop
statements are executed at the as long as the given condition remains
false.
Syntax
Do
VB Statement(s)
Loop until (Condition)
Example
Private Sub Command1_click ()
Dim n as integer
n=1
Do
Print n
n = n+1
Loop until (n > 10)
End Sub
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 37
Q19: what is array and what are the types of array in Visual
Basic.
Array
Array is the collection of finite homogeneous type of data, which
are stored in successive memory locations.
The memory locations are identifies by a name, which is called the
array name. The data items of the array are called the elements of
array and each data item is stored in separate memory location.
Each memory location within the array is identifies by its position
value, which is called the index of the array.
110 112 114 116 118 120 Memory Location
22 11 13 52 21 05
x[1] x[2] x[3] x[4] x[5] x[6] Element
Index
Types of array
There are two types of array.
1) One dimensional array
2) Two Dimensional array
One Dimensional Array (Linear Array)
One-dimensional array represent the data in the form of vector, list
or linear form. It is used to store similar type of data consisting of only
one row or column.
110 112 114 116 118 120 Memory Location
22 11 13 52 21 05
x[1] x[2] x[3] x[4] x[5] x[6] Element
Index
Declaration of One Dimensional Array
The method of informing the computer of the name, data type
and total number of elements of an array is called declaring of array.
Its syntax is
Dim Array-Name (n) As DataType
For example
Dim Month (11) As String
Example
Private Sub Command1_Click( )
Dim A (4) As Integer
Dim I As Integer
For I = 0 To 4
A (I) = Inputbox (“Enter value in 1-D array”)
Next I
Print (“Element of the 1-D array are”)
For I = 0 To 4
Print A (I)
Next I
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 38
End Sub
Control Array
A group of control that share the common the same name, type
and event procedure is called control array. Since all control in the
control array have the same name, they call the same procedure and
are distinguish from each other by their index property.
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 39
Q20: What are Procedure and Functions, how they are called.
Also describe argument passing mechanism with examples.
Procedure
Group of statements that cannot return a value is called
procedure. It is also called Sub Procedure or Subroutine. Its syntax is
Specifier Sub ProcName ( Argument Seperated by Commas)
- --
- --
End Sub
Where
Specifier specifies the scope of procedure. It can be private or
public.
Sub The Keyword Sub is used to specify Sub Procedure.
ProcName It specifies the name of procedure. The procedure is
called by its name.
2. Pass by Reference
Pass by reference is called pass by address. In this the memory
address is passed to the procedure. If the value of the variable is
changed in the procedure, the value of the variable is also changes in
the calling procedure.
Example (Calling Procedure with Arguments)
Pass By Value Pass By Reference
Private Sub Command1_Click( ) Private Sub Command1_Click( )
Dim a, b As Integer Dim a, b As Integer
a=4 a=4
b=5 b=5
Call Sum (a, b) Call Sum (a, b)
Print a Print a
Print b Print b
End Sub End Sub
Public Sub Sum (ByVal I As Integer, Public Sub Sum (ByRef I As Integer,
ByVal J As Integer) ByRef J As Integer)
Dim c as Integer Dim c as Integer
c=i+j c=i+j
Print c Print c
I = 10 I = 10
J = 20 J = 20
End Sub End Sub
Function
A function is an independent procedure that is written to perform
a specific task. It is a non-event procedure. It is executed by another
procedure and it returns a single value to the procedure that executes
it.
Syntax
Specifier Funtion Fname (Arguments) As Type
- ---
- ---
Fname = Expression
End Function
Example
Private Sub Command1_Click( )
Dim A as integer
A = Sum (4, 5)
End Sub
End Function
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 42
1. Data Control
Data control is a primary interface between VB application
and a database. It provides direct access to database without writing
any code. Setting the properties and methods of this control accesses
the records form database tables.
2. Data Access Object (DAO)
DAO is a collection of objects, which are used for managing
and manipulating databases. The DAO collection contains several
objects. The properties and methods associated with these objects are
used to perform database management operation.
3. ActiveX Data Object (ADO)
It is a latest data object technology. It used the best
features of old DAO technology. The ADO is specially designed for
client-server applications. It is also used for database management on
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore, Pakistan 43
RecordSet Object
The recordset object consists of a collection of
records. In database programming, a recordset is equivalent to a
variable that is used to store records.
Methods of RecordSet
End If
End Sub
Else
Text1.Text = Adodc1.Recordset.Fields("rno")
Text2.Text = Adodc1.Recordset.Fields("name")
End If
End Sub