I am trying to find the size of each table or if not at least the
number of records to try to figure out which tables are taking
most space in the db. Is there a script I can run that will table
and its size/recordcount?
You don't need to use DCount() or a recordset to find the number of
records, as RecordCount is a property of a TableDef that is always
kept up-to-date by Jet. For instance:
?CurrentDB.TableDefs("MSysObjects").Recordcount
However, it won't be accurate for a linked table, only on the actual
table. So, for linked tables, you'd have to do this:
Dim strBackEnd As String
Dim dbBackEnd As DAO.Database
Dim tdf As DAO.TableDef
strBackEnd = Mid(CurrentDB.TableDefs("LinkedTableName").Connect,
11) Set dbBackEnd = DBEngine.OpenDatabase(strBackEnd)
For Each tdf In dbBackEnd.TableDefs
If Not (tdf.Attributes And dbSystemObject) Then
Debug.Print tdf.RecordCount
End If
Next tdf
Set tdf = Nothing
dbBackEnd.Close
Set dbBackEnd = Nothing
On the other hand, if you have both linked and local tables, you'll
need to do this:
Dim strBackEnd As String
Dim dbBackEnd As DAO.Database
Dim dbFrontEnd As DAO.Database
Dim tdf As DAO.TableDef
strBackEnd = Mid(CurrentDB.TableDefs("LinkedTableName").Connect,
11) Set dbBackEnd = DBEngine.OpenDatabase(strBackEnd)
Set dbFrontEnd = CurrentDB
For Each tdf In dbFrontEnd.TableDefs
If Len(tdf.Connect) = 0 And _
Not (tdf.Attributes And dbSystemObject) Then
Debug.Print tdf.RecordCount
Else
Debug.Print dbBackEnd.TableDefs(tdf.Name).RecordCounrt
End If
Next tdf
Set tdf = Nothing
dbBackEnd.Close
Set dbBackEnd = Nothing
Set dbFrontEnd = Nothing
Either of these will be much more efficient than the DCount() and
Recordset alternatives, and have a greater performance advantage the
larger the number of records. That is, for tables of a few thousand
records, there wouldn't be much difference, but for tables with
hundreds of thousands of records, this will be much faster than the
Recordset (especially if you're using .MoveLast instead of
COUNT(*)).
It could be that DCount() and the COUNT(*) recordset shortcircuit
and use the underlying table's RecordCount property, but still
there's more overhead involved than by simply going to the property
directly, as in the code examples above.