Search code examples
delphims-accessdatabase-schemadelphi-xe5

How to get number of records of a table stored in an Access .MDB file?


I need the RcordCount of a TADOTable before opening it. So I run this query:

ADOQuery1.SQL.Text := ‘SELECT Count(*) AS Record_Count FROM Table1’;
ADOQuery1.Open;

But this takes 9.7s (average of 10 runs) on my PC for a table consisting of over 5 million records. This time seems nothing compared to even touching a table with this size, but I think this kind of data should be stored somewhere in the .mdb file. Is there any other way to get RecordCount of a table directly from the file? I'm using Delphi XE-5 and the file is in Microsoft Access 2003 format.


Solution

  • No, SELECT Count(*) is the only and the correct way to do it.

    But: almost 10s for this query is awfully slow, especially if this is on your local PC.

    Does the table have a Primary Key? If yes, what data type is it?

    I created a simple table with an Autonumber Primary Key and inserted 5 million records.
    SELECT COUNT(*) FROM tBig takes less than 1/10 seconds to execute.