Results 1 to 4 of 4

Thread: Easy way to get the record count in a database?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 1999
    Location
    Dover, NH
    Posts
    54

    Post

    Does anyone know an easy way to get the total record count of all the tables in an Access database?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Post

    Sure, here's an easy example using DAO:

    Private Function GetRecordCount(sDatabase As String, sTableName As String) As Long
    Dim db As Database
    Dim rs As Recordset
    Set db = OpenDatabase(sDatabase)
    Set rs = db.OpenRecordset(sTableName, dbOpenDynaset)
    rs.MoveLast
    GetRecordCount = rs.RecordCount
    rs.Close
    db.Close
    Set rs = Nothing
    Set db = Nothing
    End Function

    Good luck!

    ------------------
    Joacim Andersson
    [email protected]
    [email protected]
    www.YellowBlazer.com



  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    I haven't tested it, but this will probably do it.
    Code:
        Dim nCtr As Integer
        Dim MyDatabase As Database
        Dim lCount As Long
        Dim MyRS As Recordset
        
        Set MyDatabase = Workspaces(0).OpenDatabase("E:\RWT 6.0\data\rwtu.mdb")
        
        For nCtr = 0 To MyDatabase.TableDefs.Count - 1
            If Left(MyDatabase.TableDefs(nCtr).Name, 4) <> "Msys" Then
                Set MyRS = MyDatabase.CreateSnapshot("Select * From " & MyDatabase.TableDefs(nCtr).Name)
                MyRS.MoveLast
                lCount = lCount + MyRS.RecordCount
                MyRS.Close
            End If
        Next
    The check for "MSys" excludes the system tables that you are probably not interested in.


    ------------------
    Marty

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 1999
    Location
    Dover, NH
    Posts
    54

    Post

    Martin - After a few minor edits, your code worked like a charm. Thanks!

    Here were the changes:

    "Msys" was changed to "MSys"

    MyDatabase.CreateSnapshot was changed to Mydatabase.OpenRecordset

    Once again - thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width