Skip to Content
Course content

4.1.2 Understanding ADO Libraries

ADO (ActiveX Data Objects) is a set of libraries used in Microsoft Access and other Microsoft applications to interact with databases. It allows you to connect to, manage, and manipulate data stored in various relational database management systems (RDBMS), such as SQL Server, Oracle, or even Access itself. ADO is designed to provide a more flexible and robust way of managing database connections compared to older technologies like DAO (Data Access Objects).

What is ADO?

ADO is an API (Application Programming Interface) that provides a way to interact with data in databases through a consistent interface, regardless of the database type. It abstracts the complexities of connecting to and managing databases, making it easier to develop database applications.

ADO provides several key objects for interacting with databases:

  • Connection: Represents a connection to a data source.
  • Recordset: Represents a set of records retrieved from a database.
  • Command: Represents an SQL statement or stored procedure that can be executed on a database.
  • Parameter: Represents parameters used in SQL queries or stored procedures.

Key ADO Objects:

  1. Connection Object: The Connection object represents a connection to a data source. It allows you to open, close, and manage database connections.
    • Example:
      Dim conn As Object
      Set conn = CreateObject("ADODB.Connection")
      conn.Open "Provider=SQLOLEDB;Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
      
    This opens a connection to an SQL Server database using the SQLOLEDB provider.
  2. Recordset Object: The Recordset object holds a set of records retrieved from the database. You can use it to read data, update data, and navigate through records.
    • Example:
      Dim rs As Object
      Set rs = CreateObject("ADODB.Recordset")
      rs.Open "SELECT * FROM Employees WHERE Department = 'Sales'", conn
      
    This opens a recordset containing all employees in the "Sales" department.
  3. Command Object: The Command object is used to execute SQL statements or stored procedures on the database. It can be used to execute queries and other database commands, including those that return results or modify the database.
    • Example:
      Dim cmd As Object
      Set cmd = CreateObject("ADODB.Command")
      cmd.ActiveConnection = conn
      cmd.CommandText = "UPDATE Employees SET Salary = 50000 WHERE EmployeeID = 1"
      cmd.Execute
      
    This updates the salary of an employee with ID 1.
  4. Parameter Object: The Parameter object represents parameters used in queries or stored procedures. Parameters allow you to pass values dynamically when executing SQL queries.
    • Example:
      Dim cmd As Object
      Set cmd = CreateObject("ADODB.Command")
      cmd.ActiveConnection = conn
      cmd.CommandText = "SELECT * FROM Employees WHERE Department = ?"
      cmd.Parameters.Append cmd.CreateParameter("Department", 8, 1, 50, "Sales")
      Set rs = cmd.Execute
      
    This query retrieves employees from the "Sales" department using a parameter.

Why Use ADO Libraries?

ADO is commonly used in scenarios where you need to interact with databases outside of Access, such as SQL Server, Oracle, or other OLE DB or ODBC-compliant data sources. It provides several advantages:

  1. Flexibility: ADO supports a wide variety of data sources, allowing you to connect to and interact with different databases using the same interface.
  2. Performance: ADO is faster than DAO when working with larger databases or complex queries, especially when connecting to external databases like SQL Server.
  3. Scalability: ADO is often the preferred choice for large, multi-tier applications due to its ability to work with remote databases and manage connections efficiently.
  4. Easy Integration: ADO integrates well with web applications, making it suitable for dynamic web pages and database-driven web applications.

Using ADO in Access VBA

In Microsoft Access, ADO is an external library that must be explicitly referenced before it can be used in VBA. It is commonly used when you need to interact with external databases or manage large datasets.

Referencing ADO Library:

To use ADO in Access, ensure that the Microsoft ActiveX Data Objects x.x Library is enabled in the References dialog box in the VBA editor:

  • Go to the VBA Editor (press Alt + F11).
  • In the editor, click on Tools > References.
  • Find and check the box for Microsoft ActiveX Data Objects x.x Library (e.g., Microsoft ActiveX Data Objects 6.1 Library).

Connecting to an External Database with ADO

Here’s how you can establish a connection to an external database, execute a query, and retrieve records using ADO.

  1. Establish Connection: Use the Connection object to open a connection to a database.
    Example (connecting to SQL Server):
    Dim conn As Object
    Set conn = CreateObject("ADODB.Connection")
    conn.Open "Provider=SQLOLEDB;Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
    
  2. Querying Data: Use the Recordset object to open and manipulate a set of records returned from a query.
    Example:
    Dim rs As Object
    Set rs = CreateObject("ADODB.Recordset")
    rs.Open "SELECT * FROM Employees WHERE Department = 'Sales'", conn
    
  3. Working with Data: Once a recordset is opened, you can navigate through the records using methods like MoveNext, MovePrevious, MoveFirst, and MoveLast.
    Example:
    Do While Not rs.EOF
        Debug.Print rs!EmployeeName & " - " & rs!Salary
        rs.MoveNext
    Loop
    
  4. Closing Connections and Recordsets: Always close your Recordset and Connection objects when finished to free up system resources.
    Example:
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing
    

ADO vs DAO

ADO and DAO are both data access libraries, but they serve different purposes and are suited for different environments:

  • DAO is more tightly integrated with Access and is best used when working with Access databases (Jet database engine). It is optimized for local databases.
  • ADO is more flexible and can be used for accessing data in a wide variety of databases, both local (Access) and remote (SQL Server, Oracle). ADO is generally preferred when working with remote databases or large datasets.

Practical Use Cases of ADO in Access:

  1. Connecting to SQL Server: A common use of ADO in Access is to connect to external SQL Server databases to retrieve or modify data stored in a central location.
  2. Dynamic Querying: ADO can be used to build and execute dynamic SQL queries based on user input or other criteria.
  3. Data Integration: ADO makes it easy to integrate data from multiple databases, such as combining data from an Access database and a SQL Server database into a single report or form.

Example: ADO to Connect to SQL Server

Here’s an example of how to use ADO to connect to a SQL Server database, run a query, and display results in a message box:

Sub ADOExample()
    Dim conn As Object
    Dim rs As Object
    Set conn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    ' Open connection to SQL Server
    conn.Open "Provider=SQLOLEDB;Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

    ' Execute query to retrieve data
    rs.Open "SELECT EmployeeName FROM Employees WHERE Department = 'Sales'", conn

    ' Loop through recordset and display names
    Do While Not rs.EOF
        MsgBox rs!EmployeeName
        rs.MoveNext
    Loop

    ' Close connection and recordset
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing
End Sub

Conclusion:

ADO (ActiveX Data Objects) provides a flexible, powerful, and efficient way to interact with databases from Access and other Microsoft applications. It is particularly suited for connecting to remote databases, handling large datasets, and executing dynamic SQL queries. While DAO is often used for Access-specific tasks, ADO offers a broader range of database connectivity options and is ideal for multi-database and multi-tier applications. Understanding how to use ADO libraries in Access allows developers to build robust database applications that can interact with various data sources seamlessly.

Commenting is not enabled on this course.