Using a data source to bind parameters

From InterBase

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 named SalesDatabase linked to the employee.gdb database, SalesQuery and SalesTransaction.
  • A TIBTransaction component named SalesTransaction linked to SalesQuery and SalesDatabase.
  • A TIBTable dataset component named CustomersTable linked to the CUSTOMER table, CustomersDatabase and CustomersTransaction.
  • A TIBDatabase component named CustomersDatabase linked to the employee.gdb database, CustomersTable and CustomersTransaction.
  • A TIBTransaction component named CustomersTransaction linked to CustomersTable and CustomersDatabase.
  • A TDataSource component named SalesSource. The DataSet property of SalesSource points to SalesQuery.
  • A TDataSource named CustomersSource linked to CustomersTable. The DataSource property of the OrdersQuery component is also set to CustomersSource. This is the setting that makes OrdersQuery 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:

Sample master/detail query form and data module at design time

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.

Advance To: