Recent

Author Topic: TDBF Recordcount  (Read 441 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 82
TDBF Recordcount
« on: September 12, 2024, 10:21:12 am »
Hi All

Please help again.
I have a Tdbf with a Tdbgridview connected to it.
Displays everything correctly.

I need to know the recordcount but i get a major difference in what is displayed in the Tdbgridview  and what is returned with
Code: Pascal  [Select][+][-]
  1. entries:=dbgrid5.DataSource.DataSet.RecordCount;

I assume it counts the Deleted fields as well?
How do i get just the actual number of Displayed rows in the Tdbgridview or the valid and not deleted rows in the Tdbf?

I see there are many questions on the web, but I cannot see a real answer.

Thanks for all the help.

-Peter

rvk

  • Hero Member
  • *****
  • Posts: 6524
Re: TDBF Recordcount
« Reply #1 on: September 12, 2024, 10:41:20 am »
I need to know the recordcount but i get a major difference in what is displayed in the Tdbgridview  and what is returned with
What kind of real differences do you see.

You need to realize that you can't count on recordcount being accurate until you have traversed the entire dataset.

See https://www.freepascal.org/docs-html/fcl/db/tdataset.recordcount.html
Quote
For optimization purposes, a TDataset descendent may choose not to fetch all records from the database when the dataset is opened. If this is the case, then the RecordCount will only reflect the number of records that have actually been fetched at the current time, and therefor the value will change as more records are fetched from the database.

I'm not sure if that's also the case for TDBF.

You might want to do dbgrid5.DataSource.DataSet.Last; and dbgrid5.DataSource.DataSet.First; after opening the table to make sure the recordcount is accurate.

Another option would be to read the count with a separate query (SELECT COUNT(*) FROM YOUR_TABLE WHERE YOUR_WHERE_CLAUSE).

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 82
Re: TDBF Recordcount
« Reply #2 on: September 12, 2024, 10:46:26 am »
Thank you.

Im am using a simple legacy database. .DBF.
Pity one cannot just ask for the number of visible rows in the Tdbgridview, that would be a fast solution like when you get the rows in a normal Listview.

-Peter
« Last Edit: September 12, 2024, 10:50:05 am by Petrus Vorster »

rvk

  • Hero Member
  • *****
  • Posts: 6524
Re: TDBF Recordcount
« Reply #3 on: September 12, 2024, 11:08:24 am »
Pity one cannot just ask for the number of visible rows in the Tdbgridview, that would be a fast solution like when you get the rows in a normal Listview.
Do you mean the visible rows on screen in the TDBGrid? and not the existing rows in the TDataset?
(That was not clear from your original question where you asked for the recordcount.)

Then you can just look at this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TCustomDBGridHack = class(TCustomDBGrid);
  3.  
  4. function GetVisibleRows(DBGrid: TCustomDBGrid): Integer;
  5. begin
  6.    Result := TCustomDBGridHack(DBGrid).VisibleRowCount;
  7. end;

But my guess is you want the recordcount and that's only visible when you do a Last/First.

O, you could also adjust the BufferCount of the TDataset
(That should do the same as a Last/First... i.e. reading all records in one go but I'm not sure how this is done in TDBF).
« Last Edit: September 12, 2024, 11:11:23 am by rvk »

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 82
Re: TDBF Recordcount
« Reply #4 on: September 12, 2024, 11:16:43 am »
Thank you.

Lots to learn still.

Much appreciated.

-Peter

rvk

  • Hero Member
  • *****
  • Posts: 6524
Re: TDBF Recordcount
« Reply #5 on: September 12, 2024, 11:19:09 am »
O, for TDBF I see there is another option.
(I'm not that familiar with TDBF)

But I see there is a GetExactRecordCount.
https://www.freepascal.org/daily/packages/fcl-db/dbf/tdbf.exactrecordcount.html

It does the loop (while not eof) for you and count the exact records in the dataset.
(not tested it)

gidesa

  • Full Member
  • ***
  • Posts: 131
Re: TDBF Recordcount
« Reply #6 on: September 12, 2024, 08:32:55 pm »
Hello,
the Tdbf manual is clear about record count properties:

Quote
7.8 ExactRecordCount
     property ExactRecordCount: Integer read GetExactRecordCount;
Examine ExactRecordCount to determine the exact number of records in the current dataset. This takes into account deleted, filtered and indexed records. This in contrary to RecordCount, which will always give a rough upper bound estimate. Note that this property needs to scan the complete dataset to find the number of records that are active, while RecordCount is just a simple calculation.

 

TinyPortal © 2005-2018