...

View Full Version : recordset counter



ahmedsoliman
02-24-2003, 02:40 AM
i tried to count the number of records in my query,
i used loop and rs.recordcount, each one give me different result
and rs.recordcount always equal -1

this my code:
<%
email=Trim(Request.Form("frmail"))

set con=server.CreateObject("adodb.connection")
con.open("shazly")
set rs = Server.CreateObject("ADODB.recordset")
sql2="select email from members where email='"&email&"'"
rs.Open sql2, con

counts=rs.RecordCount

s=0
while not rs.eof
s=s+1
rs.movenext
wend

Response.Write("s="&s)
Response.Write("<br>")
response.Write("count="&counts)
Response.Write("<br>")

%>
the result:
s=60
count=-1
?:confused:

oracleguy
02-24-2003, 04:57 AM
Might be a problem with the cursor you are using.

Try refering here: http://www.asp101.com/samples/db_count.asp

And like its mentioned on the afore mentioned page, you also can get the count by using the following SQL query:
SELECT COUNT(field_name) FROM table_name;

glenngv
02-24-2003, 05:34 AM
oracleguy is correct.
but you can also try this if this works, ive seen this trick somewhere.

rs.Open sql2, con
rs.movelast
counts=rs.RecordCount
response.write counts

ahmedsoliman
02-25-2003, 12:25 AM
ok it works with sql query,but with glenngv's code it give me this error:
Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E24)
Rowset does not support fetching backward.
at this line: rs.movelast

i dont know why?

christrinder
02-25-2003, 01:30 PM
Not sure if I'm missing your point, but I use this to count my records, you can specify a criteria in the WHERE statement too...

<%
SQLnumof = "SELECT count(*) AS numof FROM #table#
Set objRS = oConnection.Execute(SQLnumof)
numof = objRS("numof")
response.write numof
%>

ahmedsoliman
02-25-2003, 04:18 PM
to christrinder:
i mean this method give me error but when using sql query(like yours) it works
<%
set con=server.CreateObject("adodb.connection")
con.open("shazly")
set rs = Server.CreateObject("ADODB.recordset")
sql2="select email from members where email='"&email&"'"
rs.Open sql2, con
rs.movelast
counts=rs.RecordCount
response.write counts
%>

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E24)
Rowset does not support fetching backward.
at this line: rs.movelast



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum