Solved Getting the count of tables in an external database

KitaYama

Well-known member
Local time
Tomorrow, 02:04
Joined
Jan 6, 2022
Messages
2,046
How can I get the count of user defined tables in an external database?
Code:
    Dim dbs As DAO.Database
    Dim rs As DAO.Recordset
    Dim sql as string

    Set dbs = OpenDatabase(PathToMyTarget)
    Debug.Print dbs.Name
    Debug.Print dbs.QueryDefs.Count
    Debug.Print dbs.TableDefs.Count

    sql = "SELECT * FROM MSysObjects WHERE MSysObjects.Type=6 AND Left([Name],1)<>'~'"
    Set rs = dbs.OpenRecordset(sql)
The above gives me the following error on
Set rs= ......

2023-05-06_10-27-07.png


thanks
 
Last edited:
Solution
Find the difference: ;)
Code:
   Dim dbs As DAO.Database
   Dim rs As DAO.Recordset
   Dim sql as string
   Set dbs = DBEngine.OpenDatabase(PathToMyTarget)

   Debug.Print dbs.Name
   Debug.Print dbs.QueryDefs.Count
   Debug.Print dbs.TableDefs.Count

   sql = "SELECT * FROM MSysObjects WHERE MSysObjects.Type=6 AND Left([Name],1)<>'~'"
   Set rs = dbs.OpenRecordset(sql)
It seems from that error that the database was secured by password? Did it let you do the Debug.Print for the dbs.xxxDefs.Count statements? If so, perhaps you could try a "FOR EACH tblvariable IN dbs.TableDefs" loop. Slower, but might work OK.
 
Database is not password protected. Debug shows correct result.

I have a loop through TableDef that works perfect. Before going through the loop I just wanted to have a count to show it in a progressbar.

Thanks for trying to help.
 
One method I often use is to link to the external MSysObjects table then do a DCount of all objects with Type1, 4 or 6.
Filter to exclude system tables if required.
That approach is used in several of my apps including
 
Last edited:
Find the difference: ;)
Code:
   Dim dbs As DAO.Database
   Dim rs As DAO.Recordset
   Dim sql as string
   Set dbs = DBEngine.OpenDatabase(PathToMyTarget)

   Debug.Print dbs.Name
   Debug.Print dbs.QueryDefs.Count
   Debug.Print dbs.TableDefs.Count

   sql = "SELECT * FROM MSysObjects WHERE MSysObjects.Type=6 AND Left([Name],1)<>'~'"
   Set rs = dbs.OpenRecordset(sql)
 
Last edited:
Solution
Find the difference: ;)
Code:
   Dim dbs As DAO.Database
   Dim rs As DAO.Recordset
   Dim sql as string
   Set dbs = DBEngine.OpenDatabase(PathToMyTarget)

   Debug.Print dbs.Name
   Debug.Print dbs.QueryDefs.Count
   Debug.Print dbs.TableDefs.Count

   sql = "SELECT * FROM MSysObjects WHERE MSysObjects.Type=6 AND Left([Name],1)<>'~'"
   Set rs = dbs.OpenRecordset(sql)
While I still don't understand why my method didn't work, but as long as I have a working method, I'm satisfied.

Million thanks.
 
It will also work like this:
Code:
   Static DbEngineCalled As Boolean
  
   Dim x As Variant
   Dim dbs As DAO.Database
   Dim rs As DAO.Recordset
   Dim sql As String
  
   If Not DbEngineCalled Then
      DbEngineCalled = True
      x = Application.DBEngine.Version
   End If
  
   Set dbs = OpenDatabase(PathToMyTarget)

   Debug.Print dbs.Name
   Debug.Print dbs.QueryDefs.Count
   Debug.Print dbs.TableDefs.Count

   sql = "SELECT * FROM MSysObjects WHERE MSysObjects.Type=6 AND Left([Name],1)<>'~'"
   Set rs = dbs.OpenRecordset(sql)
 

Users who are viewing this thread

Back
Top Bottom