访问:运行时错误13类型不匹配

时间:2008-11-28 09:46:04

标签: ms-access

我在以下代码的末尾收到运行时错误13:

Sub plausibilitaet_check()

Dim rs As DAO.Recordset
Dim rs2 As ADODB.Recordset
Dim db As database
Dim strsql As String
Dim strsql2 As String
Dim tdf As TableDef




Set db = opendatabase("C:\Codebook.mdb")
Set rs = db.OpenRecordset("plausen1")

Set rs2 = CreateObject("ADODB.Recordset")
rs2.ActiveConnection = CurrentProject.Connection


For Each tdf In CurrentDb.TableDefs

   If Left(tdf.Name, 4) <> "MSys" Then
        rs.MoveFirst
        strsql = "SELECT * From [" & tdf.Name & "] WHERE "



        Do While Not rs.EOF
            On Error Resume Next

            strsql2 = "select * from table where GHds <> 0"
            Set rs2 = CurrentDb.OpenRecordset(strsql2)

错误发生在Set rs2 = CurrentDb.OpenRecordset(strsql2)

有人能看到我出错的地方吗?

2 个答案:

答案 0 :(得分:3)

CurrentDB.OpenRecordset返回DAO.Recordset的实例。您正尝试将结果分配给ADODB.Recordset

将rs2定义更改为

dim rs2 as DAO.Recordset

答案 1 :(得分:2)

你正在混淆ADO和DAO。在这种情况下,rs2应该是DAO记录集。

Sub plausibilitaet_check()

Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim db As database
Dim strsql As String
Dim strsql2 As String
Dim tdf As TableDef

Set db = opendatabase("C:\Codebook.mdb")
Set rs = db.OpenRecordset("plausen1")


For Each tdf In CurrentDb.TableDefs

   If Left(tdf.Name, 4) <> "MSys" Then
        rs.MoveFirst
        strsql = "SELECT * From [" & tdf.Name & "] WHERE "

        Do While Not rs.EOF
            On Error Resume Next

            strsql2 = "select * from table where GHds <> 0"
            Set rs2 = CurrentDb.OpenRecordset(strsql2)