Solved Getting the count of tables in an external database (1 Viewer)

KitaYama

Well-known member
Local time
Today, 13:17
Joined
Jan 6, 2022
Messages
1,541
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)

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:17
Joined
Feb 28, 2001
Messages
27,189
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.
 

KitaYama

Well-known member
Local time
Today, 13:17
Joined
Jan 6, 2022
Messages
1,541
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.
 

isladogs

MVP / VIP
Local time
Today, 05:17
Joined
Jan 14, 2017
Messages
18,235
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:

Josef P.

Well-known member
Local time
Today, 06:17
Joined
Feb 2, 2023
Messages
827
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

KitaYama

Well-known member
Local time
Today, 13:17
Joined
Jan 6, 2022
Messages
1,541
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.
 

Josef P.

Well-known member
Local time
Today, 06:17
Joined
Feb 2, 2023
Messages
827
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

Top Bottom