Query não retorna registros do banco

09/10/2020

0

Bom dia pessoal,
Preciso da ajuda de vocês. Em um form tenho uma 3 queries que carregam registros referente a 2 tabela de preços diferentes praticadas. A implementação é a seguinte se não encontrar registros na primeira passa para a segunda, se não encontrar registros nas duas primeiras preenche os campos com a terceira query. As duas primeiras regras são cumpridas em qualquer circunstância, somente se não tem registros nas duas primeiras a terceira query não retorna o registro do banco. Já debuguei e não consigo encontrar o erro. Se alguém puder me dar uma direção ficarei muito grato. Segue código abaixo:


procedure TfrmCadastroOrcamentos.buscaTabelaValores(Sender: TObject);
begin
CDSTabelaPrecosClientes.Close;
CDSTabelaPrecosClientes.ParamByName('ident').Value:= DBEdit1.Text;
CDSTabelaPrecosClientes.ParamByName('plp').Value:= DBLookupComboBox5.KeyValue;
CDSTabelaPrecosClientes.Open;
 if CDSTabelaPrecosClientes.RecordCount > 0 then
 begin
 ComboBox2.ItemIndex:= 0;
 StringGrid1.Cells[18, linhaGrid]:= 'Cliente';
  if ComboBox10.Text = 'SIM'  then
  begin
  StringGrid1.Cells[19, linhaGrid]:= CDSTabelaPrecosClientes.FieldByName('SIMPLES').Value;
  end;
  if ComboBox10.Text = 'COM' then
  begin
  StringGrid1.Cells[19, linhaGrid]:= CDSTabelaPrecosClientes.FieldByName('COM').Value;
  end;
  if ComboBox10.Text = 'OF' then
  begin
  StringGrid1.Cells[19, linhaGrid]:= CDSTabelaPrecosClientes.FieldByName('OF').Value;
  end;
 end
  else if CDSTabelaPrecosClientes.IsEmpty then
 begin
 CDSBuscaTabelaComum.Close;
 CDSBuscaTabelaComum.ParamByName('plp').AsInteger:= DBLookupComboBox5.KeyValue;
 CDSBuscaTabelaComum.ParamByName('ptab').AsInteger:= keyFieldComum; // id registrado no banco para está tabela
 CDSBuscaTabelaComum.Open;
  if CDSBuscaTabelaComum.RecordCount > 0 then
  begin
  ComboBox2.ItemIndex:= 1;
  StringGrid1.Cells[18, linhaGrid]:= 'Comum';
   if ComboBox10.Text = 'SIM'  then
   begin
   StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaComum.FieldByName('VALOR_SIMPLES').Value;
   end;
   if ComboBox10.Text = 'COM' then
   begin
   StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaComum.FieldByName('VALOR_COM').Value;
   end;
   if ComboBox10.Text = 'OF' then
   begin
   StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaComum.FieldByName('VALOR_OF').Value;
   end
    else if (CDSTabelaPrecosClientes.IsEmpty) AND (CDSBuscaTabelaComum.IsEmpty)  then
   begin
   CDSBuscaTabelaOficial.Close;
   CDSBuscaTabelaOficial.ParamByName('pid').AsInteger:= DBLookupComboBox5.KeyValue;
   CDSBuscaTabelaOficial.ParamByName('ptp').AsInteger:= keyFieldOficial; // id registrado no banco para está tabela
   CDSBuscaTabelaOficial.Open;
    if CDSBuscaTabelaOficial.RecordCount > 0 then
    begin
    ComboBox2.ItemIndex:= 2;
    StringGrid1.Cells[18, linhaGrid]:= 'Oficial';
     if ComboBox10.Text = 'SIM'  then
     begin
     StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaOficial.FieldByName('VALOR_SIMPLES').Value;
     end;
     if ComboBox10.Text = 'COM' then
     begin
     StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaOficial.FieldByName('VALOR_COM').Value;
     end;
     if ComboBox10.Text = 'OF' then
     begin
     StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaOficial.FieldByName('VALOR_OF').Value;
     end
      else
     begin
     ShowMessage('Tabela não cadastrada. Favor contatar o administrador do sistema.');
     ComboBox2.ClearSelection;
     end;
    end;
   end;
  end;
 end;

Devnator

Devnator

Responder

Posts

13/10/2020

Imex

Bom dia,

Acredito que esteja faltando um End no trecho abaixo:

  if CDSBuscaTabelaComum.RecordCount > 0 then
  begin
  ComboBox2.ItemIndex:= 1;
  StringGrid1.Cells[18, linhaGrid]:= 'Comum';
   if ComboBox10.Text = 'SIM'  then
   begin
   StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaComum.FieldByName('VALOR_SIMPLES').Value;
   end;
   if ComboBox10.Text = 'COM' then
   begin
   StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaComum.FieldByName('VALOR_COM').Value;
   end;
   if ComboBox10.Text = 'OF' then
   begin
   StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaComum.FieldByName('VALOR_OF').Value;
   end;
  end
   else if (CDSTabelaPrecosClientes.IsEmpty) AND (CDSBuscaTabelaComum.IsEmpty)  then


Acredito também que esses Ifs para verificar a propriedade IsEmpty não são necessários já que estão no Else do If que verifica se a propriedade RecordCount é maior que zero.

Espero que ajude
Responder

13/10/2020

Emerson Nascimento

muitos begin..end podem atrapalhar em vez de ajudar.
procedure TfrmCadastroOrcamentos.buscaTabelaValores(Sender: TObject);
begin
	CDSTabelaPrecosClientes.Close;
	CDSTabelaPrecosClientes.ParamByName('ident').Value := DBEdit1.Text;
	CDSTabelaPrecosClientes.ParamByName('plp').Value := DBLookupComboBox5.KeyValue;
	CDSTabelaPrecosClientes.Open;

	if not CDSTabelaPrecosClientes.IsEmpty then
	begin

		ComboBox2.ItemIndex := 0;
		StringGrid1.Cells[18, linhaGrid] := 'Cliente';

		if ComboBox10.Text = 'SIM' then
			StringGrid1.Cells[19, linhaGrid] := CDSTabelaPrecosClientes.FieldByName('SIMPLES').Value
		else
		if ComboBox10.Text = 'COM' then
			StringGrid1.Cells[19, linhaGrid] := CDSTabelaPrecosClientes.FieldByName('COM').Value
		else
		if ComboBox10.Text = 'OF' then
			StringGrid1.Cells[19, linhaGrid] := CDSTabelaPrecosClientes.FieldByName('OF').Value;

	end
	else
	begin

		CDSBuscaTabelaComum.Close;
		CDSBuscaTabelaComum.ParamByName('plp').AsInteger := DBLookupComboBox5.KeyValue;
		CDSBuscaTabelaComum.ParamByName('ptab').AsInteger := keyFieldComum; // id registrado no banco para esta tabela
		CDSBuscaTabelaComum.Open;

		if not CDSBuscaTabelaComum.IsEmpty then
		begin

			ComboBox2.ItemIndex := 1;
			StringGrid1.Cells[18, linhaGrid] := 'Comum';

			if ComboBox10.Text = 'SIM'  then
				StringGrid1.Cells[19, linhaGrid] := CDSBuscaTabelaComum.FieldByName('VALOR_SIMPLES').Value
			else
			if ComboBox10.Text = 'COM' then
				StringGrid1.Cells[19, linhaGrid] := CDSBuscaTabelaComum.FieldByName('VALOR_COM').Value
			else
			if ComboBox10.Text = 'OF' then
				StringGrid1.Cells[19, linhaGrid] := CDSBuscaTabelaComum.FieldByName('VALOR_OF').Value;

		end
		else
		begin

			CDSBuscaTabelaOficial.Close;
			CDSBuscaTabelaOficial.ParamByName('pid').AsInteger := DBLookupComboBox5.KeyValue;
			CDSBuscaTabelaOficial.ParamByName('ptp').AsInteger := keyFieldOficial; // id registrado no banco para esta tabela
			CDSBuscaTabelaOficial.Open;

			if not CDSBuscaTabelaOficial.IsEmpty then
			begin

				ComboBox2.ItemIndex := 2;
				StringGrid1.Cells[18, linhaGrid] := 'Oficial';

				if ComboBox10.Text = 'SIM'  then
					StringGrid1.Cells[19, linhaGrid] := CDSBuscaTabelaOficial.FieldByName('VALOR_SIMPLES').Value
				else
				if ComboBox10.Text = 'COM' then
					StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaOficial.FieldByName('VALOR_COM').Value
				else
				if ComboBox10.Text = 'OF' then
					StringGrid1.Cells[19, linhaGrid]:= CDSBuscaTabelaOficial.FieldByName('VALOR_OF').Value;

			end
			else
			begin

				ShowMessage('Tabela não cadastrada. Favor contatar o administrador do sistema.');
				ComboBox2.ClearSelection;

			end;
		end;
	end;

end;


se a busca na tabela oficial não estiver retornando dados, verifique se os parâmetros estão corretos, ou até mesmo se a consulta estácorreta.


Responder

14/10/2020

Devnator

Bom dia,
Obrigado pelas respostas pessoal. Imex realmente o problema era causado por falta de um end, reparei que estava fazendo o else no bloco errado, e resolvi. Emerson para este caso retirei os begin...end pois eles causaram bastante confusão na manutenção do código.
Obrigado a todos pelo tempo e disposição.
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar