IndexFile missing Dbase


How can I open or add indexes to a Dbase IV table when the Indexfile (*.MDX)
is missing. With Database desktop it is possible to open such a table, so I
expect that there is BDE function to do this. I want to make a tool that
opens such tables and restores the indexes.

I tried with, dbiopentable and Table.addindex but I get always
the message index does not exist.

I am using Delphi 1.0

All help is appreciated

Regards Didier,
Who is Participating?
jhuntConnect With a Mentor Commented:
What you basicly have to do is:
Examine if byte 28 in the header of the DBase file is set to 1. If so the BDE expects a production index. If the index file is not there then you will have to change byte 28 of the header from 1 to 0.

This is what does the trick:
TDBFHeader = record
 ID: byte;
 LastUpdate: array[1..3] of byte;
 LastRec: integer;
 DataOffset: word;
 RecordSize: word;
 Filler1: array[1..16] of byte;
 StructIDX: boolean;
 Filler2: array[1..3] of byte;

in the beforeopen event of your table:
procedure TCodeTable.DoBeforeOpen;
var      filename, extension: string;
      DBF: TFileStream; Dbase: TDatabase;
      Header: TDBFHeader;
 DBase := Session.FindDatabase ( Databasename );
 extension := ExtractFileExt(TableName);
 if not DBase.IsSQLBased and
     ( (CompareText(extension, '.DBF') = 0) or (TableType=ttDBase) )
 then begin
  filename := DBase.Directory + Tablename;
  if Empty(extension)then begin
   extension := '.DBF';
   filename := filename + '.DBF';
  if FileExists(Filename) then begin
        DBF := TFileStream.Create(Filename, fmOpenReadWrite + fmShareDenyNone);
  DBF.Read(Header, SizeOf(Header));
  if Header.StructIDX then begin
  Filename := Copy(Filename, 1, Length(Filename) - Length(Extension));
  if TableLevel = 25
  then filename := filename + '.CDX'
  else filename := filename + '.MDX';
  if not FileExists(Filename) then begin
   Header.StructIDX := false;
   DBF.Seek (0, soFromBeginning);
   DBF.Write(Header, SizeOf(Header));

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.