home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / full / delphi / DELPHI16 / TECHINFO / DELPHI / TIS / TI2711.FX < prev    next >
Encoding:
Text File  |  1995-08-24  |  3.9 KB  |  183 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2711
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  July 14, 1995                            PAGE  :  1/3
  11.  
  12.     TITLE  :  How to Circumvent the "index not found" Exception.
  13.  
  14.  
  15.  
  16.  
  17. Q: How do I open a dBASE table without the required MDX file?
  18.    I keep getting an "Index not found..." exception.
  19.  
  20. A: When you create a dBASE table with a production index (MDX), a 
  21.    special byte is set in the header of the DBF file.  When you
  22.    subsequently attempt to re-open the table, the dBASE driver
  23.    will read that special byte, and if it is set, it will also
  24.    attempt to open the MDX file.  When the MDX file cannot be
  25.    opened, an exception is raised.
  26.  
  27.    To work around this problem, you need to reset the byte (byte
  28.    28 decimal) in the DBF file that causes the MDX dependency
  29.    to zero.
  30.  
  31.    The following unit is a simple example of how to handle the 
  32.    exeption on the table open, reset the byte in the DBF file,
  33.    and re-open the table.
  34.  
  35. unit Fixit;
  36.  
  37. interface
  38.  
  39. uses
  40.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, 
  41.   Controls, Forms, Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids;
  42.  
  43. type
  44.   TForm1 = class(TForm)
  45.     Table1: TTable;
  46.     Button1: TButton;
  47.     procedure Button1Click(Sender: TObject);
  48.   private
  49.     { Private declarations }
  50.   public
  51.     { Public declarations }
  52.   end;
  53.  
  54. var
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2711
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  July 14, 1995                            PAGE  :  2/3
  72.  
  73.     TITLE  :  How to Circumvent the "index not found" Exception.
  74.  
  75.  
  76.  
  77.  
  78.   Form1: TForm1;
  79.  
  80. implementation
  81.  
  82. {$R *.DFM}
  83.  
  84. const
  85.   TheTableDir = 'c:\temp\';
  86.   TheTableName = 'animals.dbf';
  87.  
  88. procedure RemoveMDXByte(dbFile: String);
  89. { This procedure accepts a DBF file as a parameter.  It will patch}
  90. { the DBF header, so that it no longer requires the MDX file }
  91. const
  92.   Value: Byte = 0;
  93. var
  94.   F: File of byte;
  95. begin
  96.   AssignFile(F, dbFile);
  97.   Reset(F);
  98.   Seek(F, 28);
  99.   Write(F, Value);
  100.   CloseFile(F);
  101. end;
  102.  
  103. procedure TForm1.Button1Click(Sender: TObject);
  104. { This procedure is called in response to a button click.  It    }
  105. { attempts to open a table, and, if it can't find the .MDX file, }
  106. { it patches the DBF file and re-execute the procedure to        }
  107. { re-open the table without the MDX  }
  108. begin
  109.   try
  110.     { set the directory for the table }
  111.     Table1.DatabaseName := TheTableDir;
  112.     { set the table name }
  113.     Table1.TableName := TheTableName;
  114.     { attempt to open the table }
  115.     Table1.Open;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2711
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  July 14, 1995                            PAGE  :  3/3
  133.  
  134.     TITLE  :  How to Circumvent the "index not found" Exception.
  135.  
  136.  
  137.  
  138.  
  139.   except
  140.     on E:EDBEngineError do
  141.       { The following message indicates the MDX wasn't found: }
  142.       if Pos('Index does not exist. File', E.Message)>0 then begin
  143.         { Tell user what's going on. }
  144.         MessageDlg('MDX file not found.  Attempting to open 
  145.                     without index.', mtWarning, [mbOk], 0);
  146.         { remove the MDX byte from the table header }
  147.         RemoveMDXByte(TheTableDir + TheTableName);
  148.         { Send the button a message to make it think it was }
  149.         { pressed again.  Doing so will cause this procedure to }
  150.         { execute again, and the table will be opened without }
  151.         { the MDX }
  152.         PostMessage(Button1.Handle, cn_Command, bn_Clicked, 0);
  153.       end;
  154.   end;
  155. end;
  156.  
  157. end.
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. DISCLAIMER: You have the right to use this technical information
  180. subject to the terms of the No-Nonsense License Statement that
  181. you received with the Borland product to which this information
  182. pertains.
  183.