You are on page 1of 11

VB PRACTICAL FILE

01190301912
ADO Connectivity


Prior to VB6 and the introduction of ADO (ActiveX Data Objects), VB programmers would
generally use DAO (Data Access Objects) to interact with local databases such as MS Access
and use RDO (Remote Data Objects) to interact with client/server databases such as Oracle and
SQL Server. The concept behind Visual Basic ADO was Universal Data Access (UDA), where
one database access method could be used for any data source; it was designed to replace both
DAO and RDO. DAO remains a viable technology for interacting with MS Access databases as
it is faster than ADO for that purpose; however, ADO is more flexible using ADO, one could
develop a prototype database application using MS Access in the back-end, and with a "flick of
the wrist" (i.e., with very little coding changes) "upsize" that same application to use Oracle or
SQL Server. As far as RDO is concerned, no new versions of it have been developed beyond the
version that shipped with Visual Basic, and there are no future plans for it.

Follow the steps below to set up an ODBC Data Source (this process is also called "setting up
a DSN", where "DSN" stands for "Data Source Name"). These steps assume Windows 2000
for the operating system. On other versions of Windows, some steps may vary slightly.
Via Windows Control Panel, double-click on Administrative Tools, then Data
Sources (ODBC). The ODBC Data Source Administrator screen is displayed, as shown
below. Click on the System DSN tab.

VB PRACTICAL FILE
01190301912
Click the Add button. The Create New Data Source dialog box will appear. Select
Microsoft Access Driver (*.mdb) from the list and click the Finish button.


The ODBC Microsoft Access Setup dialog box will appear. For Data Source Name,
type Biblio. If desired, you can type an entry for Description, but this is not required.

VB PRACTICAL FILE
01190301912
Click the Select button. The Select Database dialog box appears. On a default
installation of VB6 or Visual Studio 6, the BIBLIO.MDB sample database should reside in
the folder C:\Program Files\Microsoft Visual Studio\VB98. Navigate to that folder, select
BIBLIO.MDB from the file list, and click OK.

Note: If VB was installed in a different location on your system, navigate to the appropriate
folder. If you do not have the BIBLIO.MDB sample database file on your system at all,
you can download it here. In that case, copy the file to the folder of your choice, and
navigate to that folder to select the database for this step.
When you are returned to the ODBC Microsoft Access Setup screen, the database you
selected should be reflected as shown below. Click OK to dismiss this screen.


VB PRACTICAL FILE
01190301912
When you are returned to the ODBC Data Source Administrator screen, the new DSN
should appear as shown below. Click OK to dismiss this screen.



At this point, the Biblio database is ready to be used with ADO in the sample application.









VB PRACTICAL FILE
01190301912
Application 1: Using the ADO Data Control (ADODC)
To build the first sample application, follow the steps below.

Start a new VB project, and from the Components dialog box (invoked from the Project
-> Components menu), select Microsoft ADO Data Control 6.0 (SPx) as shown below and
click OK.








VB PRACTICAL FILE
01190301912
The ADO Data Control should appear in your toolbox as shown below:

Put an ADO Data Control on your form, and set the properties as follows:
Bring up the Properties window for your ADO Data Control and select the Connection Property-
-this is the key to achieving the connection. The three dots (ellipsis) indicates that a window will
open for you when you click on it.


VB PRACTICAL FILE
01190301912
This is a Property Page for the Connection Property. Click on the Build button to start 'building'
the Connection String...

and this window will appear, asking you to select the Provider for your database. For Oracle, you
want to select the Microsoft OLE DB Provider for ODBC Driver. Do so, then click on the Next
button...

VB PRACTICAL FILE
01190301912
Fill the data source name and click on Test connection. If the information you supply is correct,
you should see this dialog box appear...




If your connection test proves successful, click on Apply and then OK button and you should
notice that the Connection String Property of your Data Control has been filled in for you.






VB PRACTICAL FILE
01190301912
Now change the recordsource property by filling command type and writing sql query.






VB PRACTICAL FILE
01190301912
Now put three text boxes on the form, and set their Name, DataSource, and DataField
properties as follows:
Name DataSource DataField
txtAuthor Adodc1 Author
txtAuID Adodc1 Au_ID
txtYearBorn Adodc1 Year Born
Save and run the program. Notice how it works just like the other data control.
Now change the SQL property of the data control to select * from authors order by
author and run the program again. Notice the difference.
Change the SQL property back to what it was and add three command buttons to the
form, and set their Name and Caption properties as follows:
Name Caption
cmdNameOrder Order by Name
cmdYearOrder Order by Year
cmdIDOrder Order by ID
Put the following code in the cmdNameOrder_Click event:
Adodc1.RecordSource = "select * from authors order by author"
Adodc1.Refresh
Put the following code in the cmdYearOrder_Click event:
Adodc1.RecordSource = "select * from authors order by [YEAR BORN]"
Adodc1.Refresh
Put the following code in the cmdIDOrder_Click event:
AdoDC1.RECORDSOURCE = "select * from authors order by au_id"
AdoDC1.REFRESH
Save and run the program and see what happens when you click the buttons.



VB PRACTICAL FILE
01190301912
A screen-shot of the sample app at run-time is shown below:




.

You might also like