whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

48 Visitors Online


 
...Read an Access DB using ADO?
Autor: Michael Casse
[ Print tip ]  

Tip Rating (10):  
     


// Read an MS-ACCESS Database (any versions) using ADO
// Verify if it is an ACCESS MDB
// Components Needed on the Application Form are:
// TADOtable,TDataSource,TOpenDialog,TDBGrid,TBitBtn.
// Date : 14/01/2002
// Author: Michael Casse.

unit uMain;

interface

uses
  
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, ADODB, Grids, DBGrids, ExtCtrls, DBCtrls, StdCtrls, Buttons;

type
  
TfrmMain = class(TForm)
    DSUsers: TDataSource;
    DBGridUsers: TDBGrid;
    BitBtn1: TBitBtn;
    OpenDialog1: TOpenDialog;
    TUsers: TADOTable;
    procedure FormCreate(Sender: TObject);
    procedure ValidateAccessDB;
    function CheckIfAccessDB(lDBPathName: string): boolean;
  private
    
{ Private declarations }
  
public
    
{ Public declarations }
  
end;

var
  
frmMain: TfrmMain;
const
  
DBNAME = 'ADODemo.MDB';
  DBPASSWORD = '123'; // Access DB Password Protected

implementation

{$R *.DFM}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  
validateAccessDB;
end;

procedure TfrmMain.ValidateAccessDB;
var
  
lDBpathName : String;
  lDBcheck : boolean;
begin
  if 
FileExists(ExtractFileDir(Application.ExeName) + '\' + DBNAME) then
    
lDBPathName := ExtractFileDir(Application.ExeName) + '\' + DBNAME
  else if OpenDialog1.Execute then
    
// Set the OpenDialog Filter for ADOdemo.mdb only
    
lDBPathName := OpenDialog1.FileName;

  lDBCheck := False;
  if Trim(lDBPathName) <> '' then
    
lDBCheck := CheckIfAccessDB(lDBPathName);

  if lDBCheck = True then
  begin
    
// ADO Connection String to the MS-ACCESS DB
    
TUsers.ConnectionString :=
      'Provider=Microsoft.Jet.OLEDB.4.0;' +
      'Data Source=' + lDBPathName + ';' +
      'Persist Security Info=False;' +
      'Jet OLEDB:Database Password=' + DBPASSWORD;
    TUsers.TableName := 'Users';
    TUsers.Active := True;
  end
  else
    
frmMain.Free;
end;

// Check if it is a valid ACCESS DB File Before opening it.

function TfrmMain.CheckIfAccessDB(lDBPathName: string): Boolean;
var
  
UnTypedFile: file of byte;
  Buffer: array[0..19] of byte;
  NumRecsRead: Integer;
  i: Integer;
  MyString: string;
begin
  
AssignFile(UnTypedFile, lDBPathName);
  reset(UnTypedFile);
  BlockRead(UnTypedFile, Buffer, High(Buffer), NumRecsRead);
  CloseFile(UnTypedFile);
  for i := 1 to High(Buffer) do
    
MyString := MyString + Trim(Chr(Ord(Buffer[i])));
  Result := False;
  if Mystring = 'StandardJetDB' then
    
Result := True;
  if Result = False then
    
MessageDlg('Invalid Access Database', mtInformation, [mbOK], 0);
end;

end.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners