Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to pack a Paradox or dBase table programmatically Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
pack a Paradox or dBase table programmatically 28-Aug-02
Delphi 4.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

How to pack a Paradox or dBase table programmatically


Solve 1:

1   function dgPackParadoxTable(Tbl: TTable; Db: TDatabase): DBIResult;
2   {Packs a Paradox table by calling the BDE DbiDoRestructure function. The TTable 
3   passed as the first parameter must be closed. The TDatabase passed as the second 
4   parameter must be connected.}
5   var
6     TblDesc: CRTblDesc;
7   begin
8     Result := DBIERR_NA;
9     FillChar(TblDesc, SizeOf(CRTblDesc), 0);
10    StrPCopy(TblDesc.szTblName, Tbl.TableName);
11    TblDesc.bPack := True;
12    Result := DbiDoRestructure(Db.Handle, 1, @TblDesc, nil, nil, nil, False);
13  end;

Solve 2:

If you use the DBGrid and DBNavigator to delete records in a table which has unique 
fields, you will find that the table grows relentlessly and you can't re-enter the 
same data without packing the table first.

The following routine will pack and reindex a DBase and Paradox table, taking from 
a few seconds to a few minutes. Tested with a 650K DBaseIV file. Much quicker after 
the first call.

Just add the code below to the relevant sections. Call the function with your 
table's name, then wait while it grinds away. Returns True if the table is packed 

14  uses
15    WinProcs, Classes, SysUtils, StdCtrls, Forms, Controls, DB, DBIProcs, DBITypes,
16    DBIErrs, DBTables;
18  {Add to declarations}
20  function PackTable(tbl: TTable): Boolean;
22  {Add to Implementation}
23    function PackTable(tbl: TTable): Boolean;
25      {Packs a DBaseIV (and Paradox?) table}
26    var
27      crtd: CRTblDesc;
28      db: TDataBase;
29    begin
30      try
31        Screen.Cursor := crHourglass;
32        Result := True;
33        with tbl do
34        begin
35          db := DataBase;
36          if Active then
37            Active := False;
38          if not db.Connected then
39            db.Connected := True;
40          FillChar(crtd, SizeOf(CRTblDesc), 0);
41          StrPCopy(crtd.szTblName, TableName);
42          crtd.bPack := True;
43          if DbiDoRestructure(db.Handle, 1, @crtd, nil, nil, nil, False) <> 
44  DBIERR_NONE then
45            Result := False;
46          Open;
47        end;
48      except
49        on Exception do {any exception}
50          Result := False;
51      end;
52      Screen.Cursor := crDefault;
53  end;

Solve 3:

Wouldn't it be great for the TTable component to have a method that does this? Just 
open up a TTable, connected it to a table on disk, call the method, and wham! The 
table's packed (Hmm.... I just might have to look into that). But short of that, 
you have to make direct calls to the BDE to accomplish this task. For dBase tables, 
it's easy. There's a single function called dbiPackTable that'll pack any dBase 
file. For Paradox files, you have to jump through a couple of hoops first, then 
call dbiDoRestructure because Paradox tables can only be packed within the context 
of a table restructure (Note: If you've restructured a table in Paradox or the 
Database Desktop, you'll notice a checkbox at the bottom of the restructure dialog 
called "Pack Table").

Below is a simple procedure for packing tables. I took most of the code right out 
of the online help (yes, there's really good stuff in there if you know where 
look), and made some alterations. The difference between what I did and what the 
help file lists is that instead of the formal parameter being a TTable, I require a 
fully qualified file name (path/name). This allows for greater flexibility - the 
procedure opens up its own TTable and works on it instead you having to create one 
yourself. I guess it might all boil down to semantics, but I still like my way 
better (so there!). Check out the code below:

54  procedure PackTable(TblName: string);
55  var
56    tbl: TTable;
57    cProps: CURProps;
58    hDb: hDBIDb;
59    TblDesc: CRTblDesc;
60  begin
62    tbl := TTable.Create(nil);
63    with tbl do
64    begin
65      Active := False;
66      DatabaseName := ExtractFilePath(TblName);
67      TableName := ExtractFileName(TblName);
68      Exclusive := True;
69      Open;
70    end;
72    // Added 23/7/2000 to make sure that the current path is the same as the table
73    //see note below
75    SetCurrentDir(ExtractFilePath(TblName));
77    // Make sure the table is open exclusively so we can get the db handle...
78    if not tbl.Active then
79      raise EDatabaseError.Create('Table must be opened to pack');
81    if not tbl.Exclusive then
82      raise EDatabaseError.Create('Table must be opened exclusively to pack');
84    // Get the table properties to determine table type...
85    Check(DbiGetCursorProps(tbl.Handle, cProps));
87    // If the table is a Paradox table, you must call DbiDoRestructure...
88    if (cProps.szTableType = szPARADOX) then
89    begin
90      // Blank out the structure...
91      FillChar(TblDesc, sizeof(TblDesc), 0);
92      // Get the database handle from the table's cursor handle...
93      Check(DbiGetObjFromObj(hDBIObj(tbl.Handle), objDATABASE, hDBIObj(hDb)));
94      // Put the table name in the table descriptor...
95      StrPCopy(TblDesc.szTblName, tbl.TableName);
96      // Put the table type in the table descriptor...
97      StrPCopy(TblDesc.szTblType, cProps.szTableType);
98      // Set the Pack option in the table descriptor to TRUE...
99      TblDesc.bPack := True;
100     // Close the table so the restructure can complete...
101     tbl.Close;
102     // Call DbiDoRestructure...
103     Check(DbiDoRestructure(hDb, 1, @TblDesc, nil, nil, nil, False));
104   end
105   else
106     {// If the table is a dBASE table, simply call DbiPackTable...} if
107       (cProps.szTableType = szDBASE) then
108       Check(DbiPackTable(tbl.DBHandle, tbl.Handle, nil, szDBASE, True))
109     else
110       // Pack only works on Paradox or dBASE; nothing else...
111       raise EDatabaseError.Create('You can only pack Paradox or dBase tables!');
113   with tbl do
114   begin
115     if Active then
116       Close;
117     Free;
118   end;
119 end;

See? Nothing fancy. What you should know is that all operations involving 
dbiDoRestructure revolve around a table descriptor type CRTblDesc. With this record 
type, you can set various field values, then execute dbiDoRestructure to make your 
changes. That's kind of the trick to making BDE calls in general. You typically 
work with some sort of record structure, then use that structure in one of the 
calls. I know I'm probably oversimplifying, but that's it in a nutshell. The point? 
Don't be scared of the BDE. More later!

I encourage you to look at the BDE online help under any dbi- subject. There are 
lots of code examples that will get you on your way.

NOTE: I received an email from Stewart Nightingale who said "When running PackTable straight after saving something to floppy it became obvious that packing a table must use the current directory for temporary tables and things - the current directory was "a:" and it came up with an error saying "No Disk in Drive a:". I put in the line "SetCurrentDir(ExtractFilePath(TblName));" at the top of the procedure so it would work properly ." I have added this line to the code sample shown above - use it if you wish, leave it out if you do not...........

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC