Using a data source to bind parameters
Go Up to Setting parameters
If parameter values for a parameterized query are not bound at design time or specified at runtime, the query component attempts to supply values for them based on its DataSource
property. DataSource
specifies a different table or query component that the query component can search for field names that match the names of unbound parameters. This search dataset must be created and populated before you create the query component that uses it. If matches are found in the search dataset, the query component binds the parameter values to the values of the fields in the current record pointed to by the data source.
You can create a simple application to understand how to use the DataSource property to link a query in a master-detail form. Suppose the data module for this application is called LinkModule, and that it contains a query component called SalesQuery
that has the following SQL property:
SELECT Cust_No, Po_Number, Order_Date FROM Sales WHERE Cust_No = :Cust_No
The LinkModule data module also contains:
- A
TIBDatabase
component namedSalesDatabase
linked to theemployee.gdb
database,SalesQuery
andSalesTransaction
. - A
TIBTransaction
component namedSalesTransaction
linked toSalesQuery
andSalesDatabase
. - A TIBTable dataset component named
CustomersTable
linked to theCUSTOMER
table,CustomersDatabase
andCustomersTransaction
. - A
TIBDatabase
component namedCustomersDatabase
linked to theemployee.gdb
database,CustomersTable
andCustomersTransaction
. - A
TIBTransaction
component namedCustomersTransaction
linked toCustomersTable
andCustomersDatabase
. - A TDataSource component named
SalesSource
. TheDataSet
property ofSalesSource
points toSalesQuery
. - A TDataSource named
CustomersSource
linked toCustomersTable
. TheDataSource
property of theOrdersQuery
component is also set toCustomersSource
. This is the setting that makesOrdersQuery
a linked query.
Suppose, too, that this application has a form, named LinkedQuery
that contains two data grids, a Customers Table
grid linked to CustomersSource
, and an SalesQuery
grid linked to SalesSource
.
The following figure illustrates how this application appears at design time:
- Note: If you build this application, create the table component and its data source before creating the query component.
If you compile this application, at runtime the :Cust_No
parameter in the SQL statement for SalesQuery
is not assigned a value, so SalesQuery
tries to match the parameter by name against a column in the table pointed to by CustomersSource
. CustomersSource
gets its data from CustomersTable
, which, in turn, derives its data from the CUSTOMER
table. Because CUSTOMER
contains a column called “Cust_No,” the value from the Cust_No
field in the current record of the CustomersTable
dataset is assigned to the :Cust_No
parameter for the SalesQuery
SQL
statement. The grids are linked in a master-detail relationship. At runtime, each time you select a different record in the Customers Table grid, the SalesQuery
SELECT
statement executes to retrieve all orders based on the current customer number.