SQL MAX function

Freerider
Freerider used Ask the Experts™
on
Hi all,

I'm trying to display the highest value from a table,
using the following code:

---
Query2.SQL.Add('SELECT MAX(Price1) AS maxprice');
Query2.SQL.Add(' FROM CustomDTL');
Query2.ExecSQL;
showmessage(FloatToStr(maxprice));
---

This isn't returning the result I expect.
How do I get the value "maxprice" out of the query?

Thanks,
Freerider,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ShowMessage(FloatTostr(query2MaxPrice.value));

But you must Doubleclick your Tquery, and Add the Fields in order to make persistant fields.

Best Regards.
Marcos.
Top Expert 2004
Commented:
as it is a select-query, use the open method,
to get the value use something like this


Query2.SQL.Add('SELECT MAX(Price1) AS maxprice');
Query2.SQL.Add(' FROM CustomDTL');
Query2.Open;

showmessage(FloatToStr(query2.fieldbyname('maxprice').AsFloat));

or
showmessage(query2.fieldbyname('maxprice').AsString);

Author

Commented:
Thanks kretzschmar,

without the "Query2.Open;" it wouldn't have worked.
But I didn't realise I had to create a new field for "MaxPrice".

Freerider.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial