Do While not Statement

B

Ben

I have two 'Do While Not' statements, that are getting information from
the same recordset. If I comment out the first one I can get the results
for the second one, and vice-versa. Why is this happening? Is it because
it's already at the EOF? If so how do I get it back to the BOF for the
2nd 'Do While Not' statement?

'----------------------------------------
'Create an ADO recordset object
Set rs_Report = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the
database
strSQL = "SELECT gender, referral, youthmember, age FROM tblUsers WHERE
base = '" + Replace(chosenBase, "'", "''") + "'"

'Open the recordset with the SQL query
rs_Report.Open strSQL, adoCon

'-----Gets the total number of male and female users
intMale = 0
intFemale = 0
Do While NOT rs_Report.EOF
If rs_Report("gender") = "male" then
intMale = intMale + 1
Else
intFemale = intFemale + 1
End If
rs_Report.MoveNext()
Loop
'-----Figures out how they heard about the program

Do While NOT rs_Report.EOF
If rs_Report("referral") = "Base Paper" then
intBasePaper = intBasePaper + 1
End IF
rs_Report.MoveNext()
Loop
----------------------------------------------
--Sample Output--
Male = 11
Female = 4

Base Paper =__ "nothing is returned here"

----------------------------------------------
Like I said before If I comment out the first do while not statement I
can get the answers for the second one. If I don't it doesn't even get
inside of the second do while not statement.

I know I can fix it by just having a seperate SQL statement to the
database and get my information seperately, but it seems like a waste of
time. Thanks for any help.

-=Ben
-=To email me take out the joke.
 
S

Steven Burn

Why've you got them in 2 DW loops?

Do While NOT rs_Report.EOF
If rs_Report("gender") = "male" then
intMale = intMale + 1
Else
intFemale = intFemale + 1
End If

'-----Figures out how they heard about the program

If rs_Report("referral") = "Base Paper" then
intBasePaper = intBasePaper + 1
End IF
rs_Report.MoveNext
Loop

--

Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!
 
A

Aaron Bertrand - MVP

Why do you need to perform the loop twice? One problem is that in your
first loop, you move to the EOF. When you start your second loop, you're
already at EOF, so where is it supposed to go? The Do never enters...

Isn't this simply re-written as:

do while not rs.eof
' do stuff from first loop
' do stuff from second loop
rs.movenext
loop

You should also review http://www.aspfaq.com/2191 ... I see no reason for
ADODB.Recordset here.
 
B

Bryan Harrington

Previous responses provided a better solution... and Aaron hints as to why
the second loop returns nothing. That being said..and noting that *I* would
use the aformentioned solutions.. to answer your specific question; to make
your code work add rs_Report.Movefirst prior to starting the second loop.
 
R

Ray at

How about just doing a count query or queries?


intMale = odoCon.Execute("SELECT COUNT(gender) FROM tblUsers WHERE
gender='male'").Fields.Item(0).Value
intFemale = odoCon.Execute("SELECT COUNT(gender) FROM tblUsers WHERE
gender='female'").Fields.Item(0).Value
intBasePaper = odoCon.Execute("SELECT COUNT(referral) FROM tblUsers WHERE
referral='Base Paper'").Fields.Item(0).Value

(Some would argue against the methods above, however.)

For sexes, you could also do "SELECT gender,COUNT(gender) FROM tblUsers
GROUP BY gender ORDER BY gender" to gram male and female at once.

Ray at work
 
A

Aaron Bertrand - MVP

Depending on the database and version, you can probably do this in one
query, returning one row, and eliminating the need to loop at all, never
mind multiple times.

For SQL Server:

SELECT
male = SUM(CASE gender WHEN 'male' THEN 1 ELSE 0 END),
female = SUM(CASE gender WHEN 'female' THEN 1 ELSE 0 END),
basePaper = SUM(CASE referral WHEN 'Base Paper' THEN 1 ELSE 0 END)
FROM tblUsers

I'm continually amazed that people think it is too much work, or not
valuable enough, to include what type of database they are working with...

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
 
B

Ben

do while not rs.eof
' do stuff from first loop
' do stuff from second loop
rs.movenext
loop

Aaron: This is what Im going to end up doing, thanks for the insight, Im
still a beginner at this.
intMale = odoCon.Execute("SELECT COUNT(gender) FROM >tblUsers WHERE
gender='male'").Fields.Item(0).Value
intFemale = odoCon.Execute("SELECT COUNT(gender) FROM >tblUsers WHERE
gender='female'").Fields.Item(0).Value
intBasePaper = odoCon.Execute("SELECT COUNT(referral) FROM >tblUsers WHERE
referral='Base Paper'").Fields.Item(0).Value

Ray: Why would some argue against this method?
I'm continually amazed that people think it is too much >work, or not
valuable enough, to include what type of database they are >working
with...

Aaron: Sorry about the oversight,it's an Access database until reading
the article I didn't realize that it was important, but now I know
better. Im still new to all of this(SQL, ASP, Access), so any help is
appreciated.

-=Ben
-=To email me take out the joke.
 
R

Ray at

Ray: Why would some argue against this method?

The argument would come from doing this:

someValue = ADOObject.Execute("a query").Fields.Item(0).Value

as opposed to:

Set rs = ADOObject.Execute("a query")
someValue = rs.Fields.Item(0).Value
rs.Close
Set rs = Nothing


When you implicitly create an object as is done in code sample 1, that
object won't be destroyed until the ASP engine is finished processing the
whole page and is cleaning up. So, it is not the most efficient way of
doing things. You typically want to create your recordset (or any object),
just before you need it and close and destroy it as soon as you no longer
need it. Does this matter or will you notice a difference in page load
times? No. If you get to that point, you won't be using Access anymore
anyway. :]

Ray at home
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top