Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 14th, 2015, 12:00 PM
Authorized User
Points: 146, Level: 2
Points: 146, Level: 2 Points: 146, Level: 2 Points: 146, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , .
Posts: 31
Thanks: 1
Thanked 0 Times in 0 Posts
Question While Wend statement question

I have a While Wend statement I use quite often to add up the total of a field on an Admin page. Easy for an Admin to see how many people signed up for an event. Works perfectly.

I now would like to use the same statement (with a few changes) to do the opposite, count down to zero.

Original working statement:

HowMany1Tot = 0
While not rs.eof
HowMany1Tot = (HowMany1Tot + rs("HowMany1"))
Rs.Movenext
Wend
Rs.MoveFirst

What I would like it to do now:

HowMany1Tot = 575
While not rs.eof
HowMany1Tot = (HowMany1Tot - rs("HowMany1"))
Rs.Movenext
Wend
Rs.MoveFirst

But, it does not...it won't even show up on the page with my <% = HowMany1Tot %> (which works in the original example).

What am I doing wrong? Can the While Wend statement add but not subtract? Confused....Help!

Thanks
JackieW
 
Old January 14th, 2015, 06:02 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Hello. Yes you can subtract inside the loop, you can do what ever inside any loop. The type of loop does not decide what can be done inside the loop, it is not connected in any way.

To be honest I can not see any issue with your code, in fact it does work and displays the correct result for me. I would say there is a simple issue somewhere, maybe the placement of <% = HowMany1Tot %>. I suggest you place a simple response.write inside your loop and trace it from there. here is me usin your code whith these response.writes in place. I have added a query of my own but otherwise its all your code:

Code:
dim HowMany1Tot, rs
strsql = "select Top 14 obsDate, currentScore from diseaseScores where diseaseID=1 AND pCode=2640 order by obsDate desc;"
set rs = conn.execute(strsql)
HowMany1Tot = 0
While not rs.eof
   HowMany1Tot = (HowMany1Tot + rs("currentScore"))
   response.write HowMany1Tot & " = my increasing running total<bR>"
   Rs.Movenext
Wend
response.write HowMany1Tot & " = my increasing grandTotal<bR><br>"

Rs.MoveFirst

HowMany1Tot = 129
While not rs.eof
   HowMany1Tot = (HowMany1Tot - rs("currentScore"))
   response.write HowMany1Tot & " = my decreasing running total<bR>"
   Rs.Movenext
Wend
response.write HowMany1Tot & " = my decreasing grandTotal<bR><br>"
I am more a fan of using the getRows method to place a record set into an array. Its a very very efficient way of looping. Also notice the position of set rs = nothing. It means your record set is using local memory and not connected the the database. This makes large record sets load three times faster on the page. here is an example using your code:
Code:
dim HowMany1Tot, rs, myArray, i
strsql = "select Top 14 currentScore from diseaseScores where diseaseID=1 AND pCode=2640 order by obsDate desc;"
set rs = conn.execute(strsql)
HowMany1Tot = 0
myArray = rs.getRows()
se rs = nothing
for i = 0 to uBound(myArray, 2)
   HowMany1Tot = (HowMany1Tot + myArray(0,i))
   response.write HowMany1Tot & " = my increasing total<bR>"
next
response.write HowMany1Tot & " = my increasing GrandTotal<bR><br>"

HowMany1Tot = 129
for i = 0 to uBound(myArray, 2)
   HowMany1Tot = (HowMany1Tot - myArray(0,i))
   response.write HowMany1Tot & " = my decreasing total<bR>"
next
response.write HowMany1Tot & " = my decreasing GrandTotal<bR><br>"
__________________
Wind is your friend
Matt

Last edited by mat41; January 14th, 2015 at 06:05 PM..
The Following User Says Thank You to mat41 For This Useful Post:
jackiew (January 15th, 2015)
 
Old January 15th, 2015, 11:30 AM
Authorized User
Points: 146, Level: 2
Points: 146, Level: 2 Points: 146, Level: 2 Points: 146, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: , , .
Posts: 31
Thanks: 1
Thanked 0 Times in 0 Posts
Talking Works like a charm

Thanks! It worked like a charm.

I'm also looking at your getRow method and figuring that one out to replace the While Wend statement. Now that the page is working I have time to learn how to do things better.

Thanks again,
JackieW
 
Old January 15th, 2015, 11:39 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

No worries, glad to help.

Personally I hardly ever use while loops. I am more a fan of do until loops. That said, im sure this is a personal preference.

I use getRows 70% of the time. I would not use it for a query that is only run and used once that is not large. I always use it for large record sets in loops and especially record sets which I intend on using more than once. IMO doing the whole moveFirst thing is slow and inefficient.

A great test is to take a page which is heavy on database content that is running slow. Change all the record set to arrays (make sure you SET RS = NOTHING immediately after calling getRows) and watch the page load in under half the time.
__________________
Wind is your friend
Matt




Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Question About Switch Statement AgentSmith ASP.NET 3.5 Basics 1 April 22nd, 2009 11:51 AM
Select statement question????? RinoDM SQL Server 2000 11 March 18th, 2009 12:07 PM
Question on select statement RinoDM SQL Server 2000 3 January 19th, 2007 10:21 PM
IIF Statement Question fastcorvette Access 8 October 31st, 2003 11:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.