PaidtheUmpire
Programmer
Is there a way to compact an Access database like what happens when you exit Access itself?
Delphi, Delphi, Delphi. Oi! Oi! Oi!
Delphi, Delphi, Delphi. Oi! Oi! Oi!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
library compactmdb;
uses
Sysutils,
JRO_TLB;
{$R *.res}
procedure databasecompact(sdbname, FdbConn : shortstring; out FResult : PChar); stdcall;
var
JE : TJetEngine;
sdbTemp : Widestring;
sdbTempConn : Widestring;
sdbConn : Widestring;
begin
sdbTemp:=ExtractFileDir(sdbName)+'tmp_'+ExtractFileName(sdbName);
sdbTempConn:=Format(FdbConn,[sdbtemp]);
sdbConn:=Format(FdbConn,[sdbName]);
if FileExists(sdbTemp) then DeleteFile(sdbTemp);
JE:= TJetEngine.Create(nil);
try
try
JE.CompactDatabase(sdbConn, sdbTempConn);
DeleteFile(sdbName);
RenameFile(sdbTemp, sdbName);
FResult:='';
except
on E:Exception do
begin
FResult:=PChar(E.Message);
end;
end;
finally
JE.Free;
end;
end;
exports databasecompact;
end.
function CompactMDBDatabase : string;
type TDatabaseCompactproc = procedure(sdbname, FdbConn : shortstring; out FResult : PChar); stdcall;// typecast compactmdb function
var Handle_cmdb : THandle;
proc_cmdb : TDatabaseCompactproc;
Fres : PChar;
begin
Result:='No success';
if FileExists(InstallDir+FILE_COMPACTMDB) then
begin
// try to load library
Handle_cmdb:=LoadLibrary(PChar(InstallDir+FILE_COMPACTMDB));
if Handle_cmdb <> 0 then
try
// get compactmdb function pointer
Proc_cmdb:=GetProcAddress(Handle_cmdb, PROC_COMPACTMDB);
// call compactmdb function
Proc_cmdb(SourceMDB, 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s', Fres);
Result:=string(FRes);
// destroy dll resources
finally
FreeLibrary(Handle_cmdb);
end;
end;
end;