Fórum Como fazer filtro composto DELPHI #613678
31/12/2020
0
utilizo SQLite de BD
Muito obrigado, desde ja agradeço
Ramboli
Curtir tópico
+ 0Post mais votado
31/12/2020
vou tentar adivinhar aqui... supondo que todos os comboboxes estejam com Style = cdDropDownList
você precisa de uma parte fixa da instrução, e também de uma parte variável.
var
strSELECT, strWHERE: string;
procedure AddWhere( strCondicao: string );
begin
if strWhere <> EmptyStr then strWhere := strWhere + ' AND ';
strWhere := strWhere + strCondicao;
end;
begin
strSELECT := 'SELECT [LISTA DE CAMPOS OU *] FROM TABELA';
strWHERE := EmptyStr;
// 1o. combobox, com o filtro por UFs
if ComboBox1.Text <> 'TODOS' then
AddWhere('UF = '+QuotedStr(ComboBox1.Text));
// 2o. combobox, com o estado civil (casado. solteiro, etc)
if ComboBox2.Text <> 'TODOS' then
AddWhere('ESTCIVIL = '+QuotedStr(ComboBox2.Text));
// 3o. combobox, com os campos que serão utilizados para filtrar o conteúdo do Edit
// por exemplo: no combobox haverá as opções: Código / Nome / CNPJ
// e no Edit será indicado o valor a procurar
if Edit1.Text <> EmptyStr then
begin
if ComboBox3.ItemIndex = 0 then // pesquisa por código
AddWhere('CODIGO = '+QuotedStr(Edit1.Text))
else
if ComboBox3.ItemIndex = 1 then // pesquisa por nome
AddWhere('NOME LIKE '+QuotedStr('%'+Edit1.Text+'%'))
else // pesquisa por CNPJ
AddWhere('CNPJ = '+QuotedStr(Edit1.Text));
end;
if strWHERE <> EmptyStr then
strSELECT := strSELECT + ' WHERE ' + strWHERE;
// o código abaixo depende dos componentes utilizados
objQuery.Close;
objQuery.SQL.Text := strSELECT;
objQuery.Open;
end;
Emerson Nascimento
Gostei + 1
Mais Posts
31/12/2020
Ramboli
vou tentar adivinhar aqui... supondo que todos os comboboxes estejam com Style = cdDropDownList
você precisa de uma parte fixa da instrução, e também de uma parte variável.
var
strSELECT, strWHERE: string;
procedure AddWhere( strCondicao: string );
begin
if strWhere <> EmptyStr then strWhere := strWhere + ' AND ';
strWhere := strWhere + strCondicao;
end;
begin
strSELECT := 'SELECT [LISTA DE CAMPOS OU *] FROM TABELA';
strWHERE := EmptyStr;
// 1o. combobox, com o filtro por UFs
if ComboBox1.Text <> 'TODOS' then
AddWhere('UF = '+QuotedStr(ComboBox1.Text));
// 2o. combobox, com o estado civil (casado. solteiro, etc)
if ComboBox2.Text <> 'TODOS' then
AddWhere('ESTCIVIL = '+QuotedStr(ComboBox2.Text));
// 3o. combobox, com os campos que serão utilizados para filtrar o conteúdo do Edit
// por exemplo: no combobox haverá as opções: Código / Nome / CNPJ
// e no Edit será indicado o valor a procurar
if Edit1.Text <> EmptyStr then
begin
if ComboBox3.ItemIndex = 0 then // pesquisa por código
AddWhere('CODIGO = '+QuotedStr(Edit1.Text))
else
if ComboBox3.ItemIndex = 1 then // pesquisa por nome
AddWhere('NOME LIKE '+QuotedStr('%'+Edit1.Text+'%'))
else // pesquisa por CNPJ
AddWhere('CNPJ = '+QuotedStr(Edit1.Text));
end;
if strWHERE <> EmptyStr then
strSELECT := strSELECT + ' WHERE ' + strWHERE;
// o código abaixo depende dos componentes utilizados
objQuery.Close;
objQuery.SQL.Text := strSELECT;
objQuery.Open;
end;
Opa, bem, na verdade estou precisando pro intraweb, eu adaptei seu code, na deburação a query recebe o valor de strSELECT certinho, mas não filtra a grid, esqueci de fazer algo? Fiz igual aqui
objQuery.Close; objQuery.SQL.Text := strSELECT; objQuery.Open;
So substituindo a query
Gostei + 0
31/12/2020
Ramboli
vou tentar adivinhar aqui... supondo que todos os comboboxes estejam com Style = cdDropDownList
você precisa de uma parte fixa da instrução, e também de uma parte variável.
var
strSELECT, strWHERE: string;
procedure AddWhere( strCondicao: string );
begin
if strWhere <> EmptyStr then strWhere := strWhere + ' AND ';
strWhere := strWhere + strCondicao;
end;
begin
strSELECT := 'SELECT [LISTA DE CAMPOS OU *] FROM TABELA';
strWHERE := EmptyStr;
// 1o. combobox, com o filtro por UFs
if ComboBox1.Text <> 'TODOS' then
AddWhere('UF = '+QuotedStr(ComboBox1.Text));
// 2o. combobox, com o estado civil (casado. solteiro, etc)
if ComboBox2.Text <> 'TODOS' then
AddWhere('ESTCIVIL = '+QuotedStr(ComboBox2.Text));
// 3o. combobox, com os campos que serão utilizados para filtrar o conteúdo do Edit
// por exemplo: no combobox haverá as opções: Código / Nome / CNPJ
// e no Edit será indicado o valor a procurar
if Edit1.Text <> EmptyStr then
begin
if ComboBox3.ItemIndex = 0 then // pesquisa por código
AddWhere('CODIGO = '+QuotedStr(Edit1.Text))
else
if ComboBox3.ItemIndex = 1 then // pesquisa por nome
AddWhere('NOME LIKE '+QuotedStr('%'+Edit1.Text+'%'))
else // pesquisa por CNPJ
AddWhere('CNPJ = '+QuotedStr(Edit1.Text));
end;
if strWHERE <> EmptyStr then
strSELECT := strSELECT + ' WHERE ' + strWHERE;
// o código abaixo depende dos componentes utilizados
objQuery.Close;
objQuery.SQL.Text := strSELECT;
objQuery.Open;
end;
Opa, bem, na verdade estou precisando pro intraweb, eu adaptei seu code, na deburação a query recebe o valor de strSELECT certinho, mas não filtra a grid, esqueci de fazer algo? Fiz igual aqui
objQuery.Close; objQuery.SQL.Text := strSELECT; objQuery.Open;
So substituindo a query
Opa, resolvi aqui, foi necessário dar um refresh na query apos abrir ela
objQuery.Close; objQuery.SQL.Text := strSELECT; objQuery.Open; objQuery.Refresh;
Muito obrigado cara
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)