Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Compact and Repair Access Database

Author: admin | Category: Database, Tips and Tricks

//This is how we can Compact and Repair Access Database
//The CompactAndRepair Function originally wrote by Dan Towers
//For this Compact and Repair Access Database example we use an OpenDialog1 (TOpenDialog) Component
//and Button1 and Button1 (TButton)
//Click Button1 to open the .mdb database then Click button1 to Compact and Repair those Access Database
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    stringdb:string;
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses
  ComObj;
 
{$R *.dfm}
 
function CompactAndRepair(DB: string): Boolean; {DB = Path to Access Database}
var
  v: OLEvariant;
begin
  Result := True;
  try
    v := CreateOLEObject('JRO.JetEngine');
    try
      V.CompactDatabase('Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+DB,
                        'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+DB+'x;Jet OLEDB:Engine Type=5');
      DeleteFile(DB);
      RenameFile(DB+'x',DB);
    finally
      V := Unassigned;
    end;
  except
    Result := False;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    stringdb := OpenDialog1.FileName;
  end;
 
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  result:boolean;
begin
  result := CompactAndRepair(stringdb);
  if result = true then ShowMessage('Compact and Repair Succeed')
  else ShowMessage('Compact and Repair Failed!!');
end;
 
end.

Tags: ,