Sie sind auf Seite 1von 15

Visual Basic & ADO Tutorial

This tutorial describes how you can use ADO objects in VB6. Now days, almost any time you write full
fledged database application you will want to use ADO. Along with this, as your applications become
more and more complex you will probably not want to rely on Visual Basic's data controls, but instead
use the ADO objects directly. Read on to find out exactly how this can be done.

Originally Written By TheVBProgramer.

The "Alphabet Soup" of Database Access

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.

In the VB4 and VB5 worlds, RDO was the main method used to interact with client/server databases.
RDO works perfectly fine with VB6, so when folks migrated their VB5 applications over to VB6, little or no
coding changes were required. However, ADO is the preferred method of database access for new VB6
applications .

About this Tutorial

This tutorial presents three small sample applications using ADO. All three applications use a local MS
Access database.
The first sample application introduces the ADO Data Control (ADODC) which demonstrates a "quick and
dirty" way to connect to a remote database. The second and third applications use ADO code: the second
allows navigation and searching of a database table; the third allows navigation and updating on a
database table. All three connect to an ODBC Data Source, which must be set up through the Windows
Control Panel. How to do this is described below.

Note: If you have previously set up a DSN for the Biblio database as described in the previous topic on
RDO, you can skip the section on setting up an ODBC data source and resume here.

Setting Up an ODBC Data Source


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.

 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.

 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.

 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 RDO in the sample application.

Sample 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.
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:

Property Value
Name adoBiblio
DataSourceName Biblio
SQL select * from authors
 Now put three text boxes on the form, and set their Name, DataSource, and DataField properties as
follows:

Name DataSource DataField


txtAuthor adoBiblio Author
txtAuID adoBiblio Au_ID
txtYearBorn adoBiblio 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.
Database Access with RDO (Remote Data Objects)

Introduction to ODBC and RDO

Open Database Connectivity (ODBC) provides a set of application programming interface (API) functions
which make it easier for a developer to connect to a wide range of database formats. ODBC gives the
developer a method of designing programs that are not specific to database format whether you are using
Oracle, SQL Server, Access, or others. ODBC drivers are DLLs that contain the functions that let you
connect to various databases. Each ODBC driver is separate for each database format. ODBC drivers
take the code from a program and convert the functions to the specific database format being used. RDO
(Remote Data Objects) is a thin layer of code that acts as an ODBC "wrapper", enabling the developer to
invoke ODBC functionality using familiar object method and property syntax.

The "Alphabet Soup" of Database Access

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 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 VB6, and there are no future plans for it.

In the VB4 and VB5 worlds, RDO was the main method used to interact with client/server databases.
RDO works perfectly fine with VB6, so when folks migrated their VB5 applications over to VB6, little or no
coding changes were required. However, ADO is the preferred method of database access for new VB6
applications .

About this Tutorial

This tutorial presents three small sample applications using RDO. All three applications use a local MS
Access database, as this is suitable for illustrative purposes; in actual practice, RDO would not be a
suitable choice for interacting with a local MS Access database. You could approach these sample
applications "as if" the local Access database was an Oracle or SQL Server database sitting on a server
somewhere – the coding techniques are the same.
The first sample application introduces the RDO Data Control (RDODC) which demonstrates a "quick and
dirty" way to connect to a remote database. The second and third applications use RDO code: the second
allows navigation and searching of a database table; the third allows navigation and updating on a
database table. All three connect to an ODBC Data Source, which must be set up through the Windows
Control Panel. How to do this is described below.

Setting Up an ODBC Data Source

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.

 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.

 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.

 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 RDO in the sample application.

Sample Application 1: Using the RDO Data Control (RDODC)

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 RemoteData Control 6.0 (SP3) as shown below and click OK.
The RDO Data Control should appear in your toolbox as shown below:
 Put a remote data control (RDC) on your form, and set the properties as follows:

Property Value
Name rdoBiblio
DataSourceName Biblio
SQL select * from authors
 Now put three text boxes on the form, and set their Name, DataSource, and DataField properties as
follows:

Name DataSource DataField


txtAuthor rdoBiblio Author
txtAuID rdoBiblio Au_ID
txtYearBorn rdoBiblio 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.

Das könnte Ihnen auch gefallen