Accessing a Data Module from a Form
Go Up to Using Data Modules
To associate visual controls on a form with a data module, you must first add the data module to the form's uses clause. You can do this in several ways:
- In the Code editor, open the form's unit file and add the name of the data module to the uses clause in the interface section.
- Click the form's unit file, choose File > Use Unit, and enter the name of the module or pick it from the list box in the Use Unit dialog.
- For database components, in the data module click a dataset or query component to open the Fields editor and drag any existing fields from the editor onto the form. The IDE prompts you to confirm that you want to add the module to the form's uses clause, then creates controls (such as edit boxes) for the fields.
For example, if you've added the TClientDataSet component to your data module, double-click it to open the Fields editor. Select a field and drag it to the form. An edit box component appears.
Because the data source is not yet defined, Delphi adds a new data source component, DataSource1, to the form and sets the edit box's DataSource property to DataSource1. The data source automatically sets its DataSet property to the dataset component, ClientDataSet1, in the data module.
You can define the data source before you drag a field to the form by adding a TDataSource component to the data module. Set the data source's DataSet property to ClientDataSet1. After you drag a field to the form, the edit box appears with its TDataSource property already set to DataSource1. This method keeps your data access model cleaner.