Using Dialog Values in Queries

Using Dialog Values in Queries

This is where it gets tricky. The Parameter Editor has limited use with dialog controls.

Method 1: Direct Parameter Assignment (RECOMMENDED)

Set query parameters directly in the dialog's OnCloseQuery event:

procedure DialogPage1OnCloseQuery(Sender: TfrxComponent; var CanClose: Boolean);
begin
  // Close the queries first
  qryOrders.Close;
  
  // Set parameter values from dialog controls
  qryOrders.ParamByName('CustomerID').Value := DBLookupComboBox1.KeyValue;
  qryOrders.ParamByName('StartDate').Value := DateEdit1.Date;
  qryOrders.ParamByName('EndDate').Value := DateEdit2.Date;
  qryOrders.ParamByName('Status').Value := ComboBox1.Text;
  
  // Reopen with new parameters
  qryOrders.Open;
end;

Your query SQL:

SELECT * FROM Orders 
WHERE CustomerID = :CustomerID 
  AND OrderDate BETWEEN :StartDate AND :EndDate
  AND Status = :Status

Leave the Parameter Editor values EMPTY - you're setting them in code.

Method 2: Using Report Variables (Alternative)

If you prefer using the Parameter Editor, use report variables as an intermediary:

Step 1: Create variables

(Report → Variables):

Variable Name: CustomerID
Type: Integer
Value: 0

Step 2: Set variables in dialog script:

procedure DialogPage1OnCloseQuery(Sender: TfrxComponent; var CanClose: Boolean);
begin
  Report.Variables['CustomerID'] := Integer(DBLookupComboBox1.KeyValue);
  Report.Variables['StartDate'] := DateEdit1.Date;
end;

Step 3: Use in Parameter Editor:

Parameter: CustomerID
Value: <CustomerID>

Problem with this method:

Articles sometimes evaluates variables incorrectly. Method 1 is more reliable.