Solved

Change Access Databse Password

Posted on 2006-07-23
3
303 Views
Last Modified: 2012-05-05
Hi Experts
How I can Change MSAccess Password In Delphi?
0
Comment
Question by:mahdiparak
3 Comments
 
LVL 9

Expert Comment

by:sun4sunday
ID: 17163162
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21080111.html

copying the solution

var
  db1, db2, fp1, fp2 : string;
begin
  db1:='c:\test\test.mdb';    // original flename
  db2:='c:\test\new.mdb';   // new filename
  fp1:='password';        //original file password
  fp2:='password';       //new file password
  try
    StatusBar.SimpleText:='Loading driver...';
    v := CreateOLEObject('JRO.JetEngine');
    try
      v.CompactDatabase('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + db1 +
                        ';Persist Security Info=False' + fp1,
                        'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + db2 +
                        'x; Jet OLEDB:Engine type=5'+ fp2);
    except
    on E:Exception do
           ShowMessage(E.Message);
    end;
  finally
    v := Unassigned;
    StatusBar.SimpleText:='Done.';
  end;
end;

sun4sunday
0
 
LVL 28

Expert Comment

by:TName
ID: 17163173

Hi, are you using ADO? Try

ALTER DATABASE PASSWORD MyNewPassword MyOldPassword

e.g.:
ADOCommand1.CommandText := 'ALTER DATABASE PASSWORD MyNewPassword MyOldPassword';
ADOCommand1.Execute;

see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnacc2k/html/acadvsql.asp

and you might want to have a look at
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21900292.html

0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17163341
Another alternative example.

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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