ADO Database 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 }