Complete Examples
Example 1: Customer Orders Report with Date Range
Data Setup:
qryCustomers:
SQL: SELECT CustomerID, CompanyName FROM Customers ORDER BY CompanyName
qryOrders:
SQL: SELECT * FROM Orders
WHERE CustomerID = :CustomerID
AND OrderDate BETWEEN :StartDate AND :EndDate
Dialog Layout:
Label1: "Customer:"
DBLookupComboBox1: (ListSource=dsCustomers, KeyField=CustomerID, ListField=CompanyName)
Label2: "Start Date:"
DateEdit1:
Label3: "End Date:"
DateEdit2:
btnOK: (ModalResult=mrOk)
btnCancel: (ModalResult=mrCancel)
Dialog Script:
procedure DialogPage1OnShow(Sender: TfrxComponent);
var
ds: TfrxDataSet;
begin
// Set default dates
DateEdit2.Date := Date;
DateEdit1.Date := Date - 30;
// Set first customer
ds := DBLookupComboBox1.ListSource.DataSet;
if Assigned(ds) and (ds.RecordCount > 0) then
begin
ds.First;
DBLookupComboBox1.KeyValue := ds.Value['CustomerID'];
end;
end;
procedure DialogPage1OnCloseQuery(Sender: TfrxComponent; var CanClose: Boolean);
begin
// Validate
if VarIsNull(DBLookupComboBox1.KeyValue) then
begin
ShowMessage('Please select a customer');
CanClose := False;
Exit;
end;
if DateEdit1.Date > DateEdit2.Date then
begin
ShowMessage('Start date must be before end date');
CanClose := False;
Exit;
end;
// Set parameters
qryOrders.Close;
qryOrders.ParamByName('CustomerID').Value := DBLookupComboBox1.KeyValue;
qryOrders.ParamByName('StartDate').Value := DateEdit1.Date;
qryOrders.ParamByName('EndDate').Value := DateEdit2.Date;
qryOrders.Open;
CanClose := True;
end;
Example 2: Product Sales with Multiple Filters
Dialog Controls:
DBLookupComboBox1: Category selection
DBLookupComboBox2: Product selection
CheckBox1: Include inactive products
Edit1: Minimum quantity
Dialog Script:
procedure DialogPage1OnShow(Sender: TfrxComponent);
begin
// Set defaults
CheckBox1.Checked := False;
Edit1.Text := '0';
// Set first category
var ds := DBLookupComboBox1.ListSource.DataSet;
if Assigned(ds) and (ds.RecordCount > 0) then
begin
ds.First;
DBLookupComboBox1.KeyValue := ds.Value['CategoryID'];
end;
end;
procedure DBLookupComboBox1OnChange(Sender: TfrxComponent);
begin
// When category changes, update product list
qryProducts.Close;
qryProducts.ParamByName('CategoryID').Value := DBLookupComboBox1.KeyValue;
qryProducts.Open;
// Select first product in new list
if qryProducts.RecordCount > 0 then
begin
qryProducts.First;
DBLookupComboBox2.KeyValue := qryProducts.Value['ProductID'];
end;
end;
procedure DialogPage1OnCloseQuery(Sender: TfrxComponent; var CanClose: Boolean);
var
MinQty: Integer;
begin
// Validate numeric input
try
MinQty := StrToInt(Edit1.Text);
if MinQty < 0 then
begin
ShowMessage('Minimum quantity cannot be negative');
CanClose := False;
Exit;
end;
except
ShowMessage('Please enter a valid number');
CanClose := False;
Exit;
end;
// Set all parameters
qrySales.Close;
qrySales.ParamByName('ProductID').Value := DBLookupComboBox2.KeyValue;
qrySales.ParamByName('IncludeInactive').Value := CheckBox1.Checked;
qrySales.ParamByName('MinQty').Value := MinQty;
qrySales.Open;
CanClose := True;
end;