ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

Script Output:

This table currently has 22 records in it!

They are:

id text_field integer_field date_time_field
1200672 Friday 19 11/19/2010 3:52:59 PM
1200673 Friday 19 11/19/2010 3:53:01 PM
1200674 Friday 19 11/19/2010 3:53:02 PM
1200675 Friday 19 11/19/2010 3:53:03 PM
1200676 Friday 19 11/19/2010 3:53:03 PM
1200677 Friday 19 11/19/2010 3:53:04 PM
1200678 Friday 19 11/19/2010 3:53:04 PM
1200679 Friday 19 11/19/2010 3:53:04 PM
1200680 Friday 19 11/19/2010 3:53:05 PM
1200681 Friday 19 11/19/2010 3:53:05 PM
1200682 Friday 19 11/19/2010 3:53:05 PM
1200683 Friday 19 11/19/2010 3:53:05 PM
1200684 Friday 19 11/19/2010 3:53:05 PM
1200685 Friday 19 11/19/2010 3:53:05 PM
1200686 Friday 19 11/19/2010 3:53:06 PM
1200687 Friday 19 11/19/2010 3:53:06 PM
1200688 Friday 19 11/19/2010 3:53:06 PM
1200689 Friday 19 11/19/2010 3:53:08 PM
1200690 Friday 19 11/19/2010 3:53:11 PM
1200691 Friday 19 11/19/2010 3:53:17 PM
1200692 Friday 19 11/19/2010 3:57:50 PM
1200693 Friday 19 11/19/2010 4:19:11 PM

Hide the table

ASP HTML Download            FAQ

ASP.NET Version

One of the most misunderstood features of ADO is the RecordCount property. I've heard from many frustated users on the topic and quite a few of them have decided that it must just be something that's not supported with their DB. Well I'm going to try and set the record straight as best I can.

The RecordCount property is supported by both Access and SQL Server. I can't speak for sure on other platforms, but I've been told it's a pretty widely supported feature and I'd assume most vendors have some way of doing it.

So why isn't it working for you? Well it's supported only if the recordset supports approximate positioning (adApproxPosition) or bookmarks (adBookmark). I only mention this so that if you feel like testing for support you can do so using code like this:

If objRecordset.Supports(adBookmark) Or objRecordset.Supports(adApproxPosition) Then
	' RecordCount will work!
End If

So what the heck does that mean?

Basically it's supported if you use the right kind of cursor. For the most part all client-side cursors support it as do server-side static and server-side keyset cursors. So the only two that don't are server-side dynamic and server-side forward only cursors. Unfortunately, most people don't specify the cursor type to use when they open a recordset and what you usually get when you don't specify one is a server-side, forward only cursor which is one of the ones that doesn't support a record count! So the trick is to specify what type of cursor you want.

If for some reason you need one of the ones that doesn't support RecordCount, you can get around this by fully populating the recordset before asking for the RecordCount. This can be accomplished by doing a MoveLast, but again only if this is supported by the recordset. Otherwise you're basically out of luck unless you can wait until you've read all the records to get an accurate count.

Oh and one last thing... If you're just trying to get the count and don't care about the records instead or using ADO, try this SQL syntax instead:

SELECT COUNT(field_name) FROM table_name;

It tends to be easier on the server and you can even use the WHERE clause to limit the results to match whatever criteria you want. If you want to get really creative, try adding the GROUP BY or other clauses. With some thought and a little bit of trial and error, you'll soon be counting anything you want in the database!


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers