Board index » delphi » Compacting an Access 2000 dbase with delphi

Compacting an Access 2000 dbase with delphi

Hi,

Is there a way I can compact an Access 2000 database programmatically
using delphi 6 ?
If not, is there any other way to do it without having the user to do it
manually by themselves ?

Regards.

 

Re:Compacting an Access 2000 dbase with delphi


Quote
Ricky wrote:
> Hi,

> Is there a way I can compact an Access 2000 database programmatically
> using delphi 6 ?
> If not, is there any other way to do it without having the user to do it
> manually by themselves ?

> Regards.

Here's what I cobbled together from various web sources.  Works in Delphi 7.
Just call the procedure from a buttonclick.

function SQLConfigDataSource(hwndParent: HWND; fRequest: WORD; lpszDriver: LPCSTR;
   lpszAttributes: LPCSTR): BOOL; stdcall; external'ODBCCP32.DLL';

procedure TDataModule1.DatabasePack;
var OldDB,NewDB: string;
const ODBC_ADD_DSN=1;
begin
   {compress and repair the database}
   with DataModule1 do begin
     Database1.Close;
     OldDB:='"c:\my documents\mydatabase.mdb"';
     NewDB:='"c:\my documents\mydatabase.mdb"';
     if not SQLConfigDataSource(0,ODBC_ADD_DSN,'Microsoft Access Driver (*.mdb)',
       PChar('REPAIR_DB='+OldDB+#0)) then
       MessageDlg('Repair not successful.',mtInformation,[mbOK],0)
       else MessageDlg('Repair successful.',mtInformation,[mbOK],0);
     if not SQLConfigDataSource(0,ODBC_ADD_DSN,'Microsoft Access Driver (*.mdb)',
       PChar('COMPACT_DB='+OldDB+' '+NewDB+' General'#0)) then
       MessageDlg('Compression not successful.',mtInformation,[mbOK],0)
       else MessageDlg('Compression successful.',mtInformation,[mbOK],0);
     Database1.Open;
   end;
end;

Re:Compacting an Access 2000 dbase with delphi


Quote
>Is there a way I can compact an Access 2000 database programmatically
>using delphi 6 ?
>If not, is there any other way to do it without having the user to do it
>manually by themselves ?

Import Type library (microsoft Jet and Replication Objects 2.5 library)

Use code like this:

Var
  fJetEngine : JetEngine;
Begin
  fJetEngine := CoJetEngine.Create;
  try
  fJetEngine.CompactDatabase(<Source connection>,<Destination connection>);
finally
  fJetEngine :=nil;
end;
End
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads