From: koterski@genghis (Steve Koterski)
Newsgroups: comp.lang.pascal
Subject: Re: Delphi - PACK a dBase database
Date: 18 Apr 1995 17:19:38 GMT
Organization: Borland Intl

There is no built-in procedure equivalent to the dBASE-specific PACK
operation to rebuild a table without soft-deleted records. To do this in
Delphi, you will need to call the Borland Database Engine (BDE) function
DbiPackTable. This function is constructed as:

   DbiPackTable(hDb, hCursor, pszTableName, pszDriverType, bRegenIdxs)

Where:

1. hDb is a handle to the database. (In the case of an active TTable, use
   the DBHandle property.)
2. hCursor is a handle to the table. (Use the TTable's Handle property.)
3. pszTableName is a pointer to a string representing the name of the
   dBASE data file. (Use nil.)
4. pszDriverType is a pointer to the driver type. (Use szdBASE.)
5. bRegenIdxs is a boolean specifying whther to rebuild the associated
   indexes. (Use True.)

To use this function, you must include the BDE-related units in the unit
that will call this function. The units are: DbiTypes, DbiProcs, and
DbiErrs. Also, the table must be opened in exclusive mode.

Following is an example of the DbiPackTable function executed in the
OnClick event procedure of a TButton. The example deactivates the TTable,
set the Exclusive property, reactivates the TTable, calls the function,
resets Exclusive (if desired), and eventually resets the tables Active
property to true. The example also shows the possible return values for
DbiPackTable, how to use them, and how to trap for an error not in that
list.

unit Pack_1;

interface

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

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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

procedure TForm1.FormShow(Sender: TObject);
begin
  Table1.Active := True;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Table1.Active := False;
end;

end.

--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ Steve Koterski               _/   The opinions expressed here are    _/
_/ koterski@borland.com         _/         exclusively my own           _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
