Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 How do I limit a query numerically?
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

Alfred
Senior Member

USA
1527 Posts

Posted - 25 October 2003 :  21:24:08  Show Profile  Visit Alfred's Homepage
In the following statement I select all new members of the last 30 days:
quote:
set my_Conn = Server.CreateObject("ADODB.Connection")
my_Conn.Open strConnString

strSql = "SELECT * FROM FORUM_MEMBERS WHERE M_Date > " & DateAdd("d",-30,Now()) & " ORDER BY MEMBER_ID DESC"
set myMEMBERS = my_Conn.Execute(strSql)

I use a loop to display their data
quote:
do while not myMembers.eof
response.write myMembers("M_NAME")
myMembers.movenext
loop
How can I specify that I want to have X records (or loops)?

Alfred
The Battle Group
CREDO

The Impact
Junior Member

Australia
398 Posts

Posted - 25 October 2003 :  21:38:12  Show Profile
Try something like that, remember that you'll need to define I as a variable.
do while not myMembers.eof
for I = 1 to X
response.write myMembers("M_NAME")
myMembers.movenext
next
loop
Replace the X with how many records you want.
Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 25 October 2003 :  22:10:46  Show Profile  Visit Alfred's Homepage
So, this will do ten loops only?
quote:
do while not myMembers.eof
for I = 1 to 10
response.write myMembers("M_NAME")
myMembers.movenext
next
loop


Alfred
The Battle Group
CREDO
Go to Top of Page

The Impact
Junior Member

Australia
398 Posts

Posted - 25 October 2003 :  23:44:36  Show Profile
Yes, that's what I use to limit records.
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 25 October 2003 :  23:46:44  Show Profile  Visit dayve's Homepage
quote:
Originally posted by Alfred

So, this will do ten loops only?
quote:
do while not myMembers.eof
for I = 1 to 10
response.write myMembers("M_NAME")
myMembers.movenext
next
loop





yes, but you should always include a EOF check in case there are less than 10 records. this would actually be a safer loop:

While (Not myMembers.EOF And  myMembersIndex < 10)
 response.write myMembers("M_NAME")
 myMembersIndex = myMembersIndex+1
 myMembers.movenext
Wend


You can do similar loops with Do While and For Next

Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 26 October 2003 :  00:36:17  Show Profile  Visit Alfred's Homepage
Thank you, gentlemen - it works wonderfully!

Alfred
The Battle Group
CREDO
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 26 October 2003 :  00:52:28  Show Profile  Visit dayve's Homepage
quote:
Originally posted by Alfred

Thank you, gentlemen - it works wonderfully!



I just realized the other loop method shown in this thread did in fact have an EOF check. I am just used to seeing the conditional checks done in one line.

Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 26 October 2003 :  07:01:40  Show Profile
Another way to do this is to use the TopSQL function, where NumberOfRecords is the amount you wish to return

strSql = "SELECT * FROM FORUM_MEMBERS WHERE M_Date > " & DateAdd("d",-30,Now()) & " ORDER BY MEMBER_ID DESC"

Set myMEMBERS = my_conn.Execute(TopSQL(strSql, NumberOfRecords))

The UK MkIVs Forum
Go to Top of Page

laser
Advanced Member

Australia
3859 Posts

Posted - 26 October 2003 :  07:21:50  Show Profile
I think you missed something David
Go to Top of Page

chad
New Member

50 Posts

Posted - 26 October 2003 :  13:11:39  Show Profile
I could be mistaken here but:

can't you just say SELECT TOP 10 FROM .....
Go to Top of Page

redbrad0
Advanced Member

USA
3725 Posts

Posted - 26 October 2003 :  13:42:59  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
Chad yes you can

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 26 October 2003 :  14:16:59  Show Profile
quote:
Originally posted by laser

I think you missed something David



What was that

The UK MkIVs Forum
Go to Top of Page

dayve
Forum Moderator

USA
5820 Posts

Posted - 26 October 2003 :  14:35:50  Show Profile  Visit dayve's Homepage
quote:
Originally posted by DavidRhodes

quote:
Originally posted by laser

I think you missed something David



What was that



I think because you put a * instead of a number??

chad is also right, but you should still check for EOF.

Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 26 October 2003 :  14:46:30  Show Profile
SELECT * means select all records.
SELECT TOP 10 is T-SQL isn't it? So it will only work on SQL Server (or am I wrong!). The TopSQL function is part of the snitz base code and will work on all databases

The UK MkIVs Forum
Go to Top of Page

Nikkol
Forum Moderator

USA
6907 Posts

Posted - 26 October 2003 :  14:51:03  Show Profile
select top x ... works in access too. the topsql function uses rowcount for sql server.

Nikkol ~ Help Us Help You | ReadMe | 3.4.03 fixes | security fixes ~
Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 26 October 2003 :  23:13:49  Show Profile  Visit Alfred's Homepage
Wow - watching the experts didcuss finer points is highly educational.
And wow - I actually KNEW one of those points (the '*' standing for all fields)!

Alfred
The Battle Group
CREDO
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.32 seconds. Powered By: Snitz Forums 2000 Version 3.4.07