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.

Open A Password Protected Access Database with TOpenDialog

Author: admin | Category: Database, Tips and Tricks

//This is how we can open a password protected access database with TOpenDialog
//For this example we need Button1 and Button2 (TButton), Edit1 and Edit2 (TEdit) and also AdoConnection1 (TAdoConnection)
//Button1 is used to open access database file
//Edit2 is used for access database password
//Button2 is used to open connection
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    ADOConnection1: TADOConnection;
    Edit2: TEdit;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    Edit1.Text := OpenDialog1.FileName;
  end;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
    ADOConnection1.Close;
    ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data ' +
      'Source='+Edit1.Text+';Mode=ReadWrite;Persist ' +
      'Security Info=False;Jet OLEDB:Database Password='+Edit2.Text;
    Edit1.Text := OpenDialog1.FileName;
    try
      ADOConnection1.Open;
      if ADOConnection1.Connected = true then
        showmessage('Connected Sccessfull to '+Edit1.Text);
    except
      showmessage('Connection to '+Edit1.Text+' failed');
    end
end;
 
end.
 

Tags: ,