1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Data Modules
How to use the TDataModule class - central location for collecting and encapsulating data access objects, their properties, events and code.
 More of this Feature
Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
 Related Resources
• free DB Course.TOC
• more Database articles

When developing simple database applications (with one or two forms) it is quite usual to place all the data-access components on a form with data-aware components. But if you plan on reusing groups of database and system objects, or if you want to isolate the parts of your application that handle database connectivity and business rules, then Delphi's Data Modules provide a convenient organizational tool.

This chapter of the free database course for Delphi beginners shows how to use Data Modules in Delphi (ADO) database development to partition an application into user interface, application logic and data.

Data modules provide a formal mechanism for collecting and encapsulating DataSet and DataSource objects, their attributes, events and code (business rules) in one central location. Data modules can contain only nonvisual components and are generally used in database and Web development. They provide Delphi developers a visual way to manipulate and code non-visual components of an application.

Generally, a TDataModule class is used for some of the purposes:

- Sharing data access components and code
Use a TDataModule to provide a location for centralized handling of nonvisual components. Typically these are data access components (TADOConnection, TADOTable, TADOQuery, TADOCommand, etc), but they can also be other nonvisual components. This is convenient when an application has multiple forms that share the same data access provider - if your application operates on only one database file (one mdb) then you'll need only one TADOConnection component for all the dataset components.

- Design time visual organization
At design time, the Diagram page in the Code editor provides visual tools for setting up logical relationships among the components on the data module. You can drag a property connector from one component to another to hook them up. Drag a master-detail connector between tables to join them. You reach the persistent datasets fields much faster than when using the fields editor.

- Business rules centralization
In the unit file for the data module a Delphi developer may also place any business rules that are to be applied to the application. These are the controls that prevent invalid data begin entered into a database and ensuring that valid data is maintained within the database.

- Code maintenance
Data modules make code maintenance easier since you only have to change code in one place instead of in every form - especially when having common db functions located in a data module or if we create an application with several forms serving the same database tables and/or queries.

   New ... Data Module
To create a data module at design time, choose File | New | Data Module. At design time, a data module looks like a standard Delphi form with a white background and no alignment grid. At run time data module exists only in memory. DataModule has only two properties, Name and Tag, and two events, OnCreate and OnDestroy. Use the Name property when referring to module's objects from other units.

DataModules are not limited to data access components, they can also contain other nonvisual components, such as TMainMenu, TTimer, TSaveDialog or TPopUpMenu).

Data Module1

You place all the components in the right pane ("Components") of data module's window. The tree view on the left is for navigation purposes.

Note: in Delphi versions prior to version 6, the TDataModule class was defined in the Forms unit. Delphi 6 moves TDataModule class to the Classes unit to allow smaller GUI-less applications to be written and to separate OS independent classes. In Delphi 6, the Tree diagram and the Data Diagram are separated. The Diagram page on the Code editor provides visual tools for setting relationships among non-visual (and visual) components.

When you select the Data Diagram page (for the first time), you are presented with an empty container, in which you can drag any object from the Tree View and then link those objects. Relationships include parent/child, datasource/dataset and similar. For example, after moving a TADOTable and TADOConnection to the Diagram view, you select the Property connector icon, click the ADOTable and drag to the ADOConnection. The connection will be made: TADOTable.Connection property will point to ADOConnection component - as can be seen in the Object Inspector.

data module in D5 and less

To make the data module available to another unit in the application, select that unit, then choose File|Use Unit to add the data module to the uses clause for the unit. If you have several forms that refer to the same data module, make sure the data module is created before it is referenced from those forms - or you'll get an access violation error.
When refering to a dataset like ADOTable1 (on a data module, named uDM) from some data browsing form (let's say form1) your code will look like:

// some procedure in form1

   To the next chapter
If you need any kind of help so far, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

DB Course Next Chapter >>
>> Handling database errors

©2017 About.com. All rights reserved.