![jumpstorydownload20200403000521 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/jumpstory-download20200403-000521.jpg?resize=1140%2C694&ssl=1)
This sample demonstrates how to use the filter feature of TFDQuery.
To this end, the sample implements different preset filters. Moreover, you can edit your own filtering condition and apply the filter to a dataset in order to search for specific records.
Table of Contents
Location
You can find the Filter sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
Object PascalDatabaseFireDACSamplesComp LayerTFDQueryFilter
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
How to Use the Sample
- Navigate to the location given above and open
Filter.dproj
. - Press F9 or choose Run > Run.
![Filter | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/10/Filter.png?resize=750%2C448&ssl=1)
Files
File in Delphi | Contains |
---|---|
Filter.dproj Filter.dpr | The project itself. |
fFilter.pas fFilter.fmx | The main form. |
Implementation
Before running the sample, the main components are configured at design time using the Object Inspector as follows:
- A TFDQuery object named qryWithFilter. This component is used to implement a dataset capable of executing SQL queries. The following setup is needed:
- Configure the Connection property to specify the FireDAC connection object that is used to connect to a DBMS.
- Set the SQL property to
select * from {id Orders}
. It specifies the SQL statement to execute for the query. - Set the Filtered property to
True
in order to active the filtering for the dataset.
- A TDataSource object named DataSource1. This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the dataset and the grid where the dataset is displayed. To this end, the DataSet property of DataSource1 is set to
qryWithFilter
, and the DataSource property of DBGrid1 is set toDataSource1
.
When you run the application, click on the Use Connection Definition combo box and select an option in order to define a connection. When you select an item of the combo box, the sample uses the Open method of qryWithFilter in order to execute the SQL statement that retrieves the “Orders” table from the database. Then, the sample displays the “Orders” table using a TDBGrid component. Once the DBGrid1 is filled, you can interact with the sample in the following ways:
- Set a filter. Type your filter condition on the edit box and click on the Set button to apply the filter to the current dataset. For example: type
ShipCountry='Spain'
and press Set in order to see the records from the “Orders” table whose ShipCountry field is set toSpain
. - Use a preset filter. Click on the other buttons to see the results for different preset filters. For example: click on the Using BETWEEN button to see the records whose OrderID field is set between
10270
and10280
.
The link below will redirect you to the original post of the sample:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDQuery.Filter_Sample
Head over and check out the full source code for the TFDQuery FireDAC filter sample for Delphi.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition