Link to home
Start Free TrialLog in
Avatar of m_adil
m_adil

asked on

Access DB...

How can I compact and repair an access DB?
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Hello

  here's a function to do that using the ADO technology

Function CompactAndRepair(sOldMDB : String; sNewMDB : String) : Boolean;
const
        sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var
        oJetEng   : JetEngine;
begin
        sOldMDB := sProvider + 'Data Source=' + sOldMDB;
        sNewMDB := sProvider + 'Data Source=' + sNewMDB;

        try
           oJetEng := CoJetEngine.Create;
           oJetEng.CompactDatabase(sOldMDB, sNewMDB);
           oJetEng := Nil;
           Result  := True;
        except
           oJetEng := Nil;
           Result  := False;
        end;
end;


Example :

if CompactAndRepair('e:\Old.mdb', 'e:\New.mdb') then
  ShowMessage('Successfully')
else
  ShowMessage('Error?');

if you would like to use DAO for old database like Access
97

uses ComObj;
procedure TForm1.Button1Click(Sender: TObject);
var
 dao: OLEVariant;
begin
 dao := CreateOleObject('DAO.DBEngine.35');
 dao.CompactDatabase('C:\My Documents\DB1.mdb',
'C:\My Documents\CompactedDB1.mdb');
end;

for more info look at this question
https://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20162957

Best regards
Mohammed Nasman
Avatar of m_adil
m_adil

ASKER

do i need to include something in uses clause ? its giving me error.
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of m_adil

ASKER

yes it worked. thanks alot.
also can u help me in this problem as well ?
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20242809

Thanks