Count Distinct Records For An Access Database And Active Server Pages

Count Unique Records - Filter Access Database Records Select Count

I used ASPmaker to generate my main page at
http://quarter-horse-times.com/horse-show/VIEWCLASSES.asp but I wanted to show a count of the number of horses entered in each class. Getting a count of the total number of FILES in each class was easy. The hard part for me was making the count distinct so that all the files for one horse were counted as one, thereby giving me the total number of horses entered in each class.

I used Buildapp to create the code for an array that I could copy and paste into my ASPmaker generated table. But how to filter the TOTAL number of horses entered in each class? I soon learned that Access does not support SELECT (DISTINCT column).

After much trial and error and surfing of forums, I came up with the following that works like a charm. I am posting it here in case it might be helpful to someone else. If you find it useful, please send me an email, Lil at horsevu.com.

COPY AND PASTE FRIENDLY CODE

<%
Dim strcnt 'this is to place the horse show class number into a variable
strcnt=rs("ORDERID") 'this recordset is from one that was created higher up in the code on my horse show page.
%>

<%
Dim objRS,strDBPath,objConn,strSQL,x,arrData,strFieldValue
Dim intcolcounter,introwcounter,strPageType,strOrderBy
Dim intpages,pageno,intRecordCnt,pagesize,intPage,strheadings,intRecord
 

'The following method I found on a forum, filters the records so that all of the files in a horse show class that are associated with one particular horse will be counted only once as a single entry.
The SHOWHORSECLASSID = strcnt further filters 'the TOTAL so that only the files within a particular class are counted.


strSQL = "SELECT COUNT(SHOWHORSEID) AS TOTAL FROM (SELECT DISTINCT SHOWHORSEID FROM SHOWFILES WHERE SHOWHORSECLASSID =" & strcnt &")" & ""
Set objRS = Conn.Execute(strSQL,,adCmdText)


response.write("<table border=0><tr>")
for each x in objRS.Fields
response.write("<th><font size=1><b><i>" & x.name & "</font></b></i></th>")
next
response.write("</tr>")
arrData = objRS.getrows
objRS.close
set objRS=nothing

For intRowCounter= 0 to UBound(arrData,2)
Response.Write("<tr>" & vbcrlf)
For intColCounter=0 to UBound(arrData,1)
strFieldValue = arrdata(intcolcounter,introwcounter)
If VarType(strFieldValue) = "8209" Then
response.write("<td> Ole Object or empty</td>")
Else
response.write("<td><font size=1><b><i>" & strFieldValue & "</font></b><i></td>")
End If
Next
Response.Write("</tr>")
Next
Response.Write("</table>")

%>

COPY AND PASTE FRIENDLY CODE

ANOTHER ACCESS SELECT DISTINCT COUNT:

SELECT count(HORSEENTRYNUMBER) AS TOTAL
FROM [Select distinct HORSEENTRYNUMBER from SHOWPLACEMENTS]. AS HORSENUM

'I don't know why but we get an error if we don't include the period after SHOWPLACEMENTS with "as horsenum"


My other tutorials:

ASP Examples Menu of Active Server Pages scripts code Classic ASP

Classic ASP Active Server Pages Examples, code, tutorials, scriptsASP Examples Menu (below*)



Active Server Pages | ASP | Scripts | Tutorials | Code | Web Programming | Examples
Active Server Pages ASP code examples, tutorials, and snippets for use in Programming interactive websites.

NOTE: Recently changed hosting and url so some demos aren't working now because I haven't updated pathing; if you find omissions or errors let me know.
 

*

Multiple records per row with paging

Display multiple records per row

Get ID of newly inserted record, Classic ASP, Access Database

Javascript timer with ASP to keep track of time expired until login expires / timeout session is up

Multiple Javascript event functions triggered from single form onsubmit

Get Querystring key names along with querystring variables

ASP Youtube Downloader  Version FOUR
All ASP code. Allows user client side downloads of Youtube videos. You have the option to allow files to be saved directly to your site also.

ASP/PHP video downloader (Works better than ASP for large files)

Use ASP to dynamically parse a Youtube XML playlist to render to html.

Zip up an archive file of your site for backup if you need to restore website with ASP and free Zip Component.

Zip all files in a folder into an archive or backup file dynamically by specific file extension.

Sanitize or clean price cost field for database insertion or display.

Javascript image dimensions and image file size preview.

Put recordset into array, assign keys to the records then use record associated with specific array key as a variable elsewhere.

Access Database Interface Generator Wizard to read your table and field names and to create a table display.

Combining dropdown selection box menu with dynamic ASP database page.

How to redirect user to original page they tried to view before logging, after they are logged in.

Previous/Current/Next Records, or Buddy Ring Script

Custom ASP 404 page not found error script sends visitors to virtual folders

ASP Example: Combine ASP with Javascript for popup windows
dynamically sized to image dimensions.

ASP Example: How to use ASPJPEG to create thumbnails
Click for free Highly recommended thumbnailer for which you don't need Server access

ASPjpeg: How to re-scale aspect ratio of thumbnails by height instead of width.

ASP Example: What if your host has an older version of ASPJPEG that doesn't support gifs?

Using inline frames as "dynamic includes" (offline for updating)

Select Count Distinct Records for Access Database

Upload Image to folder and insert filename into database

Get date in such a way that it can be made part of a file name

Isolate a file name away from its path or extension or strip a path from a file name or strip characters from database field

User-customized on-the-fly stylesheet css files

Enhanced Page Hits Counter (also counts downloads and menu link hits)

Loop through array and compare to string with select case

Aphabetical Paging for your ASP scripts

Web Wiz Guide's Site Searcher Script modified as a file content indexer

Let Users Select How Many Records to View

Access Database for your PayPal IPN scripts

Miscellaneous collected code

You don't have to be a genius, just persistent, to write original Active Server Pages scripts

Software Recommendation: bare_bones_no_bells_and_whistles_asp_code_generator_database_interface_maker

--Lil at gmail.com