Setting up an ADO project and writing ADO code is similar whether you use Visual Basic or Visual Basic for Applications. This topic addresses using ADO with both Visual Basic and Visual Basic for Applications and notes any differences.
The ADO library must be referenced by your project.
To reference ADO from Microsoft Visual Basic
You can use ADO just as easily with Visual Basic for Applications, using Microsoft Access, for example.
To reference ADO from Microsoft Access
To create an automation variable and an instance of an object for that variable, you can use two methods: Dim or CreateObject.
You can use the New keyword with Dim to declare and instantiate ADO objects in one step:
Dim conn As New ADODB.Connection
Alternately, the Dim statement declaration and object instantiation can also be two steps:
Dim conn As ADODB.Connection Set conn = New ADODB.Connection
Note It is not
required to explicitly use the ADODB
progid with the Dim
statement, assuming you have properly referenced the ADO library in your
project. However, using it ensures that you won't have naming conflicts with
other libraries.
For example, if you include references to both ADO and DAO in the same project, you should include a qualifier to specify which object model to use when instantiating Recordset objects, as in the following code:
Dim adoRS As ADODB.Recordset
Dim daoRS As DAO.Recordset
With the CreateObject method, the declaration and object instantiation must be two discrete steps:
Dim conn1 Set conn1 = CreateObject("ADODB.Connection") As Object
Objects instantiated with CreateObject are late-bound, which means that they are not strongly typed and command-line completion is disabled. However, it does allow you to skip referencing the ADO library from your project, and enables you to instantiate specific versions of objects. For example:
Set conn1 = CreateObject("ADODB.Connection.2.0") As Object
You could also accomplish this by specifically creating a reference to the ADO version 2.0 type library and creating the object.
Instantiating objects with the CreateObject method is typically slower than using the Dim statement.
In order to handle ADO events in Microsoft Visual Basic, you must declare a module-level variable using the WithEvents keyword. The variable can be declared only as part of a class module and must be declared at the module level. For a more complete discussion of handling ADO events, see Chapter 7: Handling ADO Events.
Many Visual Basic examples are included with the ADO documentation. For more information, see ADO Code Examples in Microsoft Visual Basic.
ActiveX Data Objects Start Page | Using ADO with Microsoft Visual C++ | Using ADO with Microsoft Visual J++ | Using ADO with Scripting Languages