Board index » delphi » Compacting MS Access Database

Compacting MS Access Database

Hi!

Does anyone know how to compact MS Access Database from within a Delphi
program (using SQL - if possible)?

Thanx!

Darko Papi?
da...@zrcalo.si

 

Re:Compacting MS Access Database


Hi,

Use the CreateOleObject () function to create an OLE object on the jet
engine and then use it to compact or repair the database. Something like
this...

    try
        Screen.Cursor := crHourglass;
        TheEngine := CreateOLEObject ('DbEngine');
        TheEngine.CompactDatabase (edDbFile.Text, TmpFile, '', 0, '');
        DeleteFile (edDbFile.Text);
        StrPCopy (ZStr1, TmpFile);
        StrPCopy (ZStr2, edDbFile.Text);
        MoveFile (ZStr1, ZStr2);
    finally
        TheEngine := nil;
        Screen.Cursor := crDefault;
    end;

There are issues around the licence for DAO if you don't have MS
development tools, but this will work if you have that.

Quote
"Darko Papi?" wrote:
> Hi!

> Does anyone know how to compact MS Access Database from within a Delphi
> program (using SQL - if possible)?

> Thanx!

> Darko Papi?
> da...@zrcalo.si

--
Shaun Cunniffe
Dubloo Software

==============================================
Dubloo Software Pty Ltd
3 Yartoo Street
Hallett Cove SA  5158
Australia

e-mail:   sha...@dubloo.com.au
Phone:    +61 8 8322 3081
Mobile:   0412 800 565 (Int'l +61 412 800 565)
Fax:      +61 8 8460 1075
==============================================

Re:Compacting MS Access Database


My Component Set offers the CompactDatabase Method in the TDAOEngine
Component

see: www.gm-software.de

Re:Compacting MS Access Database


Here's a routine that will extract the database location from the
TDatabase then  compact an MS Access 97 database.

function TfrmMain.PackAccessDB(): boolean;
var varDBEngine: variant;
    slDBParams: TStringList;
    strDBPathName,
    strTempPathName: string;
begin
  result := true;
  slDBParams := TStringList.Create;
  try
    dbModule.Query1.Close;
    dbModule.Query2.Close;
    dbModule.ImportTable.Close;
    dbModule.Database1.Connected := false;
    dbModule.Database1.Session.GetAliasParams('Zipnews', slDBParams);
    strDBPathName := slDBParams.Values['DATABASE NAME'];
    strTempPathName := ExtractFilePath(strDBPathName);
    strTempPathName := strTempPathName + 'temp.mdb';
    varDBEngine := CreateOleObject('DAO.DBEngine.35');
    try
      varDBEngine.CompactDatabase(strDBPathName, strTempPathName);
      DeleteFile(strDBPathName);
      RenameFile(strTempPathName, strDBPathName);
    except
      result := false;
    end;
  finally
    slDBParams.Free;
    varDBEngine := Unassigned;
    dbModule.Database1.Connected := true;
  end;
end;

Quote
"Darko Papi?" wrote:

> Hi!

> Does anyone know how to compact MS Access Database from within a Delphi
> program (using SQL - if possible)?

> Thanx!

> Darko Papi?
> da...@zrcalo.si

--
================================================
Ray Porter
Applications Analyst Programmer
Administrative Information Services, UNC-CH
Phone: 966-5878
email: ray_por...@unc.edu
       dra...@email.unc.edu
Home Page: http://www.unc.edu/~dragon/

"Meddle not in the affairs of dragons,
for you are crunchy and taste good with ketchup."

Re:Compacting MS Access Database


A really simple way is to call access with the command line param to compact
the database.  It isn't slick coding by any means and is probably not as
quick as some of the other methods posted, but it is very simple and works
well in practice  (the few times i've played with it...)

Max

Quote
Darko Papi? <darko.pa...@uni-mb.si> wrote in message

news:7k60tp$cf7$1@planja.arnes.si...
Quote
> Hi!

> Does anyone know how to compact MS Access Database from within a Delphi
> program (using SQL - if possible)?

> Thanx!

> Darko Papi?
> da...@zrcalo.si

Re:Compacting MS Access Database


Type
  TDAO35Object = class
  Private
    FDBEngine : _DBEngine;
  Public
    Constructor Create;
    Destructor Destroy; Override;
  Published
    property DBEngine : _DBEngine read FDBEngine;
    Procedure Compact(OldBaza, NewBaza :String);
    Procedure Repair(Baza :String);
  End;

Constructor TDAO35Object.Create;
Begin
 FDBEngine := CoDBEngine.Create;
End;

Destructor TDAO35Object.Destroy;
Begin
 Inherited Destroy;
End;

Procedure TDAO35Object.Repair(Baza : String);
Begin
 FDBEngine.RepairDatabase(Baza);
End;

Procedure TDAO35Object.Compact(StaraBaza, NowaBaza : String);
Begin
 FDBEngine.CompactDatabase(OldBaza, NewBaza,
';LANGID=0x0415;CP=1250;COUNTRY=0', 32, '');
End;

Allen A. Maxwell napisa3(a) w wiadomo?ci:
<7kgdq8$ci...@news.xmission.com>...

Quote
>A really simple way is to call access with the command line param to
compact
>the database.  It isn't slick coding by any means and is probably not as
>quick as some of the other methods posted, but it is very simple and works
>well in practice  (the few times i've played with it...)

>Max

>Darko Papi? <darko.pa...@uni-mb.si> wrote in message
>news:7k60tp$cf7$1@planja.arnes.si...
>> Hi!

>> Does anyone know how to compact MS Access Database from within a Delphi
>> program (using SQL - if possible)?

>> Thanx!

>> Darko Papi?
>> da...@zrcalo.si

Other Threads