unit Haupt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons,
DB, DBTables,
BDE;
type
TForm1 =
class(TForm)
SpeedButton1: TSpeedButton;
Table1: TTable;
OpenDialog1: TOpenDialog;
procedure SpeedButton1Click(Sender: TObject);
procedure ChangePIdx(Table: TTable; Fields: word);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if opendialog1.Execute
then begin
Table1.DatabaseName := ExtractFilePath(opendialog1.FileName);
Table1.TableName := ExtractFileName(opendialog1.FileName);
table1.Active := true;
ChangePIdx(Table1, 1);
end;
end;
procedure TForm1.ChangePIdx(Table: TTable; Fields: word);
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
Index: IDXDesc;
OP: CROpType;
W: word;
{
Example 12: Change the amount of fields in the primary index (Paradox and dBASE 7 only).
This example uses the following input:
ChangePIdx(Table1, 2);
This input will take an existing primary index and change it to apply to the first 2 fields in the table.
NOTE: If you define an index that is more restrictive on the table's data (usually by removing fields from the index) all records that violate the new primary index will be removed from the table. In other words data will be lost!
The function is defined as follows: }
begin
// Make sure that at least one field is specified...
if Fields < 1
then
raise EDatabaseError.Create('
Field count must be greater than 0');
// Make sure the table is open exclusively so we can get the db handle...
if Table.Active = False
then
raise EDatabaseError.Create('
Table must be opened to change the index');
if Table.Exclusive = False
then
raise EDatabaseError.Create('
Table must be opened exclusively to change the index');
// Get the table properties to determine table type...
Check(DbiGetCursorProps(Table.Handle, Props));
// Make sure that enough fields exist to create the index...
if Props.iFields < Fields
then
raise EDatabaseError.Create('
Cannot specify more fields in an index than existing fields');
// If the table is a Paradox table, you must call DbiDoRestructure...
if (Props.szTableType <> szPARADOX)
and (Props.szTableType <> szDBASE)
then
raise EDatabaseError.Create('
Table must be of type Paradox or dBASE 7');
// Blank out the structure...
FillChar(TableDesc, sizeof(TableDesc), 0);
// Get the database handle from the table's cursor handle...
Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
// Put the table name in the table descriptor...
StrPCopy(TableDesc.szTblName, Table.TableName);
// Put the table type in the table descriptor...
StrPCopy(TableDesc.szTblType, Props.szTableType);
// Fill in index descriptor...
FillChar(
Index, sizeof(IDXDesc), 0);
Index.bPrimary := TRUE;
Index.bUnique := FALSE;
//TRUE;
Index.bMaintained := TRUE;
Index.iFldsInKey := Fields;
for W := 1
to Fields
do
Index.aiKeyFld[W - 1] := W;
// Modify an existing index only...
OP := crMODIFY;
// Only one index to change...
TableDesc.iIdxCount := 1;
TableDesc.pecrIdxOp := @OP;
TableDesc.pidxDesc := @
Index;
// Close the table so the restructure can complete...
Table.Close;
try
// Call DbiDoRestructure...
Check(DbiDoRestructure(hDb, 1, @TableDesc,
nil,
nil,
nil, FALSE));
finally
Table.Open;
end;
end;
end.