Re: Change directory of a Delphi Table

Posted by webmaster Guido on January 15, 2008

In Reply to Folder for Delphi database files posted by Willy P15898 on January 14, 2008

: I have written a Delphi program with a database file (DBF file) that is located in the same folder as where the executable file is running from, such as D:\Projects\Animals. I've sent this program to a friend, but he wants to put the program plus data files in a different folder, such as C:\Program Files\Animals, and then the program doesn't find its database.

: I want to make this run without setting up an "alias" for the database, because that's too complicated for most people who install my program. Any ideas?

It's a nice thing to set the property Active of TTable components to "True" at design time, because this allows you to see the contents of the DataControl components (such as DBGrid,...) at design time. But of course, then the property DatabaseName of the Table must be set to an existing "alias" or to an existing directory (folder).

In most cases you don't know if that alias or directory also exists on the computer where the program will be installed later on. If the location of the database file(s) is the same directory as the .exe file, then here's a solution:

At creation of the form, set the Table's DatabaseName property to the directory given by ExtractFileDir(Application.ExeName) and next open the Table. Example for one TTable component:

procedure TForm1.FormCreate(Sender: TObject);
  Table1.DatabaseName := ExtractFileDir(Application.ExeName);

This only works without errors if the Table's Active property is set to False at design time!
You have to check each Table.Active property before the final compilation of your program.

Related articles


Follow Ups