Board index » delphi » RecordCount Method for TTable and TQuery

RecordCount Method for TTable and TQuery

The RecordCount function for TTable includes deleted records in its
count.  Is there a way to get only the number of active records in a
table or query?

Re:RecordCount Method for TTable and TQuery

On Sun, 18 Feb 1996 19:55:41 GMT, (Tom Valois) wrote:
>The RecordCount function for TTable includes deleted records in its
>count.  Is there a way to get only the number of active records in a
>table or query?

Though not specified, you must be accessing a dBASE table with these
components. Included below is information on how dBASE tables handle the
deletion of records, the PACK process, and how to use the BDE API function
DbiPackTable in a Delphi application to remove these deleted records...

dBASE tables use what is referred to as a "soft delete." When a record in a
dBASE table is "deleted," it is not physically removed from the table.
Instead, it is marked as deleted by the placement of an asterisk (ASCII
decimal 42) in the first byte of the record. The record is then suppressed
for data operations (searches, counts, filtering, etc.). To physically
remove such marked records from the table, it is necessary to perform a
PACK operation (named after the dBASE command for this operation.

In Delphi, the TTable component only supports those operations common to
all table types supported, ommitting those features that are specific to
only one table type. This allows for having only one TTable object, as
opposed to multiple TTable objects: one for each table type.

Because the soft delete is one of those specialized features found only in
the dBASE table, Delphi itself includes no provision for this operation.
However, the Borland Database Engine (BDE) does. BDE API functions can be
called from Delphi applications, making the use of these functions
available even though Delphi does not include them directly. The PACK
operation is effected with the BDE API function DbiPackTable.

Below is an example of the DbiPackTable in use in a Delphi unit. As when
calling any BDE API function, the BDE wrapper units DbiTypes, DbiErrs, and
DbiProcs must be included in the Uses section of the unit making the
function call. This example includes a demonstration of BDE error checking.
BDE functions return a value of type DBIResult that indicates the success
or failure of the function call, no error message will be generated by the
BDE so this is the only way to assess success. Any result but DBIERR_NONE
indicates a failure. With many BDE functions, failed calls cause a
different return code for different reasons for the failure. The various
failure error codes for the DbiPackTable function are demonstrated below.

unit Pack_1;


  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables,
  DbiTypes, DbiProcs, DbiErrs;

  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;


{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
  Error: DbiResult;
  ErrorMsg: String;
  Special: DBIMSG;
  table1.Active := False;
    Table1.Exclusive := True;
    Table1.Active := True;
    Error := DbiPackTable(Table1.DBHandle, Table1.Handle, nil, szdBASE,
    Table1.Active := False;
    Table1.Exclusive := False;
    Table1.Active := True;
  case Error of
      ErrorMsg := 'Successful';
      ErrorMsg := 'The specified table name or the pointer to the ' +
        'table name is NULL';
      ErrorMsg := 'The specified database handle or cursor handle is ' +
        'invalid or NULL';
      ErrorMsg := 'Table name does not exist';
      ErrorMsg := 'Table type is unknown';
      ErrorMsg := 'The table is not open in exclusive mode';
    DbiGetErrorString(Error, Special);
    ErrorMsg := '[' + IntToStr(Error) + ']: ' + Special;
  MessageDlg(ErrorMsg, mtWarning, [mbOk], 0);


Steve Koterski                  "Results! Why, man, I have gotten a lot of
Product Group Manager           results. I know several thousand things
Delphi Technical Support        that won't work."
Borland International, Inc.                    -- Thomas Edison, 1847-1931

Other Threads