VCL Delphi

Title: How to fill a ComboBox with Table-values.
Question: You will filter a Table with values from it own columns.
Answer:
Create Form1 with this Objects
Table1 Table with Customer-Infos e.g.
Firstname
....
Month (string yyyymm with Secondary Index)
....
DataSource1
DBGrid1
ComboBox1
This procedure fills the Combobox with values from field Month.
Your Table can have many Records with the sam value:
200106
200108
200108
200109
200109
200110
... and so on
In the Combobox is only one Entry from every value stored:
200106
200108
200109
200110
If you select one of this Entries, the Table ist filtered
with this value
private
cFilter: string;
procedure TForm1.FillCombo;
var
cMonth: string;
begin
MtCombo.Items.clear;
Table1.Filter := 'Month ''000000''';
// first add one entry for select to 'Filter-Reset'
MtCombo.Items.append('');
Table1.open;
// set the Month-Compare-value
cMonth := '';
// go thru the Table and get the values
// for every existing value is only one loop produced
// Missing values are ignored.
// at start cMonth is empty and all Records are available.
while not Table1.Eof do begin
// Now add the Month-value from the first Record of
// this filter-set to the Combobox
MtCombo.Items.append(Table1['Monat']);
// create the Month-Filter to Greater-Than-Current-Month
cFilter := 'Month ''' + Table1['Month'] + '''';
// save the Current Month for using after the last loop
cMonth := Table1['Month'];
// set the filter for the next loop now
Table1.Filter := cFilter;
end;
// Select the first Combobox-Entry
MtCombo.ItemIndex := MtCombo.Items.Count - 1;
MtCombo.Text := cMonth;
// and set the selectet value as filter
Table1.filter := 'Month = ''' + MtCombo.Items[MtCombo.ItemIndex] + '''';
cFilter := Table1.filter;
Table1.first;
end;
// at Startup the Combobox is filled first
procedure TForm1.FormCreate(Sender: TObject);
begin
FillCombo;
end;
// in the Combobox onChange event set the Table-Filter
procedure TForm1.MtComboChange(Sender: TObject);
begin
// look if currently filter is not ''
// than create this value as an filter-expression
if (MtCombo.Items[MtCombo.ItemIndex] '') then
Table1.filter := 'Month = ''' + MtCombo.Items[MtCombo.ItemIndex] + ''''
else
// otherwise reset filter-expression because '' is selected
Table1.Filter := '';
Table1.Last;
// save the last Filter-Settings
cFilter := Table1.Filter;
end;
// if you produces new records with insert or append
// your field Month is also set to an new value (by
// your self). If this value other than the stored
// values in the Combobox, then you can call FillCombo
// to create the new Combobox-entries:
procedure TForm1.Table1AfterInsert(DataSet: TDataSet);
begin
...
FillCombo;
end;
Regards
Kurt