前段时间同事使用DAO从Access数据库中取数据,可不知道为何原因,在使用Recordset的时候出现了这个错误,虽然理解字面意思是类型不匹配,但是哪里类型不匹配了差了半天也没查出来。

后来通过微软的帮助文档找了问题的解决方案http://support.microsoft.com/kb/181542#top

问题出现的原因是ADO和DAO中都有Recordset这个类,如果在你的项目的引用中ADO的优先级大于了DAO,则当你没有指定Recordset的时候,就会出现这个问题了,因为默认会当做ADO的Recordset处理,当然类型就不匹配了。

如下:

Dim dbs AS Database
Dim rs AS Recordset ' When you press the spacebar after "AS," the
                           ' list box includes multiple Recordset entries.
                           ' Pick any Recordset item, or type "Recordset."
Private Sub Form_Load()
Set dbs=OpenDatabase(dbname)
Set rs=dbs.OpenRecordset(source) ' The error occurs when this line
                                        ' of code executes.
End Sub

那么只需修改为:

Dim dbs AS Database
Dim rs AS DAO.Recordset ' When you press the spacebar after "AS," the
                           ' list box includes multiple Recordset entries.
                           ' Pick any Recordset item, or type "Recordset."
Private Sub Form_Load()
Set dbs=OpenDatabase(dbname)
Set rs=dbs.OpenRecordset(source) ' The error occurs when this line
                                        ' of code executes.
End Sub

对于不同的类型则不同的声明:

Dim adoRS As ADODB.Recordset
Dim daoRS As DAO.Recordset

About author
Disclaimer
The opinions and comments expressed herein are my own personal opinions and do not represent my employer's view in any way.
Comments
No comments.
Add comment
Title
Title is required.
Name
Name is required.
Email
Please input your personal email with valid format.
Comments
Please input comment content.
Captcha Refresh
Input captcha:

Subscription

Statistics

Locations of visitors to this page