DelphiFAQ Home Search:
General :: Programming :: Delphi :: Database
Database development with Delphi. Includes issues with BDE, ADO and InterBase.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Only the 40 most recently viewed articles are shown.
You can see the full list here.

Featured Article

Compact and repair an Access database from Delphi

Question:

I need to rebuild a Microsoft Access database from Delphi. How can I do this?

Answer:

The code below creates a JetEngine object and uses its CompactDatabase() method to save the compacted db under a new filename. Obviously it needs exclusive access for this. Afterwards the db files are swapped and available for everyone again.

uses
   ComObj;
 
 // Compact and repair an access database, it requires exclusive db access.
// argument DB = Path to Access Database
function CompactAndRepair(DB: string) : boolean;
 var
   v: OLEvariant;
 begin { CompactAndRepair }
   Result := True;
   try
     v := CreateOLEObject('JRO.JetEngine');
     try
       v.CompactDatabase('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DB,
                         'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DB +
                         'x;Jet OLEDB:Engine type=5');
       DeleteFile(DB);
       RenameFile(DB + 'x', DB)
     finally
       v := Unassigned
     end; { try }
 
   except
     Result := False
   end; { try }
 end; { CompactAndRepair }
 
You don't like the formatting? Check out SourceCoder then!
Generated 0:01:36 on May 26, 2017