Delphi .NET (2) Database (71) Delphi IDE (89) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
Compact and repair an Access database from Delphi
6 comments. Current rating: (2 votes). Leave comments and/ or rate it.
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;
function CompactAndRepair(DB: string) : boolean;
var
v: OLEvariant;
begin
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;
except
Result := False
end;
end;
| |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
|
anonymous from Spain
|
 |
|
|
|
|
xp0027 from Mexico
|
 |
|
|
10x!!!!!!!
|
|
anonymous from Australia
|
|
|
|
huh?
|
|
anonymous from Brazil
|
|
|
|
Congratulation nice post
|
|
anonymous from Brazil
|
|
|
|
Very good! Thank by help-me
|
|
|
|
|
Great Tool!
|
|