By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

show last 10 records

P: n/a
I have this script for showing news on a page, but i want it to only
show the last 10 records, as in the 10 records that were added to the
database last. the script shows the entries in descending order. Here
is a code snippet

Do While not rsNews.EOF
Response.Write("<table class=""tableborder"" border=""0""
cellspacing=""0"" cellpadding""0"" width=""100%"" ")
Response.Write("<tr>")
Response.Write("<td class=""tabletitle"" width=""100%"">")
Response.Write(rsNews("Title"))
Response.Write(" Posted on ")
Response.Write(rsNews("Date"))
Response.Write("</td></tr>")
Response.Write("<tr>")
Response.Write("<td class=""normal"" width=""100%"">")
Response.Write(rsNews("Body"))
Response.Write("</td></tr></table><br><br>")
rsNews.MoveNext
Loop

i tried adding a counter and making this

counter = 0
Do While not rsNews.EOF AND Counter <=10
'Response.Write code
counter = counter + 1
Loop

this didnt work either, Anyone know how i can do this? isnt there a way
to do it with an sql query?

any help much appreciated, thanks

Chris

Nov 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Christo" <cm*****@googlemail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
I have this script for showing news on a page, but i want it to only
show the last 10 records, as in the 10 records that were added to the
database last. the script shows the entries in descending order. Here
is a code snippet

Do While not rsNews.EOF
Response.Write("<table class=""tableborder"" border=""0""
cellspacing=""0"" cellpadding""0"" width=""100%"" ")
Response.Write("<tr>")
Response.Write("<td class=""tabletitle"" width=""100%"">")
Response.Write(rsNews("Title"))
Response.Write(" Posted on ")
Response.Write(rsNews("Date"))
Response.Write("</td></tr>")
Response.Write("<tr>")
Response.Write("<td class=""normal"" width=""100%"">")
Response.Write(rsNews("Body"))
Response.Write("</td></tr></table><br><br>")
rsNews.MoveNext
Loop

i tried adding a counter and making this

counter = 0
Do While not rsNews.EOF AND Counter <=10
'Response.Write code
counter = counter + 1
Loop

this didnt work either, Anyone know how i can do this? isnt there a way
to do it with an sql query?

any help much appreciated, thanks

Chris
Modify the query used create the recordset:-

SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC

However it does return them in reverse order. If that's seriously out of the
question use:-

SELECT * FROM (
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC)
ORDER BY afield _that_you_can_sort_by ASC

I'm not sure Access will be happy with that although SQL Server will (which
is why when asking a DB related question you should state the DB in use).

Nov 8 '06 #2

P: n/a
"Anthony Jones" <An*@yadayadayada.comwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
>
"Christo" <cm*****@googlemail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>I have this script for showing news on a page, but i want it to only
show the last 10 records, as in the 10 records that were added to the
database last. the script shows the entries in descending order. Here
is a code snippet

Do While not rsNews.EOF
Response.Write("<table class=""tableborder"" border=""0""
cellspacing=""0"" cellpadding""0"" width=""100%"" ")
Response.Write("<tr>")
Response.Write("<td class=""tabletitle"" width=""100%"">")
Response.Write(rsNews("Title"))
Response.Write(" Posted on ")
Response.Write(rsNews("Date"))
Response.Write("</td></tr>")
Response.Write("<tr>")
Response.Write("<td class=""normal"" width=""100%"">")
Response.Write(rsNews("Body"))
Response.Write("</td></tr></table><br><br>")
rsNews.MoveNext
Loop

i tried adding a counter and making this

counter = 0
Do While not rsNews.EOF AND Counter <=10
'Response.Write code
counter = counter + 1
Loop

this didnt work either, Anyone know how i can do this? isnt there a way
to do it with an sql query?

any help much appreciated, thanks

Chris

Modify the query used create the recordset:-

SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC

However it does return them in reverse order. If that's seriously out of
the
question use:-

SELECT * FROM (
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC)
ORDER BY afield _that_you_can_sort_by ASC

I'm not sure Access will be happy with that although SQL Server will
(which
is why when asking a DB related question you should state the DB in use).
Access is happy with that, as long as you include afield
_that_you_can_sort_by ASC in the SELECT TOP 10 clause. Although typically,
if you are pulling the most recent 10 stories from a database, you would
indeed want them in reverse order, so that the newest appears at the top of
the list.

--
Mike Brind
Nov 8 '06 #3

P: n/a
Thank you :-) I will test this out, unfortunately a new error popped up
in relation to the same set of scripts once that one is fixed ill get
this going :-) thanks appreciated.

Mike Brind wrote:
"Anthony Jones" <An*@yadayadayada.comwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...

"Christo" <cm*****@googlemail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
I have this script for showing news on a page, but i want it to only
show the last 10 records, as in the 10 records that were added to the
database last. the script shows the entries in descending order. Here
is a code snippet

Do While not rsNews.EOF
Response.Write("<table class=""tableborder"" border=""0""
cellspacing=""0"" cellpadding""0"" width=""100%"" ")
Response.Write("<tr>")
Response.Write("<td class=""tabletitle"" width=""100%"">")
Response.Write(rsNews("Title"))
Response.Write(" Posted on ")
Response.Write(rsNews("Date"))
Response.Write("</td></tr>")
Response.Write("<tr>")
Response.Write("<td class=""normal"" width=""100%"">")
Response.Write(rsNews("Body"))
Response.Write("</td></tr></table><br><br>")
rsNews.MoveNext
Loop

i tried adding a counter and making this

counter = 0
Do While not rsNews.EOF AND Counter <=10
'Response.Write code
counter = counter + 1
Loop

this didnt work either, Anyone know how i can do this? isnt there a way
to do it with an sql query?

any help much appreciated, thanks

Chris
Modify the query used create the recordset:-

SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC

However it does return them in reverse order. If that's seriously out of
the
question use:-

SELECT * FROM (
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC)
ORDER BY afield _that_you_can_sort_by ASC

I'm not sure Access will be happy with that although SQL Server will
(which
is why when asking a DB related question you should state the DB in use).

Access is happy with that, as long as you include afield
_that_you_can_sort_by ASC in the SELECT TOP 10 clause. Although typically,
if you are pulling the most recent 10 stories from a database, you would
indeed want them in reverse order, so that the newest appears at the top of
the list.

--
Mike Brind
Nov 13 '06 #4

P: n/a
Hi Christo,
Just in case that didnt work, try reversing your SQL statement
instead of ORDER BY [Field_Name] DESC make it ORDER BY [Field_Name] ASC
and then to view only ten records
for i=1 to 10
if rsNews.eof=false then
Response.write rsNews.fields("News")
end if
next
What i did is just listed the bottom records top
for example:
if the table in the descending order looks like this:
Record 1
Record 2
Record 3
Record 4

and you want to print the last two records
then order it by ascending
so it will look like this
Record 4
Record 3
Record 2
Record 1

and then run a loop that will print out the first two, which in fact
the last two but in different order.

Hope this helps

Best Regards
Firas S Assaad
Christo wrote:
Thank you :-) I will test this out, unfortunately a new error popped up
in relation to the same set of scripts once that one is fixed ill get
this going :-) thanks appreciated.

Mike Brind wrote:
"Anthony Jones" <An*@yadayadayada.comwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
>
"Christo" <cm*****@googlemail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>I have this script for showing news on a page, but i want it to only
>show the last 10 records, as in the 10 records that were added to the
>database last. the script shows the entries in descending order. Here
>is a code snippet
>>
>Do While not rsNews.EOF
>Response.Write("<table class=""tableborder"" border=""0""
>cellspacing=""0"" cellpadding""0"" width=""100%"" ")
>Response.Write("<tr>")
>Response.Write("<td class=""tabletitle"" width=""100%"">")
>Response.Write(rsNews("Title"))
>Response.Write(" Posted on ")
>Response.Write(rsNews("Date"))
>Response.Write("</td></tr>")
>Response.Write("<tr>")
>Response.Write("<td class=""normal"" width=""100%"">")
>Response.Write(rsNews("Body"))
>Response.Write("</td></tr></table><br><br>")
>rsNews.MoveNext
>Loop
>>
>i tried adding a counter and making this
>>
>counter = 0
>Do While not rsNews.EOF AND Counter <=10
> 'Response.Write code
> counter = counter + 1
>Loop
>>
>this didnt work either, Anyone know how i can do this? isnt there a way
>to do it with an sql query?
>>
>any help much appreciated, thanks
>>
>Chris
>>
>
Modify the query used create the recordset:-
>
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC
>
However it does return them in reverse order. If that's seriously out of
the
question use:-
>
SELECT * FROM (
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC)
ORDER BY afield _that_you_can_sort_by ASC
>
I'm not sure Access will be happy with that although SQL Server will
(which
is why when asking a DB related question you should state the DB in use).
>
Access is happy with that, as long as you include afield
_that_you_can_sort_by ASC in the SELECT TOP 10 clause. Although typically,
if you are pulling the most recent 10 stories from a database, you would
indeed want them in reverse order, so that the newest appears at the top of
the list.

--
Mike Brind
Nov 14 '06 #5

P: n/a
That's a very poor idea. What possible logic is there in replacing DESC
which gets just the records you want, to ASC and getting every record in the
table, only to discard all but 10 when you process them?

--
Mike Brind
"Firas S Assaad" <fi******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi Christo,
Just in case that didnt work, try reversing your SQL statement
instead of ORDER BY [Field_Name] DESC make it ORDER BY [Field_Name] ASC
and then to view only ten records
for i=1 to 10
if rsNews.eof=false then
Response.write rsNews.fields("News")
end if
next
What i did is just listed the bottom records top
for example:
if the table in the descending order looks like this:
Record 1
Record 2
Record 3
Record 4

and you want to print the last two records
then order it by ascending
so it will look like this
Record 4
Record 3
Record 2
Record 1

and then run a loop that will print out the first two, which in fact
the last two but in different order.

Hope this helps

Best Regards
Firas S Assaad
Christo wrote:
>Thank you :-) I will test this out, unfortunately a new error popped up
in relation to the same set of scripts once that one is fixed ill get
this going :-) thanks appreciated.

Mike Brind wrote:
"Anthony Jones" <An*@yadayadayada.comwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...

"Christo" <cm*****@googlemail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
I have this script for showing news on a page, but i want it to only
show the last 10 records, as in the 10 records that were added to
the
database last. the script shows the entries in descending order.
Here
is a code snippet

Do While not rsNews.EOF
Response.Write("<table class=""tableborder"" border=""0""
cellspacing=""0"" cellpadding""0"" width=""100%"" ")
Response.Write("<tr>")
Response.Write("<td class=""tabletitle"" width=""100%"">")
Response.Write(rsNews("Title"))
Response.Write(" Posted on ")
Response.Write(rsNews("Date"))
Response.Write("</td></tr>")
Response.Write("<tr>")
Response.Write("<td class=""normal"" width=""100%"">")
Response.Write(rsNews("Body"))
Response.Write("</td></tr></table><br><br>")
rsNews.MoveNext
Loop

i tried adding a counter and making this

counter = 0
Do While not rsNews.EOF AND Counter <=10
'Response.Write code
counter = counter + 1
Loop

this didnt work either, Anyone know how i can do this? isnt there a
way
to do it with an sql query?

any help much appreciated, thanks

Chris
Modify the query used create the recordset:-

SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC

However it does return them in reverse order. If that's seriously out
of
the
question use:-

SELECT * FROM (
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC)
ORDER BY afield _that_you_can_sort_by ASC

I'm not sure Access will be happy with that although SQL Server will
(which
is why when asking a DB related question you should state the DB in
use).
Access is happy with that, as long as you include afield
_that_you_can_sort_by ASC in the SELECT TOP 10 clause. Although
typically,
if you are pulling the most recent 10 stories from a database, you
would
indeed want them in reverse order, so that the newest appears at the
top of
the list.

--
Mike Brind

Nov 14 '06 #6

P: n/a
Come on man,
Im just trying to help. This might be a poor idea, ButI m not a
professional here, so im learning too.
Mike Brind wrote:
That's a very poor idea. What possible logic is there in replacing DESC
which gets just the records you want, to ASC and getting every record in the
table, only to discard all but 10 when you process them?

--
Mike Brind
"Firas S Assaad" <fi******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi Christo,
Just in case that didnt work, try reversing your SQL statement
instead of ORDER BY [Field_Name] DESC make it ORDER BY [Field_Name] ASC
and then to view only ten records
for i=1 to 10
if rsNews.eof=false then
Response.write rsNews.fields("News")
end if
next
What i did is just listed the bottom records top
for example:
if the table in the descending order looks like this:
Record 1
Record 2
Record 3
Record 4

and you want to print the last two records
then order it by ascending
so it will look like this
Record 4
Record 3
Record 2
Record 1

and then run a loop that will print out the first two, which in fact
the last two but in different order.

Hope this helps

Best Regards
Firas S Assaad
Christo wrote:
Thank you :-) I will test this out, unfortunately a new error popped up
in relation to the same set of scripts once that one is fixed ill get
this going :-) thanks appreciated.

Mike Brind wrote:
"Anthony Jones" <An*@yadayadayada.comwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
>
"Christo" <cm*****@googlemail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>I have this script for showing news on a page, but i want it to only
>show the last 10 records, as in the 10 records that were added to
>the
>database last. the script shows the entries in descending order.
>Here
>is a code snippet
>>
>Do While not rsNews.EOF
>Response.Write("<table class=""tableborder"" border=""0""
>cellspacing=""0"" cellpadding""0"" width=""100%"" ")
>Response.Write("<tr>")
>Response.Write("<td class=""tabletitle"" width=""100%"">")
>Response.Write(rsNews("Title"))
>Response.Write(" Posted on ")
>Response.Write(rsNews("Date"))
>Response.Write("</td></tr>")
>Response.Write("<tr>")
>Response.Write("<td class=""normal"" width=""100%"">")
>Response.Write(rsNews("Body"))
>Response.Write("</td></tr></table><br><br>")
>rsNews.MoveNext
>Loop
>>
>i tried adding a counter and making this
>>
>counter = 0
>Do While not rsNews.EOF AND Counter <=10
> 'Response.Write code
> counter = counter + 1
>Loop
>>
>this didnt work either, Anyone know how i can do this? isnt there a
>way
>to do it with an sql query?
>>
>any help much appreciated, thanks
>>
>Chris
>>
>
Modify the query used create the recordset:-
>
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC
>
However it does return them in reverse order. If that's seriously out
of
the
question use:-
>
SELECT * FROM (
SELECT TOP 10 YourFields FROM YourTable ORDER BY afield
_that_you_can_sort_by DESC)
ORDER BY afield _that_you_can_sort_by ASC
>
I'm not sure Access will be happy with that although SQL Server will
(which
is why when asking a DB related question you should state the DB in
use).
>

Access is happy with that, as long as you include afield
_that_you_can_sort_by ASC in the SELECT TOP 10 clause. Although
typically,
if you are pulling the most recent 10 stories from a database, you
would
indeed want them in reverse order, so that the newest appears at the
top of
the list.

--
Mike Brind
Nov 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.