PDA

View Full Version : Counting number of records when in a loop.


Mhtml
10-19-2002, 09:44 AM
I'm trying to count the number of records from a database table when in the middle of a loop.

I'm trying to make a mini message board but I'm stuck on getting the number of replies.

The replies are in a separate table and I need to insert them into the loop, they can be identified for which thread they belong to by the column ToThreadId and in the threads table there is the ThreadId column to make it all work out.

I can display replies but counting them and putting them next to the apropriate thread link is harder.

Try working it out from this code I have tried to use:[code]
SqlGT = "SELECT * FROM Threads"

rs.open SqlGT,Qconn
SqlGC = "SELECT COUNT(*) AS RpC FROM Replies WHERE ToThreadId="&rs("ThreadId")
rs.open SqlGC,Qconn
Do While not rs.EOF
Response.Write("<tr>")
Response.Write("<td width=1 height=20>&nbsp;</td>")
Response.Write("<td width=362 align=center valign=middle bgcolor=e5e5e5><font size=2 face=Arial, Helvetica, sans-serif>"&rs("ThreadSubject")&"</font></td>")
Response.Write("<td width=165 align=center valign=middle bgcolor=cccccc><font size=2 face=Arial, Helvetica, sans-serif>"&rs("ThreadStarter"))
Response.Write("</font></td>")
Response.Write("<td width=165 align=center valign=middle bgcolor=cccccc><font size=2 face=Arial, Helvetica, sans-serif>"&rs("RpC"))
Response.Write("</font></td>")
Response.Write("</tr>")
rs.MoveNext
loop%>

whammy
10-19-2002, 01:40 PM
Try reversing the order of your sql queries (that should be all you have to do). Basically, you just redefined "rs" which replaced your recordset with the count from your table (the second thing you are requesting).

Lemme guess, you're getting "Item cannot be found in the collection corresponding to the requested name or ordinal." ;)

Mhtml
10-20-2002, 02:31 AM
That's the one.