> Hi,
> I've found how to create/delete indexes at run-time, but I've a
> problem with the MDX file.
> The file is automatically created (if not already there) when I create
> the first index, and automatically removed when I remove the last
> index. That's fine.
> But if I remove (the end user will do that for sure) the MDX from disk
> while there are still indexes than Delphi refuse to open the table.
> Is there a why (with VCL, no calls to BDE please) to say: ok, forget
> about any indexes and files and start again from scratch ?
> Thanks, Luca.
Luca,
Here's how to programmatically clear the "has production index" flag
from your .DBF header.
Use caution - this routine will alter any file you pass it as long as it
has a .dbf extension - you will have to confirm that it is really a
dbase table prior to calling this.
--
Damon Pugh
Vision Software
http://www.vsnsoft.com
procedure DropProductionIndex(Name:String);
{Name parameter is filename without extension}
var
f: file of byte;
b: byte;
position:integer;
mdxName:String;
begin
{I+} {Handle I/O errors as Exceptions}
try
{The 28th byte of a dBase V header controls whether
the table has a production index or not. We can use
low-level file i/o to set this byte to 0. This byte
gets set back to 1 when an index gets created, so we
don't have to worry about reactivating the production
index.}
b:=0;
assignFile(f,Name+'.dbf');
FileMode:=of_Write+of_Share_Exclusive;
reset(f);
seek(f, 28);
write(f, b);
closefile(f);
{Now, delete the .MDX file}
{$IFDEF Win32}
if FileExists(Name+'.mdx') then DeleteFile(PChar(Name+'.mdx'));
{$ELSE}
if FileExists(Name+'.mdx') then DeleteFile(Name+'.mdx');
{$ENDIF}
except
raise EDataBaseError.Create(
'Could not delete index '+name+'.mdx. File may be in use. Make
sure all users are out of system and retry'
);
end;
end;