Como fazer uma consulta utilizando vários.

Delphi

14/02/2008

Bom dia.

Quero fazer uma consulta utilizando vários ComboBox. Exemplo:

ComboBox1, ComboBox2, ComboBox3 e ComboBox4.

--------------------------------------------------------------------
Como fiz:

pesq1:= QuotedStr(rel_Orc.ComboBox1.Text);
pesq2:= QuotedStr(rel_Orc.ComboBox2.Text);
pesq3:= QuotedStr(rel_Orc.ComboBox3.Text);
pesq4:= QuotedStr(rel_Orc.ComboBox4.Text);

strSql:= ´SELECT * FROM ORCAMENTO WHERE´;

if (rel_Orc.ComboBox1.Text <> ´´) then
begin
strSql:= strSql + ´ COD = ´ + pesq1 + ´ AND´;
end;

if (rel_Orc.ComboBox2.Text <> ´´) then
begin
strSql:= strSql + ´ NOME = ´ + pesq2 + ´ AND´;
end;

if (rel_Orc.ComboBox3.Text <> ´´) then
begin
strSql:= strSql + ´ CPF = ´ + pesq3 + ´ AND´;
end;

if (rel_Orc.ComboBox4.Text <> ´´) then
begin
strSql:= strSql + ´ RG = ´ + pesq4 + ´ AND´;
end;
------------------------------------------------------------------

Mas no final da erro, porque sempre vai sobrar um ´AND´.
O que devo fazer? Como devo proceder para fazer este tipo de pesquisa?

Obrigado.

Uelinton


Uelinton_reis

Uelinton_reis

Curtidas 0

Respostas

Massuda

Massuda

14/02/2008

Faça assim...
pesq1:= QuotedStr(rel_Orc.ComboBox1.Text);
pesq2:= QuotedStr(rel_Orc.ComboBox2.Text);
pesq3:= QuotedStr(rel_Orc.ComboBox3.Text);
pesq4:= QuotedStr(rel_Orc.ComboBox4.Text);

strSql:= ´´;

if (rel_Orc.ComboBox1.Text <> ´´) then
begin
    if strsql <> ´´ then strsql := strsql + ´ AND´;
    strSql:= strSql + ´ COD = ´ + pesq1;
end;

if (rel_Orc.ComboBox2.Text <> ´´) then
begin
    if strsql <> ´´ then strsql := strsql + ´ AND´;
    strSql:= strSql + ´ NOME = ´ + pesq2;
end;

if (rel_Orc.ComboBox3.Text <> ´´) then
begin
    if strsql <> ´´ then strsql := strsql + ´ AND´;
    strSql:= strSql + ´ CPF = ´ + pesq3;
end;

if (rel_Orc.ComboBox4.Text <> ´´) then
begin
    if strsql <> ´´ then strsql := strsql + ´ AND´;
    strSql:= strSql + ´ RG = ´ + pesq4;
end;

strSql:= ´SELECT * FROM ORCAMENTO WHERE´ + strSql;



GOSTEI 0
POSTAR