0% found this document useful (0 votes)
91 views111 pages

Visual Basic Easy

Uploaded by

MANI KANDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views111 pages

Visual Basic Easy

Uploaded by

MANI KANDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

lOMoARcPSD|25992655

Visual Basic(III CS III BCA III IT-Sem5)

visual basic (Bharathiar University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

BHARATHIAR UNIVERSITY: COIMBATORE-641 046


CORE 9: VISUAL BASIC
Course: B.Sc. CS/IT/BCA

UNIT I:

Getting Started with VB6, Programming Environment, Working with Forms, Developing an application,
Variables, Data types and Modules, procedures and control structures, arrays. Working with Controls: Creating
and using controls, working with control arrays.

UNIT II:

Menus, Mouse events and Dialog boxes: Mouse events, Dialog boxes, MDI and Flexgrid: MDI, Using the
Flexgrid control.

UNIT III:

ODBC and Data Access Objects: Data Access Options, ODBC, Remote data objects, ActiveX EXE and
ActiveX DLL: Introduction, Creating an ActiveX EXE Component, Creating ActiveX DLL Component.

UNIT IV:

Object Linking and Embedding: OLE fundamentals, Using OLE Container Control, Using OLE Automation
objects, OLE Drag and Drop, File and File System Control: File System Controls, Accessing Files.

UNIT V:

Additional controls in VB: sstab control, setting properties at runtime, adding controls to tab, list control,

tabstrip control, MSFlexgrid control, Why ADO, Establishing a reference, Crystal and Data reports.

TEXT BOOKS:

1. Visual Basic 6.0 Programming, Content Development Group, TMH, 8th reprint, 2007. (Unit I to Unit IV)
2. Programming with Visual Basic 6.0, Mohammed Azam, Vikas Publishing House,
Fourth Reprint, 2006. (Unit V)

Prepared By
Dr.D.Uma Maheswari
Assistant Professor,
Department of Computer Science,
Government Arts & Science College,
Valparai -642 127.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Unit-1
• Getting Started with VB6
• Programming Environment
• Working with Forms
• Developing an application
• Variables, Data types and Modules
• Procedures and Control Structures
• Arrays
• Working with controls
• Creating and using controls
• working with control arrays
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

1.GETTING STARTED WITH VB6


What is Visual Basic?
•It is event driven programming language.
•VB is relatively easy to learn and use.
•Graphical User Interface(GUI).
•Creates professional applications for Microsoft
Windows.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

History of Visual Basic


• The Visual Basic was developed by
ALAN COOPER, an American scientist in the late
1970’s and sold to Microsoft.
It was developed from the programming language
BASIC (stands for Beginner's All-Purpose Symbolic
Instruction Code).

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

2.VISUAL BASIC 6.0


PROGRAMMING ENVIRONMENT
Pro

Click on start->Select Programs->Select Microsoft Visual studio 6.0->


Click on Microsoft Visual Basic 6.0 -> Click on open.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

The Integrated Development


Environment
The VB IDE is made up of a number of components
•Menu bar
•Toolbar
•Project Explorer
•Properties Window
•Form Layout Window
•Toolbox
•Form Designer
•Object Browser

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Menu Bar

Adds a new Form on the Project menu

Opens an existing Form on the Project menu

Saves the Current Project on the File menu

Displays the project Explorer on the View menu

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Toolbox

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Project Explorer

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Properties Window

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Object Browser

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Properties, Methods and Events


• Properties define the characteristics of an
object.Ex.Textbox-Font,Color,Enabled,Visible.
• A methods is an action that can be performed
on objects.Ex.Setfocus,Referesh.
• Event is an action.Ex.Click,Mousemove.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Working with Forms


• A form is a window that contains an
application code and has other objects placed
on it to create the user interface.
Setting Form Properties 4.Icon
1.Boarder Style 5.MousePointer
6.MaxButton
7.MDIChild
8.MinButton
9.Moveable
10.StartUpPosition

2.Caption

3.Control box 11.Windowstate

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

• Setting Form Properties


• 12.Displaying Forms
• 13.Show Method
• 14.Using the Hide Method
• 15.Using the Unload Statement
• 16.Using the Unload Event
• 17.Setting Run Time and Design Time Properties

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Developing an application
An application can be created with the project. Writing a Visual Basic program
involves the following two steps.
1.Visual Programming step
2.Code Programming step

Textbox

Command button
Command button

Command button

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Cont..
• Examining the project Window
• Changing the properties and adding controls
• Changing the Properties of form
• Adding Code for the program
• Creating an Executable File
• Ending an Application

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Code Window

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Variables, Data types and Modules


• Variables
Variables are used for storing values temporarily.
– A variable name must begin with an alphabet.
– It must be unique with the same scope.
– It should not exceed 255 characters.
– It should not contain any special characters like %,&,#,@ or $.
For e.g
Dim str as string
Dim I as integer
Different ways are declaring variables are
1.Explicit Declaration
2.Implicit Declaration
2.Using Option Explicit Statement
3.Scope of Variables
4.Local Variables
5.Static Variables
6.Module-level Variables
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Data types
• By default Visual Basic variables are of variant data types. The
variant data type can store numeric, date/time or string data.
• When a variable is declared, a data type is supplied for it that
determines the kind of data they can store.
• The fundamental data types in Visual Basic including variant
are integer, long, single, double, string, currency, byte and
boolean.
• Visual Basic supports a vast array of data types. Each data
type has limits to the kind of information and the minimum
and maximum values it can hold.
• In addition, some types can interchange with some other
types.
• A list of Visual Basic's simple data types are given below.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

1.Numeric

Byte Store integer values in the range of 0 - 255

Integer Store integer values in the range of (-32,768) - (+ 32,767)

Long Store integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468)

Single Store floating point value in the range of (-3.4x10-38) - (+ 3.4x1038)

Double Store large floating value which exceeding the single data type value

Currency store monetary values. It supports 4 digits to the right of decimal point and 15 digits to the left

2. String
Use to store alphanumeric values. A variable length string can store approximately
4 billion characters.
3. Date
Use to store date and time values. A variable declared as date type can store both
date and time values and it can store date values 01/01/0100 up to 12/31/9999.
4. Boolean
Boolean data types hold either a true or false value. These are not stored as
numeric values and cannot be used as such. Values are internally stored as -1
(True) and 0 (False) and any non-zero value is considered as true.
5. Variant
Stores any type of data and is the default Visual Basic data type. In Visual Basic
if we declare a variable without any data type by default the data type is assigned as default.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Modules
• A key part of developing applications using
Visual Basic is ensuring that the code is
carefully structured. This involves segmenting
the code
into projects, modules and procedures so that
it is easy to understand and maintain.
• A complete Visual Basic application is typically
contained in a single project. Within a project,
code is placed in separate code files
called modules, and within each module, the
Visual Basic code is further separated into self
contained and re-usable procedures.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Procedures
• Visual Basic offers different types of procedures to execute
small sections of coding in applications.
• 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.
• A Procedure can be Sub, Function or Property Procedure.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

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:
[Private | Public] [Static] Sub Procedurename [( arglist)]
[ statements]
End Sub
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.

Figure: Procedure

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

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.
Private Sub Form_Load()
....statement block..
End Sub
Event Procedures acquire the declarations as Private by default.
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.
In order to add General 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. Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

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 returns 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.
Function Hypotenuse (A As Double, B As Double) As Double
Hypotenuse = sqr (A^2 + B^2)
End Function
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.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Control structures
• Control Statements are used to control the
flow of program's execution. Visual Basic
supports control structures such as if... Then,
if...Then ...Else, Select...Case, and Loop
structures such as Do While...Loop,
While...Wend, For...Next etc method.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

• If...Then selection structure


• The If...Then selection structure performs an
indicated action only when the condition is
True; otherwise the action is skipped.
• Syntax of the If...Then selection
• If <condition> Then
statement
End If
• e.g.: If average>75 Then
txtGrade.Text = "A"
End If

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

If...Then...Else selection structure


The If...Then...Else selection structure allows the programmer to specify that a different action is to be
performed when the condition is True than when the condition is False.
Syntax of the If...Then...Else selection
If <condition > Then
statements
Else
statements
End If
e.g.: If average>50 Then
txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If
Nested If...Then...Else selection structure
Nested If...Then...Else selection structures test for multiple cases by placing If...Then...Else selection
structures inside If...Then...Else structures.
Syntax of the Nested If...Then...Else selection structure
You can use Nested If either of the methods as shown above
Method 1
If < condition 1 > Then
statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Select...Case selection structure


Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a single block of
statements from among multiple block of statements. Select...case is more convenient to use than
the If...Else...End If. The following program block illustrate the working of Select...Case.
Syntax of the Select...Case selection structure
Select Case Index
Case 0
Statements
Case 1
Statements
End Select

e.g.: Assume you have to find the grade using select...case and display in the text box
Dim average as Integer
average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text ="A"
Case 74 To 65
txtGrade.Text ="B"
Case 64 To 55
txtGrade.Text ="C"
Case 54 To 45
txtGrade.Text ="S"
Case 44 To 0
txtGrade.Text ="F"
Case Else
MsgBox "Invalid average marks"
End Select

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Arrays
An array is a consecutive group of memory locations that all have the same
name and the same type. To refer to a particular location or element in the
array, we specify the array name and the array element position number.

Declaring arrays
Arrays occupy space in memory. The programmer specifies the array type
and the number of elements required by the array so that the compiler may
reserve the appropriate amount of memory. Arrays may be declared as
Public (in a code module), module or local. Module arrays are declared in
the general declarations using keyword Dim or Private. Local arrays are
declared in a procedure using Dim or Static. Array must be declared
explicitly with keyword "As".
There are two types of arrays in Visual Basic namely:

Fixed-size array : The size of array always remains the same-size doesn't
change during the program execution.
Dynamic array : The size of the array can be changed at the run time- size
changes during the program execution.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Fixed-sized Arrays
• When an upper bound is specified in the declaration, a Fixed-array is
created. The upper limit should always be within the range of long data
type.
• Declaring a fixed-array
• Dim numbers(5) As Integer
• In the above illustration, numbers is the name of the array, and the
number 6 included in the parentheses is the upper limit of the array. The
above declaration creates an array with 6 elements, with index numbers
running from 0 to 5.
Multidimensional Arrays
• Multidimensional arrays can have more than two dimensions. Visual Basic
supports at least 60 array dimensions, but most people will need to use
more than two or three dimensional-arrays.
• Dim Details( 101 To 200, 1 To 100, 1 To 100).
Static and dynamic arrays
• Static arrays must include a fixed number of items, and this number must
be known at compile time so that the compiler can set aside the
necessary amount of memory. You create a static array using a Dim
statement with a constant argument:
• ' This is a static array.
Dim Names(100) As String
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Working with Controls


• Creating and using Controls
Before writing an event procedure for the control to response to
an event, you have to set certain properties for the control to
determine its appearance and how will it work with the event
procedure. You can set the properties of the controls in the
properties window or at runtime.
Visual Basic are classified into
1.standard controls
2.ActiveX controls and insertable objects
Standard controls :
Standard controls such as Command Button,Lable and Frame
control.
ActiveX controls and insertable objects:
ActiveX exist as separate files with either .VBX or .OCX extension.
Some of these objects support OLE automation

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Setting properties to control


Notes:
a) Each tool has a property window .To see this window: Click on tool on form>
Property window appears.
b) Property can be changed manually or by code and the effect of code appears in the
run time (when user runs project).
c) To put code for tool action:
Double click on tool > code sheet of the Form appears (with code of corresponding
tool is written) > User write the desired code inside tool event, or outside in Form
event.
Working With Tools
The user can work with tool in the design stage.
- To add tool: double click on tool. Tool appears on form or drags it to design
part of page and draw it in the desired size.
- To delete: click on element in page> press delete key of the key board or right
click on object for mouse list> choose delete.
- To display tool properties window: click on element> properties window
appear.
- To display code form: doubleDownloaded
clickbyon tool ([email protected])
MANI KANDAN code form for that element.
lOMoARcPSD|25992655

Using Text Box Control


The text box is the standard control for accepting input from the user as well as to
display the output. It can handle string (text) and numeric data but not images or
pictures.
Example
In this program, two text boxes are inserted into the form together with a few labels.
The two text boxes are used to accept inputs from the user and one of the labels
will be used to display the sum of two numbers that are entered into the two text
boxes. Besides, a command button is also programmed to calculate the sum of the
two numbers using the plus operator. The program use creates a variable sum to
accept the summation of values from text box 1 and text box 2.The procedure to
calculate and to display the output on the label is shown below.
Private Sub Command1_Click()
'To add the values in TextBox1 and TextBox2
Sum = Val(Text1.Text) +Val(Text2.Text)
'To display the answer on label 1 Label1.Caption = Sum
End Sub

Figure:sample form design


for Text box
A TextBox control's contents and detect changes the user makes to
he TextBox through several properties.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Using Label Control:


It is used to display fixed text on form. he label is a very
useful control for Visual Basic, as it is not only used to provide
instructions and guides to the users, it can also be used to display
outputs. One of its most important properties is Caption. Using
the syntax Label.Caption, it can display text and numeric data .

Figure:sample form design


for Label Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Using a Command Button


The command button is one of the most important controls as
it is used to execute commands.
It displays an illusion that the button is pressed when the user
click on it.
The most common event associated with the command button
is the Click event, and the syntax for the procedure is
Private Sub Command1_Click ()
Statements
End Sub

Figure:sample form design for


command
Downloaded by MANI KANDAN ([email protected])
Using Option Button
lOMoARcPSD|25992655

Option button: Used only as a group of buttons. When the user selects one of
them the others are deselected automatically. All other properties of this
control are similar to those in form and command button where they are
fully discussed which are caption, font, enabled, back color and visible
beside an important property which is value that takes true or false and it
used with if statement. The option
button usually takes click event.
Example: Design a form with three option buttons " red ", " green “ and "
blue "
such that when we click on options the color of the form colored by red, green
and blue respectively.
Private Sub Option1_Click()
Form1.BackColor = vbGreen
End Sub
Private Sub Option2_Click()
Form1.BackColor = vbBlue
End Sub
Private Sub Option3_Click()
Form1.BackColor = vbRed Figure:sample form design for option button
End Sub Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Using List box and Combo box Controls


The user can't write directly in ListBox . They can add
item to the ListBox property or by code in the form .
.

Add items to list:


a) Change property list from properties window.
b) When click on arrow, write items (elements)

Figure:sample form design


for list box and combo box

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Using the ComboBox


A ComboBox combines the features of TextBox and a list Box. Threee types of
Combo Box styles are.
1.Dropdown combo(Style 0)
2. Simple combo(Style 1)
3. 1.Dropdown list(Style 2)

Using the ScrollBar Control Figure:sample form design for


Combo box
Windows Forms scrollbar controls are used to provide easy navigation through a long list of
items or a large amount of information by scrolling either horizontally or vertically within an
application or control.

Figure:sample form design


for ScrollBar

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Working with control arrays


• A control array is a group of controls that share the
same name and type.They also share the same event
procedures.
• Creating Control arrays at Design Time
Three ways to create it.
1.Assigning the same name in the name property for
more than one control.
2.Copying an existing control and then pasting it onto
the form.
3.Setting the Index property to a vale that is not null.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Unit-2
• Mouse events
• Dialog boxes
• MDI
• Flexgrid control

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Mouse events
Visual Basic responds to various mouse events, which are recognized by most of the
controls. The main events are MouseDown, MouseUp and MouseMove.
MouseDown occurs when the user presses any mouse button and MouseUp occurs
when the user releases any mouse button.
These events use the arguments button, Shift, X, Y and they contain information about
the mouse's condition when the button is clicked.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

The following code is entered in the Form_MouseDown( ) event


Private Sub Form_MouseDown(Button As Integer, Shift As Integer,
X As Single, Y As Single)
If optCredit = True Then
imgCredit.Move X, Y
Else
imgCash.Move X, Y
End If
End Sub
Run the application by keying in F5. You can notice that when the mouse is clicked on the form
somewhere, the selected image moves to that clicked location. This is shown in the below figure.

Figure:sample form design


for Mouse Events

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Dialog boxes
Dialog Boxes are used to display information and to prompt the user about
the data needed to continue an application.
Three ways of adding dialog boxes.
1. Predefined Dialog Boxes- Creating using InputBox() and MsgBox() function.
2.Custom Dialog Boxes –Created by adding controls to the form or by
customizing an existing dialog box.
3.Standard Dialog Boxes –Created using Common Dialog Control.
Predefined Dialog Boxes:
It can be added easily to an application. This uses the Input Box and MsgBox
functions.
Displays a prompt in a dialog box, waits for the user to input text or click a button,
and returns a String containing the contents of the text box.

Figure:sample form
design for Input box

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Following code is entered in cmdOK_Click (


) event
Private Sub cmdok_Click()
Dim ans As String
ans = InputBox("Enter something to be displayed
in the label", "Testing", 0)
If ans = "" Then
lbl2.Caption = "No message"
Else
lbl2.Caption = ans
End If
End Sub

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Message BOX
Displays a message in a dialog box and wait for the user to click a button, and
returns an integer indicating which button the user clicked.

Figure:Msgbox Dialog box

Downloaded by MANI KANDAN ([email protected])


Custom Dialog Control
lOMoARcPSD|25992655

• A Custom Dialog Box is a Form that is created


containing controls including command
button, option Button and textbox controls
that supplies information to the application.

Figure: Menus
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Standard Dialog Boxes


• The common dialog Control is a control that
displays the commonly used dialog boxes such
as Save as,Colour,Font,Print,File Open.
• Components is selected from the Project
menu which displays a Components box.
• After ensuring the Microsoft Common
Dialog6.0 Control Check Box has a check mark
in it, the OK button is clicked.

Figure:Dialog Box Control

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

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, you can maintain
multiple open windows, but not multiple copies of the application. Data exchange is easier
when you can view and compare many documents simultaneously.

Creating an MDI Application

A new Standard EXE project is opened .An MDI form is inserted by selecting Add MDI Form
from the Project Menu.Otherwise add from property window.

Downloaded by MANI KANDAN ([email protected]) Figure: MDI form


lOMoARcPSD|25992655

Fig: Menu Editor

Fig:MDI Form
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Fig: Parent form and Child Form

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Flexgrid control
A MS Flex Grid Control in VB is used to create applications that is used
to create an application that present information in rows and columns.
To add the Flex grid control ,Choose Components from the project menu
click on the Microsoft Flexgfrid Control 6.0.

Fig:MS Flex Grid

Flex Grid Control in VB support properties such as rows,collumns,


RowHeight and ColWidht etc.,
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Unit-3
ODBC and Data Access Objects:
• Data Access Options
• ODBC
• Remote data objects
ActiveX EXE and ActiveX DLL
• Introduction
• Creating an ActiveX EXE Component
• Creating ActiveX DLL Component.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Data Access Options


• VB provides a variety of options to access
remote client\server databases. They are.
• Data Access Objects(DAO)
• DATA Control
• ODBC Direct
• Remote Data Objects(RDO)
• Remote Data Control(RDC)
• Open Database Connectivity(ODBC)
• Visual Basic Library for SQL Server(VBSQL)
• Active Data Objects(ADO)

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Data Access Objects(DAO)


• It is a collection of object classes that model
the structure of a relational database system.
They provide properties and methods that
allow the accomplishment of all the
operations to manage such a system, including
features for creating databases, defining
tables, navigating and querying a database.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Opening a Database
• Database is a collection of data that is related one to another to support a
common application. For example Employee details - Name, Address, etc.
Each of these collections of data continue a database.
• database accessing methods are as follows;
• Jet Engine - Accessing Microsoft Access and Visual Basic databases.
• ODBC (Open Database Connectivity) - Allow access to the client server
databases on a network.
• ISAM (Index Sequential Access Method) - Used to access flat databases
such as dBase, FoxPro, ParaDox.
• Syntax:
openDatabse(dbname,[options],[readonly],[connect])

Dim db as Database
Set db=OpenDatabase(“employee Details”)

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Recordset
• A Recordset is an object that contains a set of
records from the database.
• There are five major types of Recordset
objects.
• 1.Table Type Recordset
• 2.Dynaset Type Recordset
• 3.Snapshot Type Recordset
• 4.Dynamic Type Recordset
• 5.Forward only Type Recordset

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Creating a Recordset
Syntax:
Dim rs as Recordset
set rs= db.OpenRecordset(“employee”,dbOpentable,dbReadonly)
Navigating a Recordset
-MoveFirst-Moves to the first row in the Recordset
-MoveNext-Moves to the next row in the Recordset
-MovePrevious-Moves to the previous row in the Recordset
-MoveLast-Moves to the last row in the Recordset
The Recordset object provides two properties to the user to know
when he has moved to the beginning (BOF)or the (end of the recordset
(EOF).
Modifing and Deleting Records
To manipulate a record in a recordset,the following methods are used.
Edit method
AddNew Method
Delete Method
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Finding Records
Find method can be used to locate a record .Four find methods are.
FindFirst
FindLast
FindNext
FindPrevious
Performing Indexed Searches Using the Seek Method
This method performs an indexed search for the first occurrence of the
record that matches the indexed criteria.
Manipulating stored queries Using the QueryDef object
The queryDef object contains information about a stored SQL query.
There are two basic methods for working QueryDefs.They are.
Execute Method and
OpenRecordset method
Creating Parameterized Queries using the Parameter object
Parameter query can be created using the Parameters Collection of a
QueryDef object.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

TableDefDataObject
TableDef is a collection of Table objects that contain detailed definition
about each data table in the database. There are five methods can be
used with Table Def object.
 OpenRecordset
 RefereshLink
 CreateProperty
 CreateIndex
 CreateField
Modifying and Deleting Existing Tables
New fields can be added or existing fields can be deleted using the Append
or Delete methods.
Creating aTable in Oracle using SQL*Plus
A table created in SQL*Plus using the Following syntax.
CREATE TABLE <table_name>
(Column_name1 datatype, Column_name2 datatype,..)
Inserting Values in a Table
The INSERT command is used to add rows to a table.
Syntax
INSERT into <table-name. VALUES<data_list>
The data entered in the table should match the order of the columns asd they appear in
the table.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

ODBC
• OPEN DATABSE CONNECTIVITY(ODBC)- is a
windows technology that lets a database client
application connect to a remote databse.
• The ODBC has three parts. They are
A driver manager
one or more drivers
one or more data sources
Creating an ODBC Data Source
Before any application can access an ODBC
databse,the ODBC drivers must be installed and
a Data Source Name(DSN) crated using the
control panel.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Steps for creating an ODBC Data Source Name on the client


• 1.Double click on the Oracle ODBC driver.
• 2.Enter the Data Source Name as XYZ company in
the Data Source Name box.
• 3.Click the Next button to continue and display
the screen that allows us to choose more options.
• 4.Click the select button that allows us to choose
appropriate translators.
• 5.To see the list of ODBC drivers installed in the
system,the drivers tab must be clicked.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Using ODBC with DAO

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

HOW TO CONNECT DATA ACCESS OBJECT(DAO)


CLICK MICROSOFT VISUAL BASIC 6.0

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Click on Standard exe and then open

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Vb opened

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Fig:Form Design of DAO

Form Design
Property Window
Set
Auto size =true
Label1.caption=name
Label2.caption=phone
button1.caption=EXIT

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

HOW TO CREATE ACCESS DATABASE

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

OUTPUT

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

REMOTE DATA OBJECTS


Using Remote Data Objects (RDO) is an object
oriented way of accessing client server data
sources. In order to get started with Remote Data
Objects, a reference has to be made to them in VB
project. The steps to be followed.
1.Selct References from VB project men. The
References dialog appears.
2.Select Microsoft Remote Data Objects 2.0 from
the list.
3.click OK.Remote Data objects will be available
now in the application.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Remote Data Objects Hierarchy


rdoEngine

rdoEnvironment

rdoConnection

rdo Table rdo Query rdoResultSet

rdoColumn rdoColumn rdoColumn

rdoparameter

Fig :The Complete hierarchy of Remote Data Objects


Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Establishing Connection:To establish a connection to a database in oracle, the


following syntax is used.
Syntax
Set connection=environment.OpenConnection(dsName[,prompt[,readonly[,,connect
[,options]]]])
Executing SQL statements: After a connection has been established the user can
execute queries on the database.
Syntax:
Set rs=rdoconn.OpenResultSet(name,type,locktype,option)
Using RDO to Insert, Update and Delete Records.
Records can be inserted, existing records can be modified and unwanted records can
be deleted using RDO objects.
Creating Parameterized Queries Using rdoParameter Object
Parameterized queries can be written using the rdoparameter object. Each
rdoParameter object belongs to a rdoparameters collection.
Accessing Tables with the rdoTable Object
It contains information about every column in the database table that exist in the
remote data source.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

ActiveX EXE and ActiveX DLL


Introduction to ActiveX EXE and ActiveX DL
• VB can be used to compile class based projects as
ActiveX components.
• A class module, which we have already
studied in server,i.e an application that
provides its services to the client application.
• Servers can be implemented as ActiveX DLL
or ActiveX EXE components. The difference
between the two lies in how the server is
executed .

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Creating an ActiveX EXE Component


• An ActiveX EXE is an out-of-process server, which

can be developed and run independently.


• Compiling and registering the same and then

testing it using a client application


Creating ActiveX DLL Component
• An ActiveX DLL is an in-process server.An ActiveX

EXE ,oterhwise knoewn as an out-of-process


server, as run as a separte process.
• The application is the client ,which sends requests

and the ActiveX component is the server, which


services the request.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

• ActiveX project is compiled the same way a Standard


EXE project is compiled.
• ActiveX components are Object servers, designed to be
used with other applications.
• There are three major steps involved in developing an
ActiveXExe.
1.To create the ActiveX EXE component.
2.To compile this component and register.
3.To test the component with our client test application.
Components provide reusable code in the form of objects.
An application that uses a components code, by creating
objects and calling their properties and methods is
referred to as a client.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

UNIT-4
Object Linking and Embedding
OLE fundamentals
Using OLE Container Control
Using OLE Automation objects
OLE Drag and Drop
File and File System Control
File System Controls
Accessing Files
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Object Linking and Embedding (OLE)


OLE is a technology that enables A Windows programmer to create an application
that can display data from many different applications and enables the user to edit
that data from within the application in which it was created.

Object Linking and Embedding (OLE) Fundamentals:


The fundamentals for understanding the methodology to sue OLE in VB.
Objects and Classes
OLE automation
Container Application
Linked Objects
Embedded Objects
Using OLE Container Control:
The OLE container control allows adding objects from other applications.
1.Creating Objects at Design Time
2.To Create an Embedded Object at Design Time
3. Creating Object Using Paste Special Dialog Box
4. Creating Objects at Run Time
Using OLE Automation objects
Referencing an object Using an object Library
Events Related to OLE Drag and Drop
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

File and File System Control


File System Controls
There are three file system controls in VB.They are
DriveListBox,DirectoryListBox and FileListBox.
-The DriveListBox control is a specialized drop-down list that
displays a list of all the valid drives on the user's
system. Dir1.Path = Drive1.Drive
-The DirListBox control displays a hierarchical list of the user's
disk directories and subdirectories and automatically reacts to
mouse clicks to allow the user to navigate among
them. File1.Path = Dir1.Path
-The FileListBox control lists files in the directory specified by its
Path property

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Accessing Files
A file consists of a series of related bytes located on a disk.There are three ways
of accessing files. They are
1.Random Access
2.Sequential access
3.Binary access
-Random files are record-based files with an internal structure that supports
"direct access" by record number. This means that your program
can read from or write to a specific record in a random access file, say the
50th record, without reading through the previous 49 records.

-In sequential-access file, you can write data to the file or read data from
it sequentially from the beginning of the file to the end and vice versa.
-For sequential files, this is the number of characters buffered. This is ignored, if
mode is Binary. If the file is not existing then a new file with the given name is
created in Append, Binary, Output and Random modes.

-Binary access Files, all files are "binary" in that they are just a collection of
bytes stored in an operating system construct called a file. However, when we
talk about binary files, we are really referring to the way VB opens and
processes the file. Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

There are Three statement of Random Access Files :


Open Statement
Get Statement
Put Statement
The Open Statement for Random Access Files

1) If you only want to read from the random access file, use:

Open filename For Random Access Read As #filenumber Len = reclength

2) and if you only want to write to the random access file, use:

Open filename For Random Access Write As #filenumber Len = reclength

3) and if you want to both read from and write to the random access file (for example,
you want to access a
particular record and then update one or more of its fields), use:

Open filename For Random Access Read Write As #filenumber Len


= reclength

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

The Get Statement

The Get statement is used read data from a file opened in random
mode. The syntax, as it applies to random files is:

Get [#]filenumber, [recnumber], varname

The Put Statement

The Put statement is used write data to a file opened in random mode.
The syntax, as it applies to binary files is:

Put [#]filenumber, [recnumber], varname

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Unit-5
Additional controls in VB:
sstab control
setting properties at runtime
adding controls to tab
tabstrip control
MSFlexgrid control
Why ADO
Establishing a reference
Crystal and Data reports.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

sstab control
• The SSTab control provides an easy way of
presenting several dialogs or screens of
information on a single form using the same
interface seen in many commercial Microsoft
Windows applications.

• The SSTab control provides a group of tabs, each


of which acts as a container for other controls.
Only one tab is active in the control at a time,
displaying the controls it contains to the user while
hiding the controls in the other tabs.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Setting Tabs and Tab Rows


• Before setting the number of tabs you want to add
to the SSTab control, you should first decide what
your dialog box will contain and how it will be
organized.

• While you can set the numbers of tabs at both


design time and run time, you will more than likely
find that creating your tabbed dialog at design
time is much quicker and easier. You can set
properties at design time using the SSTab Property
Pages which is available by right-clicking the
control and then selecting Properties.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

setting properties at runtime


• Properties can be set at design time by using
the Properties window or at run time by using
statements in the program code.
Control Properties
• Object is the name of the object you're
customizing.
• Property is the characteristic you want to
change.
• Value is the new property setting.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

adding controls to tab


• Each tab in the SSTab control is essentially a container
for other controls. When you create a tabbed dialog
box, you group controls together that perform similar
functions, such as printing a document or setting
display options for your application. Once you’ve
determined how many tabs you need in your tabbed
dialog box, you add whichever controls you need to
perform those functions.
• To add controls to individual tab pages at design time,
first select the tab by clicking on it, then draw the
controls on the tab page.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

tabstrip control
• The TabStrip control is part of the Microsoft Windows
Common Controls (the same group that includes the
ListView, TreeView, ToolBar, ImageList, and others). The
TabStrip is NOT a "container" control like the SSTab.
• A TabStrip is a control that contains a collection of one
or more tabs.
• Each Tab of a TabStrip is a separate object that users
can select. Visually, a TabStrip also includes a client
area that all the tabs in the TabStrip share.
• By default, a TabStrip includes two pages, called Tab1
and Tab2. Each of these is a Tab object, and together
they represent the Tabs collection of the TabStrip. If
you add more pages, they become part of the
same Tabs collection.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

MSFlexgrid control
• The MSFlexGrid control provides all the functionality
for building spreadsheet applications, just as the
RichTextBox control provides all the functionality for
building word processing applications
• Go to the Project menu, pick Components.
• Scroll down the list of components until you find
"Microsoft FlexGrid Control 6.0 ". Select that checkbox
and hit the OK button to add the control to your
project.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Why ADO
ActiveX Data Objects (ADO) is designed to be an easy-to-use application-level interface
to any OLE DB data provider, including relational and non-relational databases, e-
mail and file systems, text and graphics, and custom business objects, as well as
existing ODBC data sources.
ADO is easy to use, language-independent, implemented with a small footprint, uses
minimal network traffic, and has few layers between the client application and the
data source — all to provide lightweight, high-performance data access.
The general characteristics of ADO are:
Ease of use.
High performance.
Programmatic control of cursors.
Complex cursor types, including batch and server- and client-side cursors.
Ability to return multiple result sets from a single query.
Synchronous, asynchronous, or event-driven query execution.
Reusable, property-changeable objects.
Advanced recordset cache management.
Flexibility — it works with existing database technologies and all OLE DB providers.
Excellent error trapping. Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Establishing a reference
• When you add an object as a reference, you can only
use the type library provided by the control, or the
"raw" type library. In contrast, adding a control as a
component also exposes the Visual Basic extender
properties and methods as if they were part of the
control.
• On the Project menu, click References.
• Click to select the check box next to the component
you want to add. If the component is not listed, locate
the . dll or . ocx file using Browse.
• Click OK.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Crystal and Data reports

• Start Visual Basic as a Standard EXE project. From


the Project menu in the VBE, select Add Data Report in
the dropdown menu.
• The data report environment contains six controls, they
are RptTextBox, RptLine, RptFunction, RptLabel,
RptImage and RptShape.
• You can customize your report here by adding a title to
the page header using the report label RptLabel.
Simply drag and draw the RptLabel control on the data
report designer window and use the Caption property
to change the text that should be displayed.
• You can also add graphics to the report using the
RptImage control.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Step 2: Connecting the report to database using Data Environment


Designer
Click the Project menu, then select Data Environment. from the drop-
down menu. The default data environment will appear

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

• Now, to connect to the database, right-


click connection1 and select Microsoft Jet
3.51 OLE DB Provider (as we are using MS
Access database) from the Data Link
Properties dialog.

Now, you need to connect to the database by selecting a database file


from your hard disk. For demonstration purpose, we will use the
database BIBLIO.MDB that comes with Visual Basic.

Downloaded by MANI KANDAN ([email protected])


• The path to this database file is C:\Program
lOMoARcPSD|25992655

Files\Microsoft Visual Studio\VB98\BIBLIO.MDB.


• This path varies from computers to computers,
depending on where you install the file. After selecting
the file, you need to test the connection by clicking
the Test Connection button at the right bottom of the
Data Link Properties dialog.
• If the connection is successful, a message that says 'Test
Connection Succeeded' will appear.
• Click the OK button on the message box to return to the
data environment.
• Now you can rename connection1 to any name you like
by right-clicking it. For example, you can change it to
MyConnection.
• You may also change the name of DataEnvironment1 to
MyDataEnvironment using the Properties window.
Downloaded by MANI KANDAN ([email protected])
lOMoARcPSD|25992655

Step 3: Retrieving Information from the Database


In order to use the database in your report, you
need to create query to retrieve the information
from the database. Here , we will use SQL
command to create the query. First of all, right
click on MyConnection to add a command to the
data environment. The default command is
Command1, you can rename it as MyCommand,

In order to use SQL command, right-click


MyCommand and you can see its properties
dialog. At the General tab, select SQL
statement and key in the following SQL
statement:
SELECT Au_ID, Author
FROM Authors ORDER BY Author
This command is to select all the fields from
the Authors table in
the Biblio.Mdb database. The command
ORDER BY Author is to arrange the list in
ascending order according to the Authors'
Names.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

To add data to your report, you need to drag the fields from MyCommand in
MyDataEnvironment into MyDataReport. You can customize the look of the labels as well as the
TextBoxes from the properties window of MyDataReport.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

The Final step is to set MydataReport as the Startup form from the Project menu,
then run the program.

Downloaded by MANI KANDAN ([email protected])


lOMoARcPSD|25992655

Downloaded by MANI KANDAN ([email protected])

You might also like