Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table filtering in two fields with two comboboxes 2

Status
Not open for further replies.

aliosman

Programmer
Oct 2, 2004
3
0
0
AL
I want to filter a table. IN the table I don`t have any indexed fileds. I have one class field and one lesson field.
NOW the problem is how can I filter this table with this two comboboxes. one of them for classes other one for lessones. It must be filtered with both of comboboxes.

I tried it, when i choose CLASS(1.combobox)table is filtered by classes.It is OKAY. But when I choose LESSON (2.combobox) Table is filtered only by lessons. it is not filtering with both of them at the same time..

thanks...
 
Can you show the code that you use to filter the table?

Andrew
Hampshire, UK
 
Are you using the Filter property or are you using the OnFilterRecord event? For this task, I would probably use the OnFilterRecord event. It would look something like this:
Code:
procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
  Accept := False;
  if (combobox1.ItemIndex >= 0) then
  begin
    Accept := (Table1.FieldByName('CLASS').AsString = 
                combobox1[combobox1.ItemIndex]);
    if Accept and (combobox2.ItemIndex >= 0) then
      Accept := (Table1.FieldByName('LESSON').AsString = 
                  combobox2[combobox2.ItemIndex]);
  end;
end;

-Dell
 
Hilfy's code can be simplified as follows:-
Code:
procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
  Accept := (Table1.FieldByName('class').AsString=ComboBox1.Text) and (Table1.FieldByName('lesson').AsString=ComboBox2.Text);
end;

Andrew
Hampshire, UK
 
Towerbase's code is good if you'll ALWAYS have values in both ComboBox1 and ComboBox2. However, if you will sometimes have only ComboBox1, you'll have to use something like my code. Both are right, they just cover different circumstances.

-Dell
 
The original requirement was for both ComboBoxes to be used for the filtering. So I don't know why you need to test for ItemIndex to be >= 0.

Maybe I don't understand the problem?



Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top