Databases - Delphi/KADao Runtime Path


In each case, the path to the databases might be different (unless you always require the same drive and path on every system).

In general, I prefer portable code that is able to determine the appropriate path at run time. In general, this is no big deal - just read an *.ini file and set the parameters. However, a problem occurs when KADaoDatabase1.Connected is True when the program compiles.

To develop applications, it is useful to set KADaoDatabase1.Connected to True (so you can see the available tables, fields, and example data). However, if that is done, and you try to run the application on another system, you will get a "file not found" error when the application starts.

Background | Architectural Goals | Datamodules | Reading the ini File |


I have worked places where the database location was hardcoded in the application - in order to run the code against a test database, you had to manually tweak the code. As a result, you could never test the deliverable code until it was running against production data. (Of course, the code was delivered at least once without making the required last minute change.)

There are several ways to configure an application at runtime

Architectural Goals

These are the main architectural goals


In my design, each database (*.mdb file) is associated with one or more datamodules.

Reading the ini File

Because forms reference objects (fields) defined in datamodules, the datamodules must be created first in the Delphi project (*.dpr) files followed by the form. When it is created, the datamodule is automatically connected to the database specified in TKADaoDatabase.Database. Since this occurs before the form is created, the ini file must be read elsewhere.

While it is possible to read the ini file from each datamodule, it makes more sense to create a special datamodule just to read the common ini file.

Of course, this datamodule must be created before any others.

To control the datamodule and form creation order, either

Because of how TKADaoDatabase is implemented, this architecture


TKADaoDatabase has an undocumented OnBeforeConnect event. While it can not be used to abort the connection (what I originally wanted), it can be used to read the correct value from the ini file.

When each datamodule is created, the TKADaoDatabase Set_Active method is called before any other event I can find. Set_Active eventually calls OnBeforeConnect. After that returns, Set_Active checks for the existence of the file. Eventually, DataModuleCreate is called (way too late to have an effect).

For this to work

Sample *.ini File

This is a typical Jet_Location.ini file with 4 databases (.mdb files) - the "active" section specifies which set of database files are actually used. This file contains 3 similar sections


May be able to include several ini files so that the application's data can also be read and the database location ini file (must be a separate file) can be set to another location by a command line parameter.

I tried this, but it did not work because DataModuleCreate is called after the values stored in the DFM file are set.

Related Commands

SmartOpen controls looking for missing databases in the current directory and is not useful in this case.

Author: Robert Clemenzi -
URL: http:// / user / clemenzi / technical / Databases / Delphi / KADao / RuntimePath.html