Author: Lou Adler
How to compact and repair MS Access 2000 (Jet Engine 4) during run time using
Delphi 5?
Answer:
Usually the size of MS Access keep growing fast by time because of it’s internal
caching and temporary buffering, which in over whole effect the performance, space
required for storing, and backing-up (if needed). The solution is to compact it
from Access menus (Tools – Database Utilities – Compact and Repair Database) or to
do that from inside your Delphi application.
1 2 function CompactAndRepair(sOldMDB: string; sNewMDB: string): Boolean;
3 const4 sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
5 var6 oJetEng: JetEngine;
7 begin8 sOldMDB := sProvider + 'Data Source=' + sOldMDB;
9 sNewMDB := sProvider + 'Data Source=' + sNewMDB;
10 11 try12 oJetEng := CoJetEngine.Create;
13 oJetEng.CompactDatabase(sOldMDB, sNewMDB);
14 oJetEng := nil;
15 Result := True;
16 except17 oJetEng := nil;
18 Result := False;
19 end;
20 end;
21 22 //Example :23 24 if CompactAndRepair('e:\Old.mdb', 'e:\New.mdb') then25 ShowMessage('Successfully')
26 else27 ShowMessage('Error…');
Important Notes:
Include the JRO_TLB unit in your uses clause.
Nobody should use or open the database during compacting.
If the compiler gives you an error on the JRO_TLB unit follow these steps:
Using the Delphi IDE go to Project – Import Type Library.
Scroll down until you reach “Microsoft Jet and Replication Objects 2.1 Library”.
Click on Install button.
Recompile a gain.