Hey,
I've been busy coding combo boxes to filter a database but the problem is they don't work together. e.g. If i select a car type, it shows all the cars of that type but if i select blue in one combo box and a car type in the other one: It doesn't filter for blue cars of that type, it just filters for the latest one selected.
I'm fairly new to Delphi so i don't have a massive technical knowledge yet so I have no idea how to get them to work together? Any ideas?

My code looks something like this:

procedure TForm1.FormActivate(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list]  ' ;
ADOQuery1.Active := true;

ADOTable1.Open;
ADOTable1.First;
while not ADOTable1.Eof do
        begin
        cmbtype.Items.add(ADOTable1['car type']);
        cmbcolour.Items.add(ADOTable1['car colour']);
        ADOTable1.Next;
        end;
end;

procedure TForm1.cmbtypeChange(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list] where [car type] = "'+ cmbtype.Text+'"';
ADOQuery1.Active := true;
end;

procedure TForm1.cmbcolourChange(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list] where [car colour] = "'+ cmbcolour.Text+'"';
ADOQuery1.Active := true;
end;

end.

Hey,
I've been busy coding combo boxes to filter a database but the problem is they don't work together. e.g. If i select a car type, it shows all the cars of that type but if i select blue in one combo box and a car type in the other one: It doesn't filter for blue cars of that type, it just filters for the latest one selected.
I'm fairly new to Delphi so i don't have a massive technical knowledge yet so I have no idea how to get them to work together? Any ideas?

My code looks something like this:

procedure TForm1.FormActivate(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list]  ' ;
ADOQuery1.Active := true;

ADOTable1.Open;
ADOTable1.First;
while not ADOTable1.Eof do
        begin
        cmbtype.Items.add(ADOTable1['car type']);
        cmbcolour.Items.add(ADOTable1['car colour']);
        ADOTable1.Next;
        end;
end;

procedure TForm1.cmbtypeChange(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list] where [car type] = "'+ cmbtype.Text+'"';
ADOQuery1.Active := true;
end;

procedure TForm1.cmbcolourChange(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list] where [car colour] = "'+ cmbcolour.Text+'"';
ADOQuery1.Active := true;
end;

end.

Hi
I Think that if you change your code as below it will work properly.

procedure TForm1.FormActivate(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.sql.Text := 'select * from [car list]  ' ;
ADOQuery1.Active := true;

ADOTable1.Open;
ADOTable1.First;
while not ADOTable1.Eof do
        begin
        cmbtype.Items.add(ADOTable1['car type']);
        cmbcolour.Items.add(ADOTable1['car colour']);
        ADOTable1.Next;
        end;
end;

procedure TForm1.cmbtypeChange(Sender: TObject);
Var
    CType,CColor:String;
begin
    CType:=Trim(cmbtype.Text);
    CColor:=Trim(cmbcolour.Text);
    If CType='' then
        CType:='%';
    If CColor='' then
        CColor:='%';
    ADOQuery1.Active := false;
    ADOQuery1.sql.Text := 'select * from [car list] where [car type]like "'+ Ctype+'"'+' And [car colour] like "'+CColor;
    ADOQuery1.Active := true;
end;

procedure TForm1.cmbcolourChange(Sender: TObject);
Var
    CType,CColor:String;
begin
    CType:=Trim(cmbtype.Text);
    CColor:=Trim(cmbcolour.Text);
    If CType='' then
        CType:='%';
    If CColor='' then
        CColor:='%';
    ADOQuery1.Active := false;
    ADOQuery1.sql.Text := 'select * from [car list] where [car type] like "'+ Ctype+'"'+' And [car colour] like "'+CColor;
    ADOQuery1.Active := true;
end;

end.
This article has been dead for over six months. Start a new discussion instead.