PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → [WD12] How to access COUNT(*) from a query
[WD12] How to access COUNT(*) from a query
Débuté par DarrenF, 11 mai 2010 08:22 - 5 réponses
Posté le 11 mai 2010 - 08:22
Hi,
I have what seems to be a simple question, but can't seem to get my head around how the retrieval of data works for query datasets in WD - maybe it's too late for me to be thinking :confused:
I have a simple select query that returns a count:
SELECT COUNT(*) FROM PRODUCTS;
Then I use HExecuteQuery, but how do I retrieve the COUNT(*) value?
I'm pretty sure it's a HRead... of some kind, but what's the syntax?
Thanks in advance...
Posté le 11 mai 2010 - 08:23
Hello Darren
I don't use queries but I use views and I can get the number of records in a view with HnbRec() Can you use that function on a query as HnbRec(QueryName) ?
Regards
Al
Posté le 11 mai 2010 - 08:23
Hi Al,
Thanks for the reply...
Just tried it and the answer is "yes" it works! :cheers:
HExecuteQuery(QRY_CountActiveCategories)
glocaliCatCount is int = HNbRec(QRY_CountActiveCategories)
HExecuteQuery(QRY_CountActiveWebProducts)
glocaliProdCount is int = HNbRec(QRY_CountActiveWebProducts)
HExecuteQuery(QRY_CountProducts2Categories)
glocaliPrd2CatCount is int = HNbRec(QRY_CountProducts2Categories)
Info("Categories = " + glocaliCatCount + CR + "Products = " + glocaliProdCount + CR + "Prods 2 Cat = " + glocaliPrd2CatCount)
Posté le 11 mai 2010 - 11:03
Darren, how do you build your query?
If you are using the designer the "count(*)" gets an alias like count_1
"select count(*) as count_1 from products"
If you build it yourself, you need to add such a field-alias as well.
Have a look at this one, just another example, returning one record with 3 fields
select SUM(amount) as total, article_id, article_description
from orders left join orderlines on orders.orderis=orderlines.orderid
where orderid={ParamOrderID}
group by article_id,article_description
Executing the query will return one record, with one or more fields field, one of them is the count or sum.
So just get the result as you would do with other queries
HReadFirst(query) // because there i just one record
nTotals is int = query.count_1
or
cyTotals is currency = query.total
sArticle is string = query.article_description
Notice that you will ALWAYS get one record, when grouping. Even if the table is empty. In that case your count_1=0 or total=0
Posté le 11 mai 2010 - 11:52
Arie,
Thanks for the explaination... now I understand :spos:
Membre enregistré
1 message
Posté le 13 juin 2017 - 07:50
Queries are helpful way of extracting information from one or more tables based on a set of search condition given by you. This tutorial help you in acquiring the complete knowledge on query, how to design and run the query wizard in MS Access. Learn how it is to be done.

Read more:
http://repairmsaccess.weebly.com/blogs/how-to-run-the-query-wizard-in-access