Mega Code Archive

 
Categories / Delphi / ADO Database
 

Compact and Repair MS Access 2000

Title: Compact and Repair MS Access 2000. Question: 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 its 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. Function CompactAndRepair(sOldMDB : String; sNewMDB : String) : Boolean; const sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;'; var oJetEng : JetEngine; begin sOldMDB := sProvider + 'Data Source=' + sOldMDB; sNewMDB := sProvider + 'Data Source=' + sNewMDB; try oJetEng := CoJetEngine.Create; oJetEng.CompactDatabase(sOldMDB, sNewMDB); oJetEng := Nil; Result := True; except oJetEng := Nil; Result := False; end; end; Example : if CompactAndRepair('e:\Old.mdb', 'e:\New.mdb') then ShowMessage('Successfully') else ShowMessage('Error'); Important Notes: 1- Include the JRO_TLB unit in your uses clause. 2- Nobody should use or open the database during compacting. 3- If the compiler gives you an error on the JRO_TLB unit follow these steps: a) Using the Delphi IDE go to Project Import Type Library. b) Scroll down until you reach Microsoft Jet and Replication Objects 2.1 Library. c) Click on Install button. d) Recompile a gain. Abdulaziz Jasser