I\'m not quite familiar with programming ASP classic. I just need a small code to run on my webpage. How do i count the record of the returned query?
<%
S
If you are using MySQL try this:
Dim strSQLscroll, rsscroll, countrs
Set rsscroll = Server.CreateObject("ADODB.Recordset")
rsscroll.CursorLocation = 3
rsscroll.open "SELECT * FROM tblItems where expiration_date > getdate()
order by expiration_date desc;",oConn
countrs = rsscroll.recordcount
rsscroll.RecordCount
It is possible (but not recommended) to use the RecordCount property on the Recordset object as follows:
iTotalRecords = rsscroll.RecordCount
If your table is really large, this can take a long time to run. I would instead run a separate SQL query to get the total records
SQL = "SELECT COUNT(*) AS TotalRecords FROM tblItems WHERE expiration_date > getdate() "
set rsRecordCount = conn.Execute(SQL)
if not rsRecordCount.Eof then
iTotalRecords = rsRecordCount.Fields("TotalRecords")
else
iTotalRecords = 0
end if
rsRecordCount.Close
set rsRecordCount = nothing
I usually use a separate query like "select count(*) from table " to get counts because I usually need not only the count but a summation of the number of units or the average price or whatever and it's easier to write a separate query than to make more variables and say "TotalUnits = TotalUnits + rs("Units").value" inside the loop to display the results. It also comes in handy for the times you need to show the totals above the results and you don't want to loop though the recordset twice.
<% ' TableID = your tables ID...
Set rsscroll = Server.CreateObject("ADODB.Recordset") Dim strSQLscroll, rsscroll strSQLscroll = "SELECT *,(SELECT TableID FROM tblItems where expiration_date > getdate()) As Count FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
rsscroll.open strSQLscroll,oConn
Count = rsscroll("Count")
%>
You could just change your SQL to count the records:
strSQLscroll = "SELECT count(*) as Total FROM tblItems where expiration_date > getdate();"
Then you just need to response.write rsscroll("Total")