1

Суть в том, чтобы в label1 отобразилась сумма двух других label. Проблема в итоговой сумме label1

var
begin
  ADOQuery:=TADOQuery.Create(Self);
  ADOQuery.Connection:=dm.conadoc;
  ADOQuery.Close;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.Add('SELECT Sum(tarif) as tarif FROM reester where len(tarif)>0;');
  ADOQuery.Open;
  lbl2.Caption:=floattostr(ADOQuery.fieldvalues['tarif']);

  ADOQuery:=TADOQuery.Create(Self);
  ADOQuery.Connection:=dm.conadoc;
  ADOQuery.Close;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.Add('SELECT Sum(taksa) as taksa FROM reester where len(taksa)>0;');
  ADOQuery.Open;
  lbl3.Caption:=floattostr(ADOQuery.fieldvalues['taksa']);

lbl1.Caption:=floattostr(strtofloat(lbl2.Caption)+floattostr(strtofloat(lbl3.Caption))); 
end;

2 ответа 2

2

Правильнее будет сначала посчитать сумму, а потом выводить её, примерно вот так:

begin
  ADOQuery:=TADOQuery.Create(Self);
  ADOQuery.Connection:=dm.conadoc;
  ADOQuery.Close;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.Add('SELECT Sum(tarif) as tarif FROM reester where len(tarif)>0;');
  ADOQuery.Open;
  tarif := ADOQuery.fieldvalues['tarif'];

  ADOQuery:=TADOQuery.Create(Self);
  ADOQuery.Connection:=dm.conadoc;
  ADOQuery.Close;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.Add('SELECT Sum(taksa) as taksa FROM reester where len(taksa)>0;');
  ADOQuery.Open;
  taksa := ADOQuery.fieldvalues['taksa'];

  lbl1.Caption := floattostr(tarif + taksa); 
  lbl2.Caption := floattostr(tarif);
  lbl3.Caption := floattostr(taksa);
end;

А еще правильнее, посчитать все сразу в одном SQL-запросе и получить 3 цифры сразу (с учетом возможности наличия NULL).

0
lbl1.Caption := FloatToStr( StrToFloat(lbl2.Caption) + StrToFloat(lbl3.Caption) ); 
1
  • Все получилось спасибо большое за помощь))
    – delphi
    17 мар 2017 в 5:31

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.