Question : InteropServices.COMException: Cannot open any more tables.

Dear Experts,

I have a simple VB.NET console application that loops through all msaccess tables and fields collections, search for a given text value in strSearch variable.

The application manages to scan through about 12 tables out of 207 tables in my msaccess test database. And it gives me run time error as the following:
---
Unhandled Exception: System.Runtime.InteropServices.COMException: Cannot open an
y more tables.
   at DAO.Database.OpenRecordset(String Name, Object Type, Object Options, Objec
t LockEdit)
   at scanTbls.Module1.Main(String[] sArgs)
---

I am also looking the same solution to this with ADO if DAO does not work well.
Here below is my console application:
---
Module Module1

    Sub Main(ByVal sArgs() As String)

        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim DaoDbEngine As New DAO.DBEngine()
        Dim tbl As DAO.TableDef
        Dim fld As DAO.Field

        Dim strTbl As String
        Dim strFld As String
        Dim strSQL As String
        Dim cntTbl As Integer
      Dim strSearch as String = "password"
      Dim strPathFile as String = "c:\test.mdb"

        db = DaoDbEngine.OpenDatabase(strPathFile)

        'loop through a collection of tables
        For Each tbl In db.TableDefs

            strTbl = tbl.Name

            For Each fld In tbl.Fields
                  If fld.Type = 10 Then   'type 10 is text, skip for number search
                      strFld = fld.Name
                        strSQL = "SELECT * FROM " & strTbl & " WHERE " & _
                                  strFld & " = '" & strSearch & "';"
                        rs = db.OpenRecordset(strSQL)
                        If rs.RecordCount > 0 Then  'if strSearch is found
                            found = True
                            Console.WriteLine("table name: " & strTbl)
                        End If
                    End If
                Next
            End If
        Next

        On Error Resume Next
        db.Close()
        tbl = Nothing
        fld = Nothing
        rs = Nothing
        db = Nothing
        DaoDbEngine = Nothing

    End Sub

End Module
---

Answer : InteropServices.COMException: Cannot open any more tables.

What happens if you add a Recordset close:

    For Each fld In tbl.Fields
                  If fld.Type = 10 Then   'type 10 is text, skip for number search
                      strFld = fld.Name
                        strSQL = "SELECT * FROM " & strTbl & " WHERE " & _
                                  strFld & " = '" & strSearch & "';"
                        rs = db.OpenRecordset(strSQL)
                        If rs.RecordCount > 0 Then  'if strSearch is found
                            found = True
                            Console.WriteLine("table name: " & strTbl)
                        End If

                        'Clean up open Recordsets as you go...
                        rs.Close

                    End If
                Next
Random Solutions  
 
programming4us programming4us