ADO: Active X Data Objects

ADO: Active X Data Objects

by Jason T Roff, Nancy Kotary

View All Available Formats & Editions

Getting data across platforms and formats is a cornerstone of present-day applications development. ADO: ActiveX Data Objects is both an introduction and a complete reference to ADO (ActiveX Data Objects), Microsoft's universal data access solution. You'll learn how to easily access data in multiple formats--such as email messages, Access databases, Word


Getting data across platforms and formats is a cornerstone of present-day applications development. ADO: ActiveX Data Objects is both an introduction and a complete reference to ADO (ActiveX Data Objects), Microsoft's universal data access solution. You'll learn how to easily access data in multiple formats--such as email messages, Access databases, Word documents, and SQL databases--even on different platforms, without spending extra time learning every last detail about each format.Author Jason Roff shows by example how to use ADO with your programming language of choice to save programming time, so you can concentrate on the content and quality of your application rather than the nitty-gritty of specific data formats.ADO: ActiveX Data Objects includes:

  • Chapters dedicated to the Connection, Recordset, Field, and Command objects and the Properties collection
  • A complete, detailed reference listing every ADO object, method, property, and event, in convenient alphabetical order
  • Chapters on ADO architecture, data shaping, the ADO Event Model
  • An appendix containing enumeration tables used by ADO objects and collections, listed alphabetically
  • Brief introductions to RDS, ADO.NET, and SQL
ADO: ActiveX Data Objects is a versatile one-stop guide to both the theory and practice of programming with ADO through Version 2.6. The thorough reference section and topic-specific chapters will help you find quick answers about the details of objects, collections, methods, and properties of ADO. And the abundance of practical code examples will give you a good grasp of how to use ADO's strong points most effectively.

Product Details

O'Reilly Media, Incorporated
Publication date:
Edition description:
1 ED
Product dimensions:
7.06(w) x 9.20(h) x 1.18(d)

Read an Excerpt

Chapter 3: Accessing ADO with Various Languages

Because ActiveX Data Objects expose their properties by means of COM interfaces, they can be accessed by any language that can utilize COM. In this book, we will look at accessing ADO from Visual Basic, Visual C++, and Visual J++, since these are the most commonly used tools for developing ADO applications on the Windows operating system.

In addition to these three languages, there are two scripting languages that are already well-established: VBScript and JScript. VBScript is a lightweight subset of Visual Basic that's designed specifically for adding script to HTML documents. JScript is Microsoft's implementation of JavaScript, designed for script development within HTML documents.

Although ADO is meant to offer the same development interface to each language from which it is accessed, some inconsistencies arise because of differences in their syntax and the development environments in which they are used. In this chapter, we will take a look at each of the five languages and learn how to get started developing ADO applications in each.

Accessing ADO with Visual Basic

Visual Basic is probably the most popular language in which to develop applications for ADO. It is also the language used in the examples and code throughout this book. Visual Basic is a very easy language to understand and excellent for both beginners and advanced developers.

Referencing ActiveX Data Objects

To write an application in Visual Basic using ActiveX Data Objects, you must first tell Visual Basic about them by adding ADO to the list of references that Visual Basic uses to run an application. You may do this by selecting the Project References menu item so that the References dialog box appears, as shown in Figure 3-1. In the Available References list box, select the latest version of Microsoft ActiveX Data Objects Library that you have installed. Now you are ready to create and use ADO objects within your current Visual Basic application.


Figure 3-1. The References dialog box of Visual Basic

When redistributing ADO applications, you should use the MDAC redistributable package available for download from Microsoft's web site.

Creating ActiveX Data Objects

In Visual Basic, you can create new ADO objects by simply referencing the ADODB classes of the Microsoft ActiveX Data Objects Library. The following piece of code creates a Connection and a Recordset object in Visual Basic:

' create a reference to a Connection object
Dim con As ADODB.Connection
' create a reference to a Recordset object
Dim rst AS ADODB.Recordset

As with any other Visual Basic objects, you must instantiate them before they can be used, as in the following examples:

' create a new instance of the Connection object
Set con = New ADODB.Connection
' create a new instance of the Recordset object
Set rst = New ADODB.Recordset

In the previous examples, the ADODB prefix to the ADO objects is used in case your Visual Basic development environment references another object of the same class name in a different class library. The following code illustrates how a DAO Recordset and an ADO Recordset can be created within the same project:

' which object model is this from?
Dim rst As Recordset
' explicitly specifying the Data Access Object Model
Dim rstDAO As DAO.Recordset
' explicitly specifying the ActiveX Data Object Model
Dim rstADO As ADODB.Recordset

If you know for a fact that no other class library listed in the References dialog box of your current Visual Basic application has the same class names as ADO, you may remove the ADODB prefix when declaring and instantiating object variables. However, if you are using more than one object model with the same class definitions (as in the previous example), not specifying the library from which the class should be derived tells VB to instantiate the class from the library that comes first in the list of references to the project.

In Visual Basic, it is always a good idea to remove an object from memory once it is no longer being used. This is done by setting the object to Nothing, as follows:

' remove the objects 
Set con = Nothing
Set rst = Nothing

Using ADO with Visual Basic: An Example

So that you can visualize how to work with ADO objects in Visual Basic, Example 3-1 uses ADO to open a connection to the Jet Biblio database and to return a recordset containing the names of its first ten authors. Each record is then written to a list box before both the Connection and Recordset objects are closed. Note that the example makes use of dynamic control creation supported by Visual Basic 6.0 or later; if you have an earlier version, simply delete the code that defines, instantiates, and sets the properties of the list box, and place a list box named lstAuthors on the form at design time.

To begin, create a new Application EXE project, and open the Project References menu so that you see the References dialog box shown in Figure 3-1. Select the latest version of Microsoft ActiveX Data Objects that you have installed, and press the OK button.

Now, replace the existing source code for Form1 with the code shown in Example 3-1, and run the application. That's all there is to it. Make sure that you have a Biblio.mdb database located at C:\Program Files\Microsoft Visual Studio\VB98, or if you have it in another location, simply change the path in the code that points to the Access database.

Example 3-1: A Simple Visual Basic Example

Option Explicit
Private WithEvents lstAuthors As ListBox
Private Sub Form_Load(  )
    ' create new instances of the Connection and Recordset objects
    Dim con As ADODB.Connection
    Dim rst As ADODB.Recordset
    ' instantiate the Connection and Recordset objects
    Set con = New ADODB.Connection
    Set rst = New ADODB.Recordset
    ' create two strings to define the connection and the recordset
    Dim sConString As String
    Dim sSQLString As String
    ' Create list box control
    Set lstAuthors = Me.Controls.Add("vb.listbox", _
                                     "lstAuthors", _
    lstAuthors.Visible = True
    ' open the BiblioDSN data source with the Connection object
    sConString = "Provider=Microsoft.Jet.OLEDB.4.0; " _
               & "Data Source=C:\Program Files" _
                             & "\Microsoft Visual Studio" _
                             & "\VB98\Biblio.mdb"
    con.Open sConString
    Debug.Print "Connection opened."
    ' create a Recordset object from a SQL string
    sSQLString = "SELECT TOP 10 Author " & _
                 "FROM Authors"
    Set rst = con.Execute(sSQLString)
    Debug.Print "SQL statement processed."
    ' retrieve all the data within the Recordset object
    Debug.Print "Getting data now..."
    Do Until (rst.EOF)
        lstAuthors.AddItem rst("Author").Value
    Debug.Print "End of data."
    ' close and remove the Recordset object from memory
    Set rst = Nothing
    Debug.Print "Closed and removed " _
              & "Recordset object from memory."
    ' close and remove the Connection object from memory
    Set con = Nothing
    Debug.Print "Closed and removed " _
              & "Connection object from memory."
End Sub
Private Sub Form_Resize(  )
    ' this code is added for asthetics
    lstAuthors.Top = 0
    lstAuthors.Left = 0
    lstAuthors.Width = Me.Width
    lstAuthors.Height = Me.Height
End Sub

A lot of this information will not make much sense to you now, but it will start to as you begin to learn how to use ActiveX Data Objects from the rest of the chapters in this book. The important technique to notice from this example is how the ADO objects are created in the beginning of the code example, and how the ADO objects are removed at the end of the code example.

Accessing ADO with Visual C++

Visual C++ is a much more difficult language and environment with which to develop applications for ActiveX Data Objects. Because it is so difficult, Microsoft is constantly trying to provide developers with easier ways to access ADO components.

By far the easiest method (and the only method described here) is one that takes advantage of the #import keyword. This approach offers not only the most control to the developer, but it also allows the developer to code in a Visual Basic programming style....

Meet the Author

Jason T. Roff has worn many hats with his extensive experience as a project manager, a software developer and a software architect. Roff's technical talents include expertise in C++, Java, ADO, Visual Basic, SQL Server, and object-oriented design.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >