Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compacting An Access Database Through Delphi

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
Is there a way to compact an Access database like what happens when you exit Access itself?

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
I made a small library for this purpose :

Code:
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.

to have the JRO_TLB type file, just import the "Microsoft jet and Replication Objects Library" type library which is in fact msjro.dll .

here's an example how to use compactmdb.dll

Code:
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;

cheers,
Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top