Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Protect access database with password

Posted on 2006-06-27
6
291 Views
Last Modified: 2010-04-16
Hi,

I need to protect a access database by password.
The user select the mdb file and my delphi code need to protect it by defined password.

How can I do that please ?

Jean-Luc !
0
Comment
Question by:Alphomega
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
atul_parmar earned 125 total points
ID: 16991025
Import a Microsoft DAO type library. add it to your uses clause and then ...

var
  FEngine, FDB : OleVariant;
  DatabaseName, Password : String;
begin
  DatabaseName := 'c:\db1.mdb';
  Password := 'mypwd';
  FEngine := CoDBEngine.Create;
  FDB := FEngine.OpenDatabase(DatabaseName, True);
  FDB.NewPassword(#0, Password);
  FDB.Close;
  FDB := Unassigned;
  FEngine := Unassigned;
end;
0
 

Author Comment

by:Alphomega
ID: 16991545
Thanks for your code but when I'm trying to open a database, I get the error:

FDB := FEngine. OpenDatabase(DatabaseName, True);

---------------------------
Project1
---------------------------
Unrecognized database format 'c:\bd.mdb'
---------------------------
OK  
---------------------------

It's a real access 97 database ...
import typelib is
Type Lib: C:\Program Files\Fichiers communs\Microsoft Shared\DAO\DAO350.DLL

:-(

0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16991615
0
 
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 125 total points
ID: 16992735
Example using ADO that shows how to:

- set the initial password
- open a password protected db
- change a db password
- clear the  password from the db


Regards,
Russell

-----

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComObj, ActiveX; // Include Variants for D6 and up

type
  TForm1         = class(TForm)
     Button1:    TButton;
     procedure   Button1Click(Sender: TObject);
  private
     // Private declarations
  public
     // Public declarations
  end;

var
  Form1:         TForm1;

implementation
{$R *.DFM}

const
  adModeShareExclusive =  12;

function UpdateDBPassword(FileMDB: String; OldPassword, NewPassword: String): Boolean;
var  ovConn:        OleVariant;
begin

  // Create ADO connection
  ovConn:=CreateOleObject('ADODB.Connection');
  try
     // Set exclusive mode
     ovConn.Mode:=adModeShareExclusive;
     try
        // Open the database
        ovConn.Open(Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source="%s";Jet OLEDB:Database Password="%s"', [FileMDB, OldPassword]));
        // Update the password
        ovConn.Execute(Format('ALTER DATABASE PASSWORD `%s` `%s`', [NewPassword, OldPassword]));
        // Close the connection
        ovConn.Close;
        // Success
        result:=True;
     except
        // Exception, return failure
        result:=False;
     end;
  finally
     // Clear the object reference
     ovConn:=Unassigned;
  end;

end;

function SetDBPassword(FileMDB: String; Password: String): Boolean;
var  ovConn:        OleVariant;
begin

  // Create ADO connection
  ovConn:=CreateOleObject('ADODB.Connection');
  try
     // Set exclusive mode
     ovConn.Mode:=adModeShareExclusive;
     try
        // Open the database
        ovConn.Open(Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source="%s"', [FileMDB]));
        // Update the password
        ovConn.Execute(Format('ALTER DATABASE PASSWORD `%s` NULL', [Password]));
        // Close the connection
        ovConn.Close;
        // Success
        result:=True;
     except
        // Exception, return failure
        result:=False;
     end;
  finally
     // Clear the object reference
     ovConn:=Unassigned;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  // Set initial password
  if SetDBPassword('c:\db1.mdb', 'Testing') then
  begin
     // Update password
     UpdateDBPassword('c:\db1.mdb', 'Testing', 'New Password');
     // Remove the password
     UpdateDBPassword('c:\db1.mdb', 'New Password', '');
  end;

end;

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Communication Between RC4 Delphi <-> PHP 3 123
Delphi Yen format 3 45
Delphi: making a BW image transparent 10 61
Delphi: barcode reading on android platform 1 51
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question