Secrets Of FireDAC: Learn How To Use Macros In TFDQuery With This Delphi Sample

The Macros sample shows you how to use the Macros property of TFDQuery in order to parameterize parts of the SQL query. The sample allows you to modify the macro values at run time.

Location

You can find the Macros sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesComp LayerTFDQueryMacros
  • 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

  1. Navigate to the location given above and open Macros.dproj.
  2. Press F9 or choose Run > Run.
  3. Click on the Use Connection Definition combo box and select an option.
  4. Interact with the sample:
    • Modify the Macros values.
    • Click on the Open Query button.

Files

File in Delphi Contains
Macros.dproj
Macros.dpr
The project itself.
fMacros.pas
fMacros.fmx
The main form.

Implementation

Before running the sample, the main components are configured at design time using the Object Inspector as follows:

  • TFDQuery object named qryMain. 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 !column from !tab where !id !Sign !idValue. It specifies the SQL statement to execute for the query. On run time, when you click on the Open Query button, the Macro values are substituted in the place of their names in the SQL statement.
  • Set up the macros property. The sample sets the five macros used on the SQL statement: COLUMNTABIDSignIDVALUE.
  • Set the MacroCreate property of ResourceOptions to True.
  • 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 qryMain, and the DataSource property of DBGrid1 is set to DataSource1.

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 enables the Open Query button. Then, if you click on the Open Query button, the sample uses the Open method of qryMain in order to execute the SQL statement. The SQL statement is executed using the Macros values in order to retrieve the corresponding data, which is displayed using a TDBGrid component. Once the DBGrid1 is filled, you can interact with the sample in run time:

  • Modify the Macros values.
  • Choose the corresponding Data Type.
  • Click on the Open Query button

Please follow the link below to get more information about the sample:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDQuery.Macros_Sample