?
Solved

Create BDE Alias on the fly

Posted on 2002-07-10
4
Medium Priority
?
1,380 Views
Last Modified: 2007-12-19
Dear Experts, please help !

     I am trying to make the BDE-alias on the fly. When I pressed the button for the first
time, the table1 can't open and I got this error message 'Unknown database alias : Temp_Alias',
but when I pressed the button again, the table1 can be opened.      
     Please help me to correct the problem.

Thanks from apin


ps. I'm using D5 Enterprise, Windows ME and paradox table.

{ main program }

unit uCreate_Alias;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids, Db, DBTables ;
type
  TForm1 = class(TForm)
    Open_Table_Btn: TButton;
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure Open_Table_BtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Uses CreateMyAlias ;

procedure TForm1.Open_Table_BtnClick(Sender: TObject);
begin
  CreateAlias('Temp_Alias','C:\Temp\','STANDARD',True) ;
  Table1.Close ;
  Table1.DatabaseName := 'Temp_Alias' ;
  Table1.TableName    := 'Coba.db' ;
  try
    Table1.Open ;
  except
    On E:Exception Do Showmessage('Error ' + E.Message) ;
  end ; // try
end;

end.



{  CreateMyAlias.pas }

unit CreateMyAlias  ;

interface

uses
  Windows, SysUtils, Messages, Dialogs, DBIPROCS, DBIERRS, DBTables      ;

Const
  CRLF = #13 + #10 ;
  ERR_ALIASDRIVERNOTFOUND = 'Specified drive do not exist ' ;
  ERR_ALIASALREADYEXISTS  = 'The alias (%s) already exists. ' + CRLF +
                            'would you like to reconfigure it ?!' ;
  ERR_ALIASINVALIDPARAM   = 'Invalid alias name' ;
  ERR_ALIASCLOSEBDE       = 'Error closing the BDE ' + CRLF +
                            'Please close all application and restart windows....' ;
  ERR_ALIASOPENBDE        = 'Error initializing BDE, Cannot create alias ' ;

procedure CreateAlias(sAlias, sPath, sDriver : String ; PersistentAlias : Boolean ) ;

implementation

procedure CreateAlias(sAlias, sPath, sDriver : String ; PersistentAlias : Boolean ) ;
ResourceString
  cfg_File = 'C:\Program Files\Common Files\Borland Shared\BDE\Idapi.cfg' ;
var
  dbenv : DbiEnv ;
  dbRes : DbiResult  ;
  Resp  : Word ;
begin
   // Set the BDE  Environment
   With dbEnv Do Begin
      StrPCopy(szWorkDir, sPath ) ;
      StrPCopy(szIniFile,cfg_file) ; // StrPCopy(szIniFile,'')
      bForceLocalInit := True ;
      StrPCopy(szLang,'') ;
      StrPCopy(szClientName,'dbClientName') ;
   End ; // With
   // Initialize BDE with the environment dbEnv
   IF DbiInit(@dbEnv) <> DbiERR_NONE Then
      raise Exception.Create(ERR_ALIASOPENBDE) ;

   // Add the specified alias
   IF sDriver = 'STANDARD' then
        dbRes := DbiAddAlias(NIL,pChar(sAlias),NIL,pChar('PATH:' + sPath),PersistenTAlias)
   Else dbRes := DbiAddAlias(NIL,pChar(sAlias),pChar(sDriver),pChar('PATH:' + sPath),PersistenTAlias) ;

   Case dbRes OF
     DbiERR_INVALIDPARAM : raise Exception.Create(ERR_ALIASINVALIDPARAM) ;
     DbiERR_NAMENOTUNIQUE : begin
        resp := MessageBox(0,PChar(Format(ERR_ALIASALREADYEXISTS,[sAlias])),'CreateAlias',MB_ICONSTOP + MB_YESNO) ;
        IF Resp = ID_YES Then Begin
           Check(DbiDeleteAlias(NIL,pChar(sAlias))) ;
           CreateAlias(sAlias,sPath,sDriver,PersistentAlias) ;
        end ; // if
     end ;
     DbiERR_UNKNOWNDRIVER : raise Exception.Create(ERR_ALIASDRIVERNOTFOUND) ;
   End ; // Case
   IF DbiExit <> DbiERR_NONE then raise Exception.Create(ERR_ALIASCLOSEBDE) ;
   // Else ShowMessage(sAlias + ' successfully created..... ') ;
end ;


end.
0
Comment
Question by:pc_melsa
[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
4 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 600 total points
ID: 7145396
Hello

  here's another way to make alias on the run time, it's easier and working fine with me

uses
  DBIProcs, DBITypes;

procedure AddBDEAlias(sAliasName, sAliasPath, sDBDriver: string);
  var
    h : hDBISes;
begin
  DBIInit(nil);
  DBIStartSession('Temp_Session', h, '');
  DBIAddAlias(nil, PChar(sAliasName), PChar(sDBDriver),
  PChar('PATH:' + sAliasPath), True );
  DBICloseSession(h);
  DBIExit;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddBDEAlias('Temp_alias1', 'C:\Temp', 'Paradox');
  Table1.DatabaseName := 'Temp_alias';
  Table1.TableName := 'test';
  Table1.Open;
end;

Best regards
Mohammed Nasman
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7145461
there is also a way using the default-session
and no dbi... routines

just searching in my paqs

meikl ;-)
0
 

Author Comment

by:pc_melsa
ID: 7149684
Thanks for Mr. Mohammad Nasman's comment !
But the procedure AddBDEAlias() provide no error checking.
0
 

Expert Comment

by:CleanupPing
ID: 9343073
pc_melsa:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 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