469,290 Members | 1,882 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Do While not Statement

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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #1
9 8417
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!
"Ben" <Be************@langley.af.mil> wrote in message
news:#j**************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 19 '05 #2
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.

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


"Ben" <Be************@langley.af.mil> wrote in message
news:#j**************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 19 '05 #3
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.


"Ben" <Be************@langley.af.mil> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 19 '05 #4
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


"Ben" <Be************@langley.af.mil> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 19 '05 #5
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/


"Ray at <%=sLocation%> [MVP]" <myfirstname at lane34 dot com> wrote in
message news:OW**************@TK2MSFTNGP09.phx.gbl...
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


"Ben" <Be************@langley.af.mil> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Jul 19 '05 #6
> 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...


http://www.aspfaq.com/5009
(There there, that feels better.)
Jul 19 '05 #7
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 WHEREreferral='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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #8
Ooh, and I didn't know about 5008. I like that one too. :]

Ray at home

"Aaron Bertrand - MVP" <aa***@TRASHaspfaq.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
http://www.aspfaq.com/5009
(There there, that feels better.)

Jul 19 '05 #9

"Ben" <Be************@langley.af.mil> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
intMale = odoCon.Execute("SELECT COUNT(gender) FROM >tblUsers WHERE
gender='male'").Fields.Item(0).Value
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
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Fábio Mendes | last post: by
13 posts views Thread by eman1000 | last post: by
37 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by Steven Bethard | last post: by
28 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by dspfun | last post: by
23 posts views Thread by florian.loitsch | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.