We help IT Professionals succeed at work.

How to get a value from TADOQuery and assign to a text field.

hawkerpac
hawkerpac asked
on
I have a query in the TADOQuery
select sum(wr_run) as ActHrs from pub.wr_route where "wr_nbr" >= :nbr1
                                                                                    and wr_nbr <= :nbr2

Run time i want to assing the value Acthrs to a text field.

I have done the following

    cop_qryactstd.Close;
    cop_qryactstd.Parameters.ParamValues['nbr1'] :=  cop_edtfwo1.Text;
    cop_qryactstd.Parameters.ParamValues['nbr2'] :=  cop_edttwo1.Text;
    cop_qryactstd.Open;

   Now how to assign the value to the text field.

  Thanks in advace for the help.

Comment
Watch Question

Top Expert 2011

Commented:
Since there are numbers involved Sum returns Float. Then what you can do is to get the value as Float and convert to String.
...
  cop_qryactstd.Close;
  cop_qryactstd.Parameters.ParamValues['nbr1'] :=  cop_edtfwo1.Text;
  cop_qryactstd.Parameters.ParamValues['nbr2'] :=  cop_edttwo1.Text;
  cop_qryactstd.Open;
  Edit1.Text := FloatToStr(cop_qryactstd.FieldByName('ActHrs').AsFloat);
...

Open in new window

Top Expert 2011
Commented:
Or simply you can assign the value AsString:
...
  cop_qryactstd.Close;
  cop_qryactstd.Parameters.ParamValues['nbr1'] :=  cop_edtfwo1.Text;
  cop_qryactstd.Parameters.ParamValues['nbr2'] :=  cop_edttwo1.Text;
  cop_qryactstd.Open;
  Edit1.Text := cop_qryactstd.FieldByName('ActHrs').AsString;
...

Open in new window

Top Expert 2011

Commented:
Also you can format the returned figure for arithmetic precision:
...
  cop_qryactstd.Close;
  cop_qryactstd.Parameters.ParamValues['nbr1'] :=  cop_edtfwo1.Text;
  cop_qryactstd.Parameters.ParamValues['nbr2'] :=  cop_edttwo1.Text;
  cop_qryactstd.Open;
  Edit1.Text := Format('%0.2n', [cop_qryactstd.FieldByName('ActHrs').AsFloat]);  // for 2 decimal places
...

Open in new window

Author

Commented:
Thanks for the help. Worked.