Solved

Protect access database with password

Posted on 2006-06-27
6
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 125 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now