DelphiFAQ Home Search:

Compact and repair an Access database from Delphi


comments13 comments. Current rating: 5 stars (2 votes). Leave comments and/ or rate it.


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


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.


// Compact and repair an access database, it requires exclusive db access.
// argument DB = Path to Access Database
function CompactAndRepair(DB: string) : boolean;
  v: OLEvariant;
begin { CompactAndRepair }
  Result := True;
    v := CreateOLEObject('JRO.JetEngine');
      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');
      RenameFile(DB + 'x', DB)
      v := Unassigned
    end; { try }

    Result := False
  end; { try }
end; { CompactAndRepair }
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html


2006-01-13, 09:31:52
anonymous from Spain  
2006-02-05, 15:09:07
xp0027 from Mexico  
2006-09-25, 20:40:46
anonymous from Australia  
2007-10-05, 21:08:47
anonymous from Brazil  
Congratulation nice post
2008-04-18, 13:47:08
anonymous from Brazil  
Very good! Thank by help-me
2008-06-10, 03:47:40
Great Tool!
2012-08-10, 19:48:16
anonymous from Iran  
very very thanks .i use this code on project.
my name is alireza
2013-06-05, 07:49:46
anonymous from Sweden  
This is a necessary thing when using an access file for your db. It's like running chkdsk, Do it regularly or your clients will come shouting at you at least once a year because things get stuck or lost or both. And you get inexplicable db errors like ''wrong bookmark' when you know you have none.
2014-06-13, 16:53:18
anonymous from Poland  
Thank you very much! You are my personal hero and champion of the digital world!
2014-09-17, 03:47:49
anonymous from India  

I would like to suggest this application, which will repair your access data in trial version and see you the preview of recover data.

Try:- http://www.recovery..tool.html
2014-10-09, 23:45:44
MichaelEwart from India  
All you need to do just read and follow the instruction posted here - http://www.filesrep..pair.html
2016-06-26, 22:49:18
Thank you very much!
2016-09-05, 20:33:37
thank you so much ..



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: