Mega Code Archive

 
Categories / Delphi / ADO Database
 

How to circumvent the index not found Exception (dBASE table)

Title: How to circumvent the index not found Exception (dBASE table) unit Fixit; interface uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids; type TForm1 = class(TForm) Table1: TTable; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} const TheTableDir = 'c:\temp\'; TheTableName = 'animals.dbf'; procedure RemoveMDXByte(dbFile: string); { This procedure accepts a DBF file as a parameter. It will patch} { the DBF header, so that it no longer requires the MDX file } const Value: Byte = 0; var F: file of byte; begin AssignFile(F, dbFile); Reset(F); Seek(F, 28); Write(F, Value); CloseFile(F); end; procedure TForm1.Button1Click(Sender: TObject); { This procedure is called in response to a button click. It } { attempts to open a table, and, if it can't find the .MDX file, } { it patches the DBF file and re-execute the procedure to } { re-open the table without the MDX } begin try { set the directory for the table } Table1.DatabaseName := ThheTableDir; { set the table name } Table1.TableName := TheTableName; { attempt to open the table } Table1.Open; except on E: EDBEngineError do { The following message indicates the MDX wasn't found: } if Pos('Index does not exist. File', E.Message) 0 then begin { Tell user what's going on. } MessageDlg('MDX file not found.Attempting to Open without Index.', mtWarning, [mbOK], 0); { remove the MDX byte from the table header } RemoveMDXByte(TheTableDir + TheTableName); { Send the button a message to make it think it was } { pressed again. Doing so will cause this procedure to } { execute again, and the table will be opened without } { the MDX } PostMessage(Button1.Handle, cn_Command, bn_Clicked, 0); end; end; end; end.