Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Protect access database with password

Posted on 2006-06-27
6
Medium Priority
?
316 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
[X]
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
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
atul_parmar earned 500 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 26

Assisted Solution

by:Russell Libby
Russell Libby earned 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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