Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open acces database with bde alias

Status
Not open for further replies.

GeoEnaru

Programmer
Mar 15, 2004
7
RO
I'm trying to open an Acces Database using bde alias, but it raises an error: "[Microsoft][ODBC Microsoft Access Driver]Invalid connection string attribute DATABASE". I don't know what I did wrong. Pls help
 
here's some code I used in an early project.

Code:
procedure EnsureDataPath(AODBC, ADescription, ADatabase, AAlias: String);
const
  ODBCKey = '\SOFTWARE\ODBC\ODBC.INI';
var
  s : String;
  l : TStringList;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    if not OpenKey(ODBCKey + '\' + AODBC, False) then  // ODBC entry has not been set up
    begin
      OpenKey(ODBCKey + '\' + AODBC + '\Engines\Jet', True);
      WriteString('ImplicitCommitSync', '');
      WriteInteger('MaxBufferSize', 2048);
      WriteInteger('PageTimeout', 5);
      WriteInteger('Threads', 3);
      WriteString('UserCommitSync', 'Yes');
      CloseKey;
      OpenKey(ODBCKey + '\ODBC Data Sources', True);
      WriteString(AODBC, 'Microsoft Access Driver (*.mdb)');
      CloseKey;
      OpenKey(ODBCKey + '\' + AODBC, True);  // create key
      s := '\System';
      if UpperCase(GetEnvironmentVariable('OS')) = 'WINDOWS_NT' then
        s := s + '32';
      WriteString('Driver', GetEnvironmentVariable('WINDIR') + s + '\odbcjt32.dll');
      WriteInteger('DriverId', 25);
      WriteString('FIL', 'MS Access;');
      WriteInteger('SafeTransactions', 0);
      WriteString('UID', '');
      WriteString('Description', ADescription);
    end;
    WriteString('DBQ', ExtractFilePath(ParamStr(0)) + ADatabase + '.mdb');
    CloseKey;
  finally
    Free;
  end;
  if not Session.IsAlias(AAlias) then  { BDE Alias doesn't exist }
  begin
    l := TStringList.Create;
    try
      l.Append('DATABASE NAME=');
      l.Append('USER NAME=');
      l.Append('ODBC DSN=' + AODBC);
      l.Append('OPEN MODE=READ/WRITE');
      l.Append('SCHEMA CACHE SIZE=8');
      l.Append('SQLQRYMODE=');
      l.Append('LANGDRIVER=');
      l.Append('SQLPASSTHRU MODE=SHARED AUTOCOMMIT');
      l.Append('SCHEMA CACHE TIME=-1');
      l.Append('MAX ROWS=-1');
      l.Append('BATCH COUNT=200');
      l.Append('ENABLE SCHEMA CACHE=FALSE');
      l.Append('SCHEMA CACHE DIR=');
      l.Append('ENABLE BCD=FALSE');
      l.Append('ROWSET SIZE=20');
      l.Append('BLOBS TO CACHE=64');
      Session.AddAlias(AAlias, 'Microsoft Access Driver (*.mdb)', l);
    finally
      l.Free;
    end;
  end;
end;

What this procedure does is ensure you can specify an alias, and have it point to your database. Parameters are:

AODBC - The ODBC filename that's created (in \Program Files\Common Files\ODBC\Data Sources)
ADescription - The description of the ODBC connection
ADatabase - the full pathname to your Access database
AAlias - The BDE alias name.

Call this procedure first, and then assign the alias to your components.
 
Oops - as usual, I should have spent another 10 seconds reviewing the code.

Please change the
Code:
WriteString('DBQ', ExtractFilePath(ParamStr(0)) + ADatabase + '.mdb');
line to
Code:
WriteString('DBQ', ADatabase);

The line as it is forces your access database to be in the same folder as your executable.
 
It's working!!! Thank you very much, I owe you a beer!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top