Print Page | Close Window

Calling Results from SELECT COUNT

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=1739
Printed Date: 28 March 2026 at 5:59pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Calling Results from SELECT COUNT
Posted By: jdumayas
Subject: Calling Results from SELECT COUNT
Date Posted: 10 April 2003 at 2:54pm
Hello,

I'm using the following line of code to return the number of students are registered for a course.

rs.Open "SELECT COUNT(courseID) FROM tblCourseStudentLookup WHERE courseID="&instanceNo, adoCon

I'm not getting an error with this line(it may be incorrect usage also), but I get an error when trying to use response.write to display me the number of records (students).

I was wondering if I could get any suggestion/examples regarding the syntax of both the COUNT function and getting the results to display using response.write. FYI I'm using Access 2002.

Thanks for all of your help. - Jeremy





Replies:
Posted By: Nigelo
Date Posted: 10 April 2003 at 3:52pm

Originally posted by jdumayas jdumayas wrote:

Hello,

I'm using the following line of code to return the number of students are registered for a course.

rs.Open "SELECT COUNT(courseID) FROM tblCourseStudentLookup WHERE courseID="&instanceNo, adoCon

As far as the SQL string is concerned, always try to use "*" with Count function as this is heavily optimised in Access using the built in Rushmore technology and will therefore speed up processing. 

Also, your code snippet appears to be missing a name for the returned result so will default to Expr1; a field name should really be enclosed in square brackets; use spaces between the quote ("), ampersand (&) and instanceNo; and terminate the SQL expression with a ";"

Your example rewritten
 rs.Open "SELECT COUNT([courseID]) As NumRecs FROM tblCourseStudentLookup WHERE courseID=" & instanceNo & ";", adoCon

Same optimised
 rs.Open "SELECT COUNT(*) As NumRecs FROM tblCourseStudentLookup WHERE courseID=" & instanceNo & ";", adoCon  

Sorry, don't have time to test it but should work



-------------
Hope this helps
Nigel


Posted By: Bunce
Date Posted: 11 April 2003 at 10:53pm

and so to retrieve it using Nigelo's code:

iResult = rs("NumRecs")

Cheers,
Andrew



-------------
There have been many, many posts made throughout the world...
This was one of them.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net