Excel - DAO versus ADO in VBA + RDO介绍

ActiveX Data Objects vs. Data Access Objects

与ADO相比,DAO与Access(JET engine environment)数据库的集成度更高,是为了Access所开发的,比ADO要老,所以如果你在Access内部工作,使用DAO会更容易,性能也可能会更好。但是ADO也很好用,在与其他外部数据源一起工作时,它具有优势。

DAO is more integrated with Access than ADO, so if you work within Access itself, it is easier to use DAO, and performance will probably be better too. But ADO also works well, and it offers advantages when working with external data sources.

本地数据库和小型项目应该使用DAO,而大型项目应该使用ADO。这样做的原因是,开发人员一般希望尽可能地保持简单。ADO对外部(远程)连接非常有效,而DAO则适合于操作本地对象。

在这片文章中,微软声明DAO为即将废弃的技术:

Driver history for Microsoft SQL Server - SQL Server | Microsoft Docs

但这篇2002年的文章有误导性,并已过时。当时,微软正计划逐步淘汰DAO,倾向于使用ADO。在Access 2000和2002中,新的数据库里并没有引用DAO库。但微软后来改变了主意,在Access 2003及以后的版本中,DAO再次成为默认的数据对象库。 在Access 2007及以后的版本,开发了一个全新的版本(Microsoft Office Access数据库引擎对象库,仍然缩写为DAO / the Microsoft Office Access database engine Object Library, still abbreviated as DAO)。DAO应该被认为是现在Access数据库访问的首选方法。

虽然ADO仍然可以在Access数据库中引用,但并不会继续优化。 要在VBA编辑器中使用ADO功能,要添加ADO库的引用:

在Tools -> References里,找到Microsoft ActivX Data Objects x.x Library,选择一个相应版本即可。

关于DAO的使用介绍可以看看:

Microsoft Access tips: DAO Object Model

DAO的新开发版本,实际叫做ACE。在Access中,由ACE替换了DAO。在Access 2007及以后版本中,不需要添加DAO库的引用,因为其已经内置到ACE中。

比如在Access 2010里,显示的引用为:

[√] Microsoft Office 14.0 Access database engine Object。

这个ACE库引用是默认选中的,但如果在Excel中打开VBA,要是用DAO库,就要手动勾选这个库引用。

比如在我的2022年的Office 365 Excel, VBA 版本7.1中,显示的就是: Microsoft Office 16.0 Access Database Engine Object Library.

而以前的库,Microsoft DAO 3.6 Object Library,如果是Access 2007以前的版本,就要勾选。

关于添加ADO或DAO库的引用的说明(Access和Excel中的VBA里的library reference)

库是提供功能的组件。安装或卸载任何软件都可能覆盖、删除或取消注册库。那么像Date()或Trim()这样的简单函数就不能工作了。

要查看Access项目引用了哪些库,可以打开任何代码窗口(如按Ctrl+G),并从工具菜单中选择引用。

Libraries are components that provide functionality. Installing or uninstalling any software may overwrite, remove, or de-register libraries. Then simple functions like Date() or Trim() don't work.

To see what libraries an Access project has referenced, open any code window (e.g. press Ctrl+G), and choose References from the Tools menu.

如果一个库被标记为MISSING,请点击浏览按钮,并找到该库的正确文件。

If a library is marked MISSING, click the Browse button, and locate the correct file for the library.

如果该库甚至没有显示,你可能需要重新注册它。点击Windows开始按钮,选择运行。输入regsvr32和库文件的完整路径。如果文件名中含有空格,则包括引号,例如:

If the library is not even shown, you may need to re-register it. Click the Windows Start button, and choose Run. Enter regsvr32 followed by the full path of the library file. Include quotes if the file name contains spaces, e.g.:

   regsvr32 "c:\program files\common files\microsoft shared\dao\dao360.dll"

偶尔,直到你取消注册库并重新注册,问题才会得到解决。在Access中取消对丢失的库的勾选。关闭Access。发出这个命令,然后再发出上面的命令,重新注册。

Occasionally, the problem is not solved until you unregister the library and re-register it. Uncheck the missing library in Access. Close Access. Issue this command, and then the one above to re-register it:

   regsvr32 -u "c:\program files\common files\microsoft shared\dao\dao360.dll"

使用第一步的命令重新注册。打开Access,并再次勾选这个库的引用

Re-register it (as above). Open Access, and select the library reference again.

如果库中有同名的对象,就会产生歧义。例如,DAO和ADO库都有一个Recordset对象,所以Access 2000和2002在打算使用DAO记录集时经常会被卡住。下面的方法可以解决这个冲突:

* 移除不需使用的ADO或DAO库的引用。

* 改变DAO库的优先级,将其置于ADO之上。

* 通过指定你想要的库来消除每个声明的歧义,例如:

   Dim rs As DAO.Recordset

Ambiguities arise if libraries have objects with the same name. For example, the DAO and ADO libraries both have a Recordset object, so Access 2000 and 2002 often choke when a DAO recordset was intended. Any of these ideas will solve the clash:

* Remove the reference to either the ADO or the DAO library. (See below.)

* Change the priority of the DAO library, by moving it above ADO.

* Disambiguate each declaration, by specifying the library you intend, e.g.:

   Dim rs As DAO.Recordset

如果你引用了错误的Access库,可能会造成冲突和数据损坏。例如,DAO 3.5 库是为 Access 97 设计的,而 DAO 3.6 是为 Access 2000、2002 和 2003 设计的。Access 2007使用ACEDAO库为您提供私有JET数据库引擎中的新功能。使用西面的表格来确保你为你的Access版本引用正确的文件。

Inconsistencies and data corruption can result from referencing the wrong library for your version of Access. For example, the DAO 3.5 library was designed for Access 97, whereas DAO 3.6 is designed for Access 2000, 2002, and 2003. Access 2007 uses the ACEDAO library to give you the new functionality in the private JET engine. Use the table below to ensure you reference the correct files for your version of Access.

引用不需要的库会浪费内存,使加载速度变慢,增加了出现问题的机会,并妨碍了调试工作。三个主要部分是VBA、Access和DAO。VBA和Access库是内置的,并且总是具有最高的优先级。Access不会让你从你的数据库中删除这些库。

Unnecessary libraries waste memory, slow loading, increase the chance of problems, and hamper debugging. The three essentials are VBA, Access, DAO. The VBA and Access libraries are built in, and will always have highest priority. Access will not let you remove these libraries from your database.

DAO是数据访问对象的缩写。它是专门为Access编写的对象模型,所以它为存储在Access表中的数据提供了最好的功能和性能,这一点并不奇怪。

DAO stands for Data Access Objects. It is the object model written specifically for Access, so it's no surprise that it gives the best power and performance for data stored in Access tables.

ADO是ActiveX Data Objects的缩写。它是一个比较通用的库,旨在处理来自Access表以外的数据(SQL Server)和Access以外的界面(如基于Web的)。如果你正在处理这些企业数据库,这里就不解释ADO的内容了。

ADO stands for ActiveX Data Objects. It is a more generic library, designed to handle data from sources other than Access tables (SQL Server) and interfaces other than Access (e.g. web-based.) If you are working on these enterprise databases, you don't need an explanation of ADO here.

不幸的是,微软将ADO作为Access 2000和2002的默认库。DAO在Access 2003和2007中恢复了默认。

Unfortunately, Microsoft made ADO the default library in Access 2000 and 2002. DAO is back by default in Access 2003 and 2007.

当你有理由这样做的时候,要包括其他库。例如,在Access 2002中引用Microsoft Office 10.0对象库可以得到FileDialog对象,这样你就可以向用户显示一个文件打开的对话框,而不必求助于API调用。

Include other libraries when you have a reason to do so. For example, referencing the Microsoft Office 10.0 Object Library in Access 2002 gives you the FileDialog object so you can show the user a File Open dialog without resorting to API calls.

本表列出了适合您的Access版本的参考资料,如果参考资料 "缺失",则列出了文件名:

This table lists the references appropriate to your version of Access, along with the file name if the reference is "missing":

Library name

Access

Ver.

File type

Library file (default location)

VBA
(Visual Basic For Applications)

 

 

 

 

2010

 

C:\Program Files\Common Files\Microsoft Shared\vba\vba7\vbe7.dll

2007

C:\Program Files\Common Files\Microsoft Shared\vba\vba6\vbe6.dll

2003

2002

2000

97

C:\Program Files\Common Files\Microsoft Shared\vba\vba332.dll

Access
(Microsoft Access xx.x Object Library)

 

 

 

 

2010

14

 

C:\Program Files\Microsoft Office\Office14\msacc.olb

2007

12

C:\Program Files\Microsoft Office\Office12\msacc.olb

2003

11

C:\Program Files\Microsoft Office\Office11\msacc.olb

2002

10

C:\Program Files\Microsoft Office\Office10\msacc.olb

2000

9

C:\Program Files\Microsoft Office\Office\msacc9.olb

97

8

C:\Program Files\Microsoft Office\Office\msacc8.olb

DAO
(Microsoft DAO 3.x Object Library
or
Microsoft Office xx.x Access database engine)

 

 

 

 

2010

14

ACCDB

C:\Program Files\Common Files\Microsoft Shared\Office14\acedao.dll

MDB

C:\program files\common files\microsoft shared\dao\dao360.dll

2007

12

ACCDB

C:\Program Files\Microsoft Office\Office12\acedao.dll

MDB

C:\program files\common files\microsoft shared\dao\dao360.dll

2003

3.6

 

2002

2000

97

3.51

C:\program files\common files\microsoft shared\dao\dao350.dll

ADODB
(Microsoft ActiveX Data Objects 2.x Library)

all versions

 

C:\Program Files\Common Files\System\ADO\msado25.tlb

关于代码使用的区别

让我们看看一个小例子,它使用ADO连接来循环浏览 "订单 "表并显示一个字段:

Function ShowRS()

On Error GoTo Err_Handle

Dim cnStr As String

Dim cn As ADODB.Connection

Dim cnRs As New ADODB.Recordset

Set cn = CurrentProject.Connection

cnRs.Open "SELECT * FROM orders", cn

   With cnRs

      .MoveLast

      .MoveFirst

         Do Until .EOF

            Debug.Print !orderid

               .MoveNext

         Loop

   End With

Err_Handle:

   cnRs.Close

   cn.Close

   Set cnRs = Nothing

   Set cn = Nothing

End Function

这也可以用DAO来完成。下面的代码输出的信息与前面的函数完全相同:

Function ShowRS()

On Error GoTo Err_Handle

Dim db As DAO.Database

Dim rs As DAO.Recordset

Set db = CurrentDb

Set rs = db.OpenRecordset("SELECT * FROM orders", dbOpenDynaset)

   With rs

      .MoveLast

      .MoveFirst

         Do Until .EOF

            Debug.Print !orderid

               .MoveNext

         Loop

   End With

Err_Handle:

   rs.Close

   db.Close

   Set rs = Nothing

   Set db = Nothing

      If Err.Number <> 0 Then

         MsgBox (Err.Description)

      End If

End Function

正如你所看到的,使用这两种方法编写的代码量没有太大区别,但语法却有很大不同。

使用ADO有很多值得学习的地方,其中大部分来自于后端位于服务器上的分离式数据库环境。众所周知,微软已经停止支持其过时的技术,即使是那些只有几年历史的技术。然而,DAO和ADO还将存在相当长的一段时间,因为大多数 "小规模 "的Access用户对较新的技术没有需求。另外,维护一个已经内置于核心产品的老技术是非常便宜的。

在Visual Basic代码中,有三种data access interfaces,前面介绍的ADO、DAO和RDO(Remote data objects)。

数据访问接口是一个对象模型,代表了访问数据的各个层面。使用Visual Basic,你可以以编程方式控制连接、语句生成器和返回的数据,以便在任何应用程序中使用。

为什么Visual Basic中有三种数据访问接口?数据访问技术在不断发展,三个接口中的每一个都代表了不同的技术水平。最新的是ADO,它的特点是比RDO或DAO更简单,但更灵活的对象模型。

而下面有关于RDO介绍,已经是一种淘汰的技术。

关于RDO (Remote data objects)

远程数据对象(简称RDO)是一个过时的数据访问应用编程接口,主要用于Windows 95及以后的操作系统上的Microsoft Visual Basic应用程序。这包括数据库连接、查询、存储过程、生成结果和提交更改。它允许开发人员创建接口,可以直接与远程机器上的开放数据库连接(ODBC)数据源进行交互,而不必处理相对复杂的ODBC API。

远程数据对象包含在Visual Basic的4、5和6版本中,而现在2022年Office 365中Excel里面看VBA的版本是,Microsoft Visual Basic for Applications 7.1;RDO的最终版本是2.0版本。

Remote Data Objects (abbreviated RDO) is an obsolete data access application programming interface primarily used in Microsoft Visual Basic applications on Windows 95 and later operating systems. This includes database connection, queries, stored procedures, result manipulation, and change commits. It allowed developers to create interfaces that can directly interact with Open Database Connectivity (ODBC) data sources on remote machines, without having to deal with the comparatively complex ODBC API.

Remote Data Objects was included with versions 4, 5, and 6 of Visual Basic; the final version of RDO is version 2.0.

参考:

Redirecting

Microsoft Access ADO vs. DAO | Database Solutions for Microsoft Access | databasedev.co.uk

UtterAccess.com

https://en.wikipedia.org/wiki/Remote_Data_Objects

Microsoft Access ADO vs. DAO | Database Solutions for Microsoft Access | databasedev.co.uk

excel - Difference between ADO and DAO - Stack Overflow

Unable to Reference DAO Object Library

Microsoft Access tips: Solving Problems with Library References

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值