RAD Studio VCL Reference
TADOQuery.DataSource Property

Specifies the data source component from which to extract current field values to use in same-name parameters in the ADO query's SQL statement.

property DataSource: TDataSource;
__property TDataSource DataSource;__property TDataSource DataSource;

Set DataSource to automatically fill parameters in a query with fields values from another dataset. Parameters that have the same name as fields in the other dataset are filled with the field values. Parameters with names that are not the same as fields in the other dataset do not automatically get values, and must be programmatically set. For example, if the SQL property of the TADOQuery contains the SQL statement below and the dataset referenced through DataSource has a CustNo field, the value from the current record in that other dataset is used in the CustNo parameter:

FROM Orders O
WHERE (O.CustNo = :CustNo)

DataSource must point to a TDataSource component linked to another dataset component; it cannot point to this query component. The dataset specified in DataSource must be created, populated, and opened before attempting to bind parameters. Parameters are bound by setting the ADO query's Prepared property to true prior to executing the query. If the SQL statement used by a query does not contain parameters, or all parameters are bound by the application using the Parameters property or the ParamByName method, DataSource need not be assigned. The example below shows setting the DataSource property of ADOQuery2 to the data source for ADOQuery1, preparing ADOQuery2, and activating ADOQuery2:

with ADOQuery2 do begin
  DataSource := DataSource1;
  Prepared := True;


ADOQuery2->DataSource = DataSource1;
ADOQuery2->Prepared = true;

If the SQL statement in the TADOQuery is a SELECT query, the query is executed using the new field values each time the record pointer in the other dataset is changed. It is not necessary to call the Open method of the TADOQuery each time. This makes using the DataSource property to dynamically filter a query result set useful for establishing Master-Detail relationships. Set the DataSource property in the Detail query to the TDataSource component for the Master dataset. 

If the SQL statement uses other than a SELECT query (such as INSERT or UPDATE), the parameters with the same name as fields in the other dataset still get values, but the query must be explicitly executed each time the other dataset's record pointer moves. For example, the SQL statement below uses the INSERT statement and has the parameters CustNo and CompanyName: 


(CustNo, Company) 

VALUES (:CustNo, :CompanyName) 

Another dataset, ADOQuery1 and DataSource1, has a CustNo field but no CompanyName field. If this dataset is used through the DataSource property, the CompanyName parameter must be programmatically assigned a value. Because ADOQuery1 has a CustNo field and ADOQuery1 is referenced through the DataSource property, the CustNo parameter automatically receives a value.

with ADOQuery2 do begin

DataSource := DataSource1;

  ParamByName ('CompanyName').AsString := Edit1.Text;

Prepared := True; 




ADOQuery2->DataSource = DataSource1;
ADOQuery2->ParamByName("CompanyName")->AsString = Edit1->Text;
ADOQuery2->Prepared = true;

If the SQL statement contains parameters with the same name as fields in the other dataset, do not manually set values for these parameters. Any values programmatically set, such as by using the Parameters property or the ParamByName method, will be overridden with automatic values. Parameters of other names must be programmatically given values. These parameters are unaffected by setting DataSource. 

DataSource can be set at runtime or at design-time using the Object Inspector. At design-time, select the desired TDataSource from the drop-down list or type in the name. 


Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!