Board index » delphi » Rebuilding dbase Index Files

Rebuilding dbase Index Files

If an .MDX file is deleted or corrupted, what is the simplest method
programmatically for recreating the index file WITHOUT losing the data in
the .DBF file?
 

Re:Rebuilding dbase Index Files


For starters, doing anything with an index can never cause data loss with
dBase tables.

The easiest way is Table.AddIndex

Paul / ColumbuSoft
www.columbusoft.com

Quote
George Kwascha <kas...@mindspring.com> wrote in message

news:3990a01c_2@dnews...
Quote
> If an .MDX file is deleted or corrupted, what is the simplest method
> programmatically for recreating the index file WITHOUT losing the data in
> the .DBF file?

Re:Rebuilding dbase Index Files


The first problem to be faced is that there is a byte in the header of
the .DBF file that indicates the presence of an associated .mdx or
.cdx index.   If the index is corrupt or missing, an error will be
raised if you try to open the file as a table.

However, you can open it as an untyped file and set byte 28 of the
header to zero.    {0 = no CDX/MDX; 1 = use CDX/MDX}

In the example below, the full pathname of the .dbf file is entered
via an edit box (Edit1), but you could change this and enter it
programatically..  

var
  f: file of byte;
  b: byte;
begin
  assignFile(f, Edit1.Text);
  reset(f);
  seek(f, 28);
  b := 0;    {use b := 1 to reinstate the index}
  write(f, b);
  closefile(f);
end;

After that, do as Paul said and use AddIndex to recreate the indexes.
Based on the information in your previous post, I would expect the
code to be something like:

with Table1 do
begin
    AddIndex('CRCCNAME', 'PutDesiredExpressionHere', [ixExpression]);
    AddIndex('CRQUAL', 'PutDesiredExpressionHere', [ixExpression]);
    AddIndex('CRNAME', 'PutRequiredExpressionHere', [ixExpression]);
    AddIndex('CRDRCODE', 'DRCODE', [ixUnique]);
    AddIndex('CRCODE', 'CODE', [ixUnique]);
end;

I have only guessed your field names and may have got them wrong but
the parameters for addindex when used with dBase prior to version 7
are (1) the Index tag name, (2) the field name or, for  an expression
index, the expression and (3) the index options.

Regards,
Colin Acheson

On Tue, 8 Aug 2000 20:15:06 -0400, "George Kwascha"

Quote
<kas...@mindspring.com> wrote:
>If an .MDX file is deleted or corrupted, what is the simplest method
>programmatically for recreating the index file WITHOUT losing the data in
>the .DBF file?

Re:Rebuilding dbase Index Files


On Tue, 8 Aug 2000 20:15:06 -0400, "George Kwascha"

Quote
<kas...@mindspring.com> wrote:
>If an .MDX file is deleted or corrupted, what is the simplest method
>programmatically for recreating the index file WITHOUT losing the data in
>the .DBF file?

Paul's answer is correct.  You can detach the MDX file by deleting it
and opening the file through Database Desktop.  However, if you need
to do this in a program, here is some code that might be useful:

procedure DetachIndex(FileName: String);
var
  Flag: Byte;
begin
  with TFileStream.Create(FileName,
                        fmOpenReadWrite or fmShareExclusive) do
    try
      Seek(28, soFromBeginning);
      Read(Flag, 1);
      Seek(-1, soFromCurrent);
      Flag := (Flag and (not 1));
      Write(Flag, 1);
      Seek(0, soFromEnd);
    finally
      Free;
    end;
end;

HTH,

Jan

Other Threads