使用 ADO 与 Microsoft Visual Basic 和 Visual Basic for ApplicationsUsing ADO with Microsoft Visual Basic and Visual Basic for Applications

无论你使用 Visual Basic 还是 Visual Basic for Applications,设置 ADO 项目和编写 ADO 代码都是类似的。Setting up an ADO project and writing ADO code is similar whether you use Visual Basic or Visual Basic for Applications. 本主题介绍如何将 ADO 与 Visual Basic 和 Visual Basic for Applications 结合使用,并注意所有差异。This topic addresses using ADO with both Visual Basic and Visual Basic for Applications and notes any differences.

引用 ADO 库Referencing the ADO Library

ADO 库必须由您的项目引用。The ADO library must be referenced by your project.

从 Microsoft Visual Basic 引用 ADOTo reference ADO from Microsoft Visual Basic

  1. 在 Visual Basic 的 " 项目 " 菜单中,选择 " 引用 ..."。In Visual Basic, from the Project menu, select References....

  2. 从列表中选择 " Microsoft ActiveX 数据对象 X.x.x.x 库 "。Select Microsoft ActiveX Data Objects x.x Library from the list. 验证是否还选择了以下库:Verify that at least the following libraries are also selected:

    • Visual Basic for ApplicationsVisual Basic for Applications

    • Visual Basic 运行时对象和过程Visual Basic runtime objects and procedures

    • Visual Basic 对象和过程Visual Basic objects and procedures

    • OLE 自动化OLE Automation

  3. 单击“确定”。Click OK.

例如,使用 Microsoft Access 就可以轻松地使用 ADO,使用 Visual Basic for Applications。You can use ADO just as easily with Visual Basic for Applications, by using Microsoft Access, for example.

引用 Microsoft Access 中的 ADOTo reference ADO from Microsoft Access

  1. 在 Microsoft Access 的 "数据库" 窗口中的 "模块" 选项卡中,选择或创建一个模块。In Microsoft Access, select or create a module from the Modules tab in the Database window.

  2. 在 " 工具 " 菜单上选择 " 引用 ..."。On the Tools menu, select References....

  3. 从列表中选择 " Microsoft ActiveX 数据对象 X.x.x.x 库 "。Select Microsoft ActiveX Data Objects x.x Library from the list. 验证是否还选择了以下库:Verify that at least the following libraries are also selected:

    • Visual Basic for ApplicationsVisual Basic for Applications

    • Microsoft Access 8.0 对象库 (或更高版本) Microsoft Access 8.0 Object Library (or later)

    • Microsoft DAO 3.5 对象库 (或更高版本) Microsoft DAO 3.5 Object Library (or later)

  4. 单击“确定”。Click OK.

在 Visual Basic 中创建 ADO 对象Creating ADO Objects in Visual Basic

若要为该变量创建自动化变量和对象的实例,可以使用两种方法: DimCreateObjectTo create an automation variable and an instance of an object for that variable, you can use two methods: Dim or CreateObject.

DimDim

可以将 New 关键字与 Dim 一起使用来声明和创建 ADO 对象的实例,步骤如下:You can use the New keyword with Dim to declare and create instances of ADO objects in one step:

Dim conn As New ADODB.Connection

或者, Dim 语句声明和对象实例化也可以分为两个步骤:Alternatively, the Dim statement declaration and object instantiation can also be two steps:

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

备注

ADODB假设您已在项目中正确引用 ADO 库,则不需要使用Dim语句显式使用 progid。It is not required to explicitly use the ADODB progid with the Dim statement, assuming you have correctly referenced the ADO library in your project. 但是,使用它可以确保不会与其他库发生命名冲突。However, using it ensures that you will not have naming conflicts with other libraries.

备注

例如,如果在同一个项目中同时包含对 ADO 和 DAO 的引用,则应包括一个限定符,以指定实例化 记录集 对象时要使用的对象模型,如以下代码所示: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

CreateObjectCreateObject

对于 CreateObject 方法,声明和对象实例化必须是两个分离步骤:With the CreateObject method, the declaration and object instantiation must be two discrete steps:

Dim conn1
Set conn1 = CreateObject("ADODB.Connection") As Object

CreateObject 实例化的对象是后期绑定的,这意味着它们不是强类型化的,并且禁用了命令行完成。Objects instantiated with CreateObject are late-bound, which means that they are not strongly typed and command-line completion is disabled. 不过,它确实允许您跳过从项目引用 ADO 库,并使您能够实例化特定版本的对象。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

还可以通过专门创建对 ADO 版本2.0 类型库的引用并创建对象来实现此目的。You could also accomplish this by specifically creating a reference to the ADO version 2.0 type library and creating the object.

使用 CreateObject 方法来实例化对象通常比使用 Dim 语句慢。Instantiating objects by using the CreateObject method is typically slower than using the Dim statement.

处理事件Handling Events

若要在 Microsoft Visual Basic 中处理 ADO 事件,必须使用 WithEvents 关键字声明模块级变量。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. 有关处理 ADO 事件的更详尽的讨论,请参阅 处理 Ado 事件For a more thorough discussion of handling ADO events, see Handling ADO Events.

Visual Basic 示例Visual Basic Examples

ADO 文档附带了许多 Visual Basic 示例。Many Visual Basic examples are included with the ADO documentation. 有关详细信息,请参阅 Microsoft Visual Basic 中的 ADO 代码示例For more information, see ADO Code Examples in Microsoft Visual Basic.

另请参阅See Also

Microsoft ActiveX 数据对象 (ado) 使用ado 和使用ado 和脚本语言进行 Microsoft Visual C++Microsoft ActiveX Data Objects (ADO) Using ADO with Microsoft Visual C++ Using ADO with Scripting Languages