You are on page 1of 24

Visual Basic

KG COLLEGE OF ARTS AND SCIENCE

DEPARTMENT OF INFORMATION TECHNOLOGY

LESSON NOTES

SUBJECT : VISUAL BASIC SEMESTER : V

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.

EVOLUTION OF COMPUTING ARCHITECTURES

Three system architectures

 Centralized System Architecture

 File Server System

 Client Server System

i)Centralized System Architecture

Companies that needed real computing power turned to the mainframe computer, Which is a
centralized system architecture. The most salient feature of a centralized system is that the only
movement of marshalling are the keystrokes between the client and the host machine.

Merits of Centralized System Architecture

 Excellent security

 Application logic and data reside on the same machine

Demerits:

 Expensive

 No way to partition application logic beyond the physical limitation

of the mainframe.
KG College of Arts and Science 1 B.Sc.,Information Technology
Visual Basic

Application Logic
Data

Network Server

Keystrokes

Terminal
Characters
Terminal 1 Terminal 2 Terminal 3 Terminal 4

ii)File Server Architecture

The file server system brought a complete change in the implementation of computer architecture. In
this system, the application logic was executed on the client workstation instead of the server.

Merits:

 Low cost

 Resources can be added or reduced as and when necessary

Demerits:

 Job of server is to store data

 Client machine need large amount of power

KG College of Arts and Science 2 B.Sc.,Information Technology


Visual Basic

Data

Network Server
Disk I/O requests

Disk Blocks

Workstation1 Workstation2 Workstation3 Workstation4

Application Application Application Application


Logic1 Logic2 Logic3 Logic4

iii)Client Server Model

Client server architecture is a process involving a minimum of two independent entities. One is the
client and the other is the server. The client makes a request to the server and the server services the
requests and the resulting data is sent to the client.

Example: SQL based Database Management System

The most popular client server applications revolve round the use of DBMS such as Oracle and Microsoft
SQL Server. These applications are referred to as back ends and offer support for storage, manipulation
and retrieval of business persistent data.

KG College of Arts and Science 3 B.Sc.,Information Technology


Visual Basic

File Server v/s Client Server DB Deployment

File Server Client Server

Data organization method for Architectural Model.


storing user files centrally within a
The business logic and database reside in the server.
department

Deals with file storage Runs the user interface.

Divide workload request among a no. of servers.

KG College of Arts and Science 4 B.Sc.,Information Technology


Visual Basic

Query Sent to a File Server System

The query issued is not sent to the server. The logic of the query is processed and evaluated at the client
end itself. Only file I/O request is sent across the network to retrieve the required data in the form of
disk blocks.

Query Sent to a Client Server System

The actual SQL statement is sent across the network and processed by an application running locally on
the server machine. As the SQL statement is processed on the server, only the results need to be sent
back to the client. Thus a major advantage of a client server is reduced network traffic and in most cases,
an incredibly quick execution time.

Two -Tier and Three-Tier Client Server Model

There is a need to an information system that can change with business needs m needs that mandate a
tighter budget and higher quality in addition to simply processing transactions and generating reports.

Two-tier Client Server Model

 Desktop Machine - Client (VB or PowerBuilder)

 N/W Server - Backend Db engine

 Business logic physically reside either on the client or be implemented on the back end.

 Merits :

Data access simplified

 Demerits:

Less Flexible

 Limitations:

Not Scalable

Unmanageable

Poor Performance

Three- Tier Client Server Model

KG College of Arts and Science 5 B.Sc.,Information Technology


Visual Basic

 Improved model.

 Based on the ability to build partitioned applications.

 Breaks the code into logical components.

 Each tier is called a service.

Service Location

User Services Client

Business Logic Server

Data Services Server

Values of Three Tier Architecture

 Reuse

 Performance

 Manageability

 Maintenance

DATA ACCESS OBJECTS

Options to access remote Client/Server databases

 DAO

 Data Control

 ODBC Direct

 Remote Data Objects(RDO)


KG College of Arts and Science 6 B.Sc.,Information Technology
Visual Basic

 Remote Data Control(RDC)

 Open Database Connectivity(ODBC)

 Visual Basic Library for SQL Server (VBSQL)

 Active Data Objects

Data Access Objects

A Data Access Object is collection of object classes that model the structure of a relational database
system.

Data Access Objects Hierarchy

KG College of Arts and Science 7 B.Sc.,Information Technology


Visual Basic

 DBEngine – Jet DB engine

 Workspace – Support transactions

 DB Object- Jet native or external DB or direct ODBC

 TableDef – Stored Table

 QueryDef –Stored Query

 Recordset – Cursor view

 Field Object –Column

 Index –Stored Index associated with TableDef

 Parameter- Associated with QueryDef

 User Object – To define & enforce DB security

 Group - Collection of users

 Relation Object – Relationship between fields

 Property Object – Characteristics

 Document Object – Information about instance

 Container – Holds information describing the objects

Accessing and Navigating Databases

 To work with data access objects, a reference has to be set to the

 Appropriate DAO libraries.

 2 Libraries

 Microsoft DAO 2.5/3.51 Compatibility Layer

 Microsoft DAO 3.51 Object Library

To set reference select,


KG College of Arts and Science 8 B.Sc.,Information Technology
Visual Basic

ProjectàReferencesàDAO 3.51 Object Library

Opening a Database

To open an existing database, the OpenDatabase method is used.

Syntax:

OpenDatabase (dbname,[options],[readonly],[connect])

Example:

Dim db as Database

Set db=OpenDatabase(“employee_details”)

Recordset

A Recordset is an object that contains a set of records from the database.

Types of Recordset Objects

 Table Type - Single Table, faster

 Dynaset - One or more tables

 Snapshot - Cannot be updated

 Dynamic - Query Result

 Forwardonly - Can only scroll forward through records

Creating a Recordset

The OpenRecordset method is used to open a Recordset and create

a Recordset variable.

Example:

Dim rs as Recordset

Set rs = db.OpenRecordset (“employee”,dbOpentable,dbReadOnly)

Navigating a Recordset:
KG College of Arts and Science 9 B.Sc.,Information Technology
Visual Basic

Move methods

 MoveFirst

 MoveNext

 MovePrevious

 MoveLast

Using BOF and EOF to Navigate through Recordsets

EOF

The EOF property is True when the user moves beyond the last record in the recordset.

BOF

The BOF pproperty is True when the user has moved to a position before the first record in the
recordset.

Modifying and Deleting Records:

Edit Method

Update MEthod

AddNew Method

Delete Method

Finding Records

The Find methods can be used to locate a record in a dynaset or snapshot type Recrdset.

FindFirst

FindLast

FindNext

FindPrevious

Performing Indexed Searches Using the Seek Method

The Seek method can be used to locate a record in a table type Recordset.

Manipulating stored queries using the QueryDef object

KG College of Arts and Science 10 B.Sc.,Information Technology


Visual Basic

The QueryDef object contains information about a stored SQL query.

QueryDef objects run faster than SQL queries.

Two basic methods for working with QueryDef:

 Execute method

 OpenRecordset method

The TableDef Object

TableDef is a collection of Table objects that contain detailed definition about each data table in the
database.

Methods used with the TableDef object

 OpenRecordset

 RefreshLink

 Create Property

 CreateIndex

 CreateField

Modifying and Deleting existing tables

New fields can be added or existing fields can be deleted using the Append or Delete method

Creating a Table in Oracle using SQL*Plus

Syntax:

CREATE TABLE <table_name>

(column_name1 datatype1,column_name2 datatype2….)

Inserting values in Table

Syntax:

INSERT into <table_name> VALUES <data_list>

KG College of Arts and Science 11 B.Sc.,Information Technology


Visual Basic

ODBC USING DATA ACCESS OBJECTS AND REMOTE DATA OBJECTS

ODBC

ODBC lets database client connect to external database

ODBC driver

Destination of Database - Login id , Password

3 parts

A Driver Manager

One of more drivers

One or more data sources

ODBC Driver
Client Application ODBC Data Source ODBC Driver
Manager

Creating ODBC Data Source

 Used when control is not used.

 Double Click ODBC configuration option (From Control Panel)

 From the list of ODBC sources select ‘ODBC driver for Oracle’

 Select Add button

KG College of Arts and Science 12 B.Sc.,Information Technology


Visual Basic

Steps to create an ODBC Data Source Name on the Client:

 Double click on the Oracle ODBC driver

 DSN configuration dialog box displays

 Give Data Source as ‘iiiB’ and Server as ‘cas’.

 Click OK

Using ODBC with DAO

OpenDatabase method - creates connection between the application and ODBC

Syntax:

Dim db as Database

Set db= OpenDatabase(“<DataSource Name>”, <dbdriverpromptinformation>,

<ReadOnly>,” ODBC ; UID=<User


ID>;PWD=<Password>”)

Accessing ODBC Databases using Data Control

Data Control – used to create simple and robust database applications

Tasks:

 Connect to a local or external databases

 Open a specified database

 Attach data fields to bound controls

 Add new methods or update a database

 Close the record

Using DBGrid Control

Spreadsheet like control

 Data Source Property

 Cell can hold text or pictures

 RowHeight

KG College of Arts and Science 13 B.Sc.,Information Technology


Visual Basic

 Arrow keys

 ColIndex Property

 Text & Value Properties

 Font , Border , Wordwrap, Color Attributes

Adding , Updating and Deleting Records

Private Sub Add_Click()

Data1.Recordset.AddNew

End Sub

Public Sub Update_Click()

Data1.UpdateRecord

End Sub

Public Sub Delete_Click()

Data1.Recordset.Delete

If Data1.Recordset.EOF Then

Data1.Recordset.MoveLast

End If

End Sub

Remote Data Objects

Object oriented way of accessing client server data sources

Projectà ReferencesàMicrosoft Remote Data Object 2.0

Click Ok

KG College of Arts and Science 14 B.Sc.,Information Technology


v
g
E
s
P
R
y
Q
m
lu
b
a
T
ir
t
c
e
n
C
o
d
1.Establishing a Connection

Set connection=Environment.OpenConnection(dsName

[,Prompt[,readonly[,connect[,options]]]])

2. Executing SQL statements

Set rs=rdoconn.OpenResultSet (name,type,locktype,option)

3.Using RDO to Insert, Update & Delete Records

Dim Env as rdoEnvironment

Dim cn as rdoConnection

Dim rs as rdoResultSet

KG College of Arts and Science 15


Visual Basic

B.Sc.,Information Technology
Visual Basic

ActiveX EXE and ActiveX DLL

Introduction

 Components of DLLs and EXEs offer functionality of objects without

redistribute or duplicate

 Class module provides services to client

 Servers can be implemented as ActiveX DLL or ActiveX EXE components

 ActiveX DLL - in-process servers

 Loaded in the same address space

 Faster, effective , become part of the application that uses them

 ActiveX EXE - out-of-process server (runs as separate process)

 Can service multiple clients

 More efficient

 Exchanging information between servers – Slow process

Creating an ActiveX EXE Component

Can be developed and run independently

3 Steps:

1. Create simple calculator(ActiveX EXE)

2. Compile this component and register

3. Test the component with client test application

Step 1:

StartàProgramsàVisual Basic6.0à New Project Dialogbox

select AcativeX EXE

Opens class module (class1)

KG College of Arts and Science 16 B.Sc.,Information Technology


Visual Basic

Property Settings

------------- -----------

Name Calculator

Instancing 5-Multiuse

Give code in General Declaration of class module,

Public Function Addn(A As Single,B As Single)As Double

Addn=A+B

End Function

Similar code can be written for Product, Subtraction, Division and Modulus operations

FileàSave Project As

ActiveXCalculator.vbp and the class module as calculator.cls

 Compilation & Registering the Calculator Component

 Run Calculator control by pressing F5

 Fileà Make EXE

Choose a folder to save the EXE & Click Ok.

 The ActiveX EXE control (ActiveXCalculator) is now compiled & registered

(Can be used in VB and also from any development environment that

knows about ActiveX components)

Testing the Calculator project with a Standard EXE application

Go to VB, open a new Standard EXE project & save it as

CalClient.vbp

KG College of Arts and Science 17 B.Sc.,Information Technology


Visual Basic

Form Name CalculatorForm


Caption ActiveXCalculator
Command1 Name cmdAddition
Caption Add
Command2 Name cmdAddition
Caption Add
Command3 Name cmdAddition
Caption Add
Command4 Name cmdAddition
Caption Add
Command5 Name cmdAddition
Caption Add
TextBox Name DisplayText
Text <blank>

 To include ActiveXCalculator project to the client application, select

 ProjectàReferences & select ActiveXCalculator

 Create instances of the class calculator in the client application & invoke the function.

KG College of Arts and Science 18 B.Sc.,Information Technology


Visual Basic

Dim CalcObj As New Calculator

Private Sub cmdAdd_Click()


Dim x As Single, y As Single
x=InputBox(“Enter first number”)
y=InputBox(“Enter second number”)
DisplayText.Text= “Sum of Given 2 nos” & Calcobj.Addn(x,y)
End Sub

 Add similar code to Sub, Prod, Div, Mod and run the Application.

Creating an ActiveXDLL Component

Steps:

 Determine the features

 Determine what objects are required

 Design a form

 Design the interface( properties, methods and events)

 Create the Project group(Component Project , test project)

 Implement the forms required by the component

 Implement the interface of each class

 Compile DLL and test it with all potential target applications

Benefits:

 DLL Components provide reusable code

 An application that uses component’s code - Client

KG College of Arts and Science 19 B.Sc.,Information Technology


Visual Basic

Can run either as in-process or out-of-process

1. Start VB & select an ActiveX DLL project

2. Set properties as follows

Name : InterestCalculator

Instancing : 5-Multiuse

3. Project à Add Module

From Add Module dialogbox, double click module icon and

name it as Interest.bas

4. Add code(in InterestCalculator)

Option Explicit

Public gdatserverstarted As Date

Sub Main()

gdatserverstarted = Now

Debug.print “Executing Sub Main”

End Sub

KG College of Arts and Science 20 B.Sc.,Information Technology


Visual Basic

KG College of Arts and Science 21 B.Sc.,Information Technology


Visual Basic

Option Explicit

Public gdatServerStarted As Date

Sub Main()

gdatServerStarted = Now

Debug.Print "Executing Sub Main"

End Sub

Public Function FixInterest(P As Single, T As Single) As Single

Dim R As Integer

R = 10

FixInterest = (P * R * T) / 100

End Function

Public Function RecInterest(P As Single, T As Single) As Single


KG College of Arts and Science 22 B.Sc.,Information Technology
Visual Basic

Dim R As Single

R=8

RecInterest = (P * R * T) / 100

End Function

Public Function CumInterest(P As Single, T As Single) As Single

Dim R As Single

R = 7.5

CumInterest = (P * R * T) / 100

End Function

KG College of Arts and Science 23 B.Sc.,Information Technology


Visual Basic

KG College of Arts and Science 24 B.Sc.,Information Technology

You might also like