Ajuda em uma Função que checa se foi cadastrado um Advogado

Delphi

30/11/2012

function TFrmCadAtos.ChecaAdvogado(sLivro : String; sFolha : String; sTermo : String; tipocentral :String) : String;
var QueryAdvogado : TQuery;
begin
QueryAdvogado := TQuery.create(application);
QueryAdvogado.DatabaseName := 'SisRecurso';
QueryAdvogado.SessionName := 'default';
QueryAdvogado.Close;
QueryAdvogado.SQL.Clear;
QueryAdvogado.SQL.add('SELECT * FROM DoiAtos ');
QueryAdvogado.SQL.Add(' WHERE Livro =' +QuotedStr(sLivro)+ ' AND FolhaIni =' +QuotedStr(sFolha)+ ' AND Ordem =' +QuotedStr(sTermo)+ 'AND testatipocentral =' +QuotedStr(tipocentral));
QueryAdvogado.Prepare;
QueryAdvogado.Open;

QueryAdvogado.First;
while not(queryAdvogado.Eof) do
begin
if (QueryAdvogado.FieldByName('CESDQUALIDADE').AsString = 'ADVOGADO(A)') then
begin
Result := 'S';
break;
end
else
Result := 'N'
;
QueryAdvogado.Next;
end;
QueryAdvogado.close;
end;

essa é a função que filtra onde os registros de livro, folha e termo são iguais, se um dos registros tem advogado.
mas, com os meus testes mesmo cadastrando um advogado quando cai no IF, ele não esta trazendo nada, que por sua vez
considera que não existem advogados nos registros selecionados.
Alguém poderia me ajudar?
Luis Flores

Luis Flores

Curtidas 0

Respostas

Roney Melo

Roney Melo

30/11/2012

Você precisa checar realmente o conteúdo do campo ? ou se retornar algum registro já satifaz sua condição ? se for isso basta vc mudar seu IF assim:

if (not QueryAdvogado.IsEmpty) then// se o result da Query não for vazio, significa que teve alguma coisa.
begin
Result := 'S';
break;
end
else
Result := 'N';
GOSTEI 0
Claudia Nogueira

Claudia Nogueira

30/11/2012

Concordo com o amigo que isEmpty é melhor.

Ao meu ver o código ficaria melhor assim:

Vou usar citação pra ficar melhor pra visualizar.


function TFrmCadAtos.ChecaAdvogado(sLivro, sFolha, sTermo, tipocentral: String): String;
Var
QueryAdvogado : TQuery;
sSql : String;
begin
try
sSql := '';
if sLivro <> '' then
sSql := sSql + ' AND (Livro = '+QuotedStr(sLivro)+') ';
if sTermo <> '' then
sSql := sSql + ' AND (Ordem = '+QuotedStr(sTermo)+') ';
if tipocentral <> '' then
sSql := sSql + ' AND (testatipocentral = '+QuotedStr(tipocentral)+') ';

QueryAdvogado := TQuery.Create(application);
QueryAdvogado.DatabaseName := 'SisRecurso';
QueryAdvogado.SessionName := 'default';
QueryAdvogado.Close;
QueryAdvogado.SQL.Clear;
QueryAdvogado.SQL.add('SELECT * FROM DoiAtos ');
QueryAdvogado.SQL.Add('WHERE (CESDQUALIDADE = ''ADVOGADO(A)'') ');
QueryAdvogado.SQL.Add(sSql);
QueryAdvogado.Prepare;
QueryAdvogado.Open;

if (not QueryAdvogado.IsEmpty) then
Result := 'S'
else
Result := 'N';

QueryAdvogado.Close;
finally
QueryAdvogado.Free;
end;
end;
GOSTEI 0
Roney Melo

Roney Melo

30/11/2012

Concordo com o amigo que isEmpty é melhor.

Ao meu ver o código ficaria melhor assim:

Vou usar citação pra ficar melhor pra visualizar.


function TFrmCadAtos.ChecaAdvogado(sLivro, sFolha, sTermo, tipocentral: String): String;
Var
QueryAdvogado : TQuery;
sSql : String;
begin
try
sSql := '';
if sLivro <> '' then
sSql := sSql + ' AND (Livro = '+QuotedStr(sLivro)+') ';
if sTermo <> '' then
sSql := sSql + ' AND (Ordem = '+QuotedStr(sTermo)+') ';
if tipocentral <> '' then
sSql := sSql + ' AND (testatipocentral = '+QuotedStr(tipocentral)+') ';

QueryAdvogado := TQuery.Create(application);
QueryAdvogado.DatabaseName := 'SisRecurso';
QueryAdvogado.SessionName := 'default';
QueryAdvogado.Close;
QueryAdvogado.SQL.Clear;
QueryAdvogado.SQL.add('SELECT * FROM DoiAtos ');
QueryAdvogado.SQL.Add('WHERE (CESDQUALIDADE = ''ADVOGADO(A)'') ');
QueryAdvogado.SQL.Add(sSql);
QueryAdvogado.Prepare;
QueryAdvogado.Open;

if (not QueryAdvogado.IsEmpty) then
Result := 'S'
else
Result := 'N';

QueryAdvogado.Close;
finally
QueryAdvogado.Free;
end;
end;


Show de bola Claudia!!!
GOSTEI 0
Luis Flores

Luis Flores

30/11/2012

deu certo, valeu aí pessoal. abraços
GOSTEI 0
POSTAR