Working with DBLookupComboBox
The TfrxDBLookupComboBox is the most powerful dialog control but also the most complex.
Setup Requirements
You need:
- A dataset in your report (on the Data page) with lookup values
- A DBLookupComboBox on the dialog page
- Proper configuration of the lookup properties
Step-by-Step Configuration
1. Create the lookup dataset:
On the Data page:
- Add TfrxFDDatabase (connects to your database)
- Add TfrxFDQuery named "qryCustomers"
- SQL: SELECT CustomerID, CompanyName FROM Customers ORDER BY CompanyName
- Set Active = True (or open it in script)
2. Create a frxDBDataset:
Add TfrxDBDataset:
Name: dsCustomers
DataSet: qryCustomers
UserName: "Customers"
3. Configure the DBLookupComboBox:
Properties:
Name: DBLookupComboBox1
ListSource: dsCustomers
KeyField: "CustomerID" (the value you want)
ListField: "CompanyName" (what the user sees)
Width: 300
Setting Default Value
In the DialogPage OnShow event:
pascal
procedure DialogPage1OnShow(Sender: TfrxComponent);
var
ds: TfrxDataSet;
begin
// Set to first item in list
ds := DBLookupComboBox1.ListSource.DataSet;
if Assigned(ds) and (ds.RecordCount > 0) then
begin
ds.First;
DBLookupComboBox1.KeyValue := ds.Value[DBLookupComboBox1.KeyField];
end;
end;
Getting the Selected Value
The selected value is in DBLookupComboBox1.KeyValue:
pascal
// KeyValue contains the CustomerID (the value from KeyField)
// Text contains the CompanyName (the value from ListField)
var
SelectedID: Integer;
SelectedName: String;
begin
SelectedID := DBLookupComboBox1.KeyValue; // e.g., 123
SelectedName := DBLookupComboBox1.Text; // e.g., "Acme Corp"
end;
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:
pascal
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:
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:
pascal
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.