Databases - Delphi Data Modules

I place all database connections in Data Modules. They work great and are easy to reuse (except in web servers.)

KADao Overview | IB Components Overview | Basic Info | Creation Order | Webservers | Transportable Code | Inheritance | Delphi Version

IB Components Overview

IB components are used to access Interbase and Firebird databases. These databases are transaction driven.

For normal database access, a datamodule should contain

KADao Overview

When using KADao to interface with MS Access tables (via ADO), I create a separate datamodule for each *.mdb file.

For normal database access, a datamodule should contain

KADao table components work fine, they are much faster than Interbase table components.

Basic Info

To create a new datamodule

Creation Order

In some cases (maybe *most* cases), you will want to control the order used to create data access components when your program starts. For instance, you must connect a database component to the database before a table using that component can be made active.

When KADaoDatabase1 is the first component added to a datamodule, it is automatically created first.

To control the creation order, from the menu, select

or right click the datamodule and select


Basically, Delphi does not allow you to include TDataModule's in web servers (TWebModule) via (Perhaps there is a problem with multithreading.)

The following works fine

Transportable Code


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 - I provide an ini file for that purpose.


It is fairly simple to inherit from most components ... but datamodules are different. This is because the Delphi IDE provides a special editor for datamodules, and, if you simply use normal inheritance, Delphi adds Color, Fonts and other TForms properties to the TDatamodule *.dfm file ... and that, of course, causes all kinds of problems (because those properties are not defined for datamodules).

According to the help, it is possible to add datamodules to the Repository. Once that is done, then you can inherit from those and everything should be ok.

The only problem is that the instructions on adding datamodules to the repository are almost correct. They say

2. .. For a .. data module, right-click the item and choose Add To Repository.
The more correct instructions
2. .. For a .. data module, make sure that the datamodule (not the code) is displayed (use F12 to toggle between the 2 displays) then right-click the datamodule and choose Add To Repository.

When creating a new datamodule (in Delphi 5), select File / New... / Data Modules and be sure to select Inherit (Copy is the default) before clicking OK.

NOTE: For Delphi 6, use File / New / Other... / Data Modules

Doing it by hand

To manually refactor an existing datamodule is simple

Using an Existing Data Module

If you just wish to USE an existing data module based on an inherited data module, then

This example provides 2 text fields

It is also possible to use Project / Add to Project... to add the DataModules ... however, you MUST add Basic_Firebird_DataModule first and then one (or more) of the other data modules. This method adds the DataModules to the *.dpr file.

Delphi Version

The unit that defines TDataModule is version dependent In addition, the entire uses clause is different.

Code created in Delphi 5 compiles ok in Delphi 6, but to get Delphi 6 code to compile in Delphi 5, the Forms unit needs to be included. For instance, this example can be used to create compatible code It is also possible to always include Forms, but it may produce slightly smaller code to omit it ... particularly when datamodules are also used in webmodules.

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