VB运行时错误 Run time error '13': Type mismatch 解决方案

391 views 0 comments posted at about 8 years ago Raymond Tang

前段时间同事使用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

Add comment

Comments (0)

No comments yet.
In this Page