Create new Alias in Delphi

One of the things that often happens when you move the application that is made from one computer to another is the name of the alias. As you know, alias name used to indicate the location of the database used. If you move an application to another computer without creating a new alias will be shown a message "... .... ... ... Unknown database ..". 

To prevent this you can add a little code to make the alias directly (on the fly). So when the application is run, it will first be checked if the alias name is already used or not. If not the program will automatically create it. To create aliases you can use the procedure AddStandardAlias declared as follows:

AddStandardAlias(const Name, Path, DefaultDriver: String);

1.      <span id="more-213"></span>

2.       

3.      Parameter pertama (Name) adalah nama alias yang akan dibuat. Parameter kedua (Path) digunakan untuk menunjukkan letak database. Sedangkan DefaultDriver digunakan untuk menentukan jenis tabel. DefaultDriver harus berisi salah satu dari "Paradox," "DBASE," atau "ASCIIDRV". Di bawah ini merupakan contoh cara membuat alias dengan Path menunjuk pada folder db di bawah folder dimana file exe terletak.

4.      <pre lang="Delphi">procedure TForm1.FormCreate(Sender: TObject);

5.      var

6.         AliasBaru, Path : string;

7.      begin

8.         Path := ExtractFilePath (Application.ExeName) + 'db';

9.         AliasBaru := 'DBInventory'// nama alias yang akan dibuat

10.     // untuk mengecek apakah nama alias sudah ada atau belum

11.     if not Session.IsAlias(AliasBaru) then

12.     begin

13.        //jika belum ada maka tambahkan nama alias

14.        Session.AddStandardAlias (AliasBaru, Path, 'PARADOX');

15.     end

16.     else

17.     begin

18.        // jika sudah ada, hapus dulu

19.        Session.DeleteAlias(AliasBaru);

20.        Session.AddStandardAlias (AliasBaru, Path, 'PARADOX');

21.     end;

22.   

23.     // menyimpan nama alias pada file konfigurasi

24.     Session.SaveConfigFile;

25.  end;