BDE Aliases
by Ken Mayer [dBVIPS]

Creating a BDE Alias

When using local (.dbf) tables, a BDE alias does not point at a particular table or tables, but at a particular folder. On the contrary, a database in a SQL database is not specifically a folder: it can be, for example, a single file that contains a set of tables. This is why the folder concept for BDE Aliases is really only pertinent to local tables.

To work with tables in a path, use a BDE Alias. Below are the steps. The instructions given here are for dB2K, but very similar instructions can be used for the 16-bit version (which is what Visual dBASE 5.x uses).

A BDE Alias is used in two ways. If you are working with SQL Server databases (Interbase, Oracle, etc.), you use the BDE Alias as a pointer to that database. If you are using local tables, you use the BDE Alias to treat those local tables as if they were a SQL Server database.

Why would you want to use a BDE alias? The following are a few reasons, and there are probably others…

- Your user may want to move the data to a different directory. All that would be needed in that case is to change the BDE Alias’ pointer to the directory, and your application will run without any modification to the source code.
- You may need to deploy the application to a network and not know exactly where it will go. Again, changing the path in the BDE Alias is all that’s necessary.
- You may eventually move the data to a backend server such as Interbase, Oracle or one of the others… The best part of this is that you could design your whole application using local tables (if you have the alias set up), move everything to the server when you deploy it, and again no internal changes to your application would be necessary.

Setting Up An Alias

The first step is to actually create a BDE alias. To do this, you must bring up the BDE Administrator (there should be an icon in the folder for dB2K — double click on it) or go to the menu item Start|Settings|Control Panel and double-click the BDE Administrator icon.

In the BDE Administrator, click on the Databases tab. The treeview may appear as closed, so click on the “+”. Depending on your setup, you may or may not see a bunch of aliases already set in the Administrator. When you install dB2K several aliases are created for you, for example “Contax” and “dB2KSample”. Click on “Contax” if it is there.

On the right side of the screen you should see four entries:

> Type: Standard
> DEFAULT DRIVER: DBASE
> ENABLE BCD: false
> PATH: C:\Program Files\dB2K\Samples\contax\data

This is the important information for your alias. The “Type” should be “Standard” for local tables, and you may see (depending on your installation) other types if you check other aliases — these refer to the table’s server and/or the driver. If you are using dBASE tables, make sure the default driver is “DBASE”. You probably don’t need BCD, so don’t worry about it (check online help if you are curious). The “Path” is the actual location of the tables.

Note: If the “Type” is something other than “Standard”, there are a bunch of other options that may appear — you will need to examine those and have some idea what they mean for that specific server or driver.

To create a new alias, on the left half of the BDE Administrator screen, right click and select New… Select Standard unless you need to select, say, “Interbase” (note, if you are using local InterBase and dConnections, use the driver named “INTRBASE”), and click “OK”.

Set the Default Driver to “DBASE” (unless you are using PARADOX tables), and then click on the “…” button for the path. This will bring up a standard path dialog… select the path to your tables. You will probably want to give this a different name — on the left side of the screen, click on “STANDARD1” and type a new name there (like `MyAlias`).

At this point, you have the alias created in the BDE Administrator. However, you will want to “Apply” it (Ctrl+A) and then exit the Administrator. This saves the information to your`.CFG` file for the BDE.

If dB2K was up at the time you did this, you should exit it and restart it… otherwise the new alias will not appear.

Using a BDE Alias

To use a BDE Alias, you start dB2K, and in the Navigator select the Tables tab. In the Look In combobox, select your new alias. (If it is selected, the image will show a green “spot” in the database image.)

Instead of using the mouse, you could accomplish the same task by typing in the Command window:

| `In XDML:` `OPEN DATABASE MyApp` `SET DATABASE TO MyApp` `In OODML:` `d = new Database("MyApp")` `set database to MyApp // There is no OOP equivalent`

From this point on, while developing, you will be automatically using this alias. When that's the case, the name of your alias is always shown in the “Look in:” field when the Tables tab is selected. Sometimes when dB2K or an application crashes, the database closes and without knowing it, you're no longer using the database. So be sure that the name of the Alias is shown in the “Look in:” field.

Once a BDE Alias has been selected, if you drag'n drop a table from the Navigator unto a form in the Form designer, dBASE will automatically add the following lines of code to that form:

| `Without a BDE Alias:` `this.MY_TABLE1 = new QUERY()` `this.MY_TABLE1.parent = this` `with (this.MY_TABLE1)` `left = 100` `top = 100` `sql = 'select * from "My_Table.DBF"'` `active = true` `endwith` `With a BDE Alias:` `this.MYAPP1 = new DATABASE()` `this.MYAPP1.parent = this` `with (this.MYAPP1)` `left = 100` `top = 100` `databaseName = "MYAPP"` `active = true` `endwith` `this.MY_TABLE1 = new QUERY()` `this.MY_TABLE1.parent = this` `with (this.MY_TABLE1)` `left = 150` `top = 150` `database = form.myapp1` `sql = "select * from My_Table.dbf"` `active = true` `endwith`

A few suggestions

- Use the database object on datamodules, forms, or reports. This will require making sure that your query has the “database” entry pointing to the correct database object (the default value is “NULL”).
  Note: If you are starting a new project, this is automatically done for you — if you drag a table from the navigator, the database object will be brought to your design surface for the first table… and the database property will be automatically filled in for you.
- When referring to a table without using the database object, you must remember the database — this is done with syntax like `select * from :MyAlias:SomeTable` or `use :MyAlias:SomeTable`. Note the colons around the alias name. That is very important. All of this assumes that the database is open.
- If you work on multiple projects life could get a bit confusing because each database you open will stay open. If you quit dBASE, and come back to it later, it will re-open your databases. So, before you quit, you may want to issue a `CLOSE DATABASES` command (in the Command window). This will shut down all BDE Aliases, so that the next time you load dBASE they will not automatically be opened.

Note: The author would like to thank Steve Hawkins and David L. Stone, my proof-readers, for the improvements they brought to this text.