473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

show last 10 records

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
6 2640

"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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
0
by: Tom Kaminski [MVP] | last post by:
I want to show a table of master records, with the right most column displayed as a comma (or space) delimited list of details. The practical application of this is a thesaurus, where the master...
15
by: Joachim | last post by:
Hi, Als a beginner, I am looking for a way to show records My code is Private sub Connection( Dim odbconn_Pro As OleDbConnectio Dim odbcomm_Pro As OleDbComman Dim odbdare_Pro As...
1
by: Galka | last post by:
Hello I have a form to enter names and some other personal information. When a name is entered, it is checked against existing records: maybe, such name was entered before? If yes, user is...
1
by: peirob2006 | last post by:
Hello, I am somewhat familliar with the VB6 way of doing things (AKA haven't the foggiest clue) I know how to create the DataReport in VB6 and How to Connect the Database....but...how do I make...
4
by: kdubble | last post by:
Hi I am trying to get the results of a query to show only unique student records (not duplicates). Is there a simple way to make the criteria field do this? I am not too familiar with SQL. ...
3
by: gbergeson | last post by:
Please be patient with me. I'm a newbie and the LAN Nazi's have blocked this site. This means I can only asked questions from home and, of course, the code for this project is at work. I'm...
1
by: premdasp | last post by:
Dear Sirs, I tried your valuable commands (below mentioned) but I got last record. Private Sub Form_Current() Dim rs As Recordset Set rs = .Form.RecordsetClone If rs.RecordCount Then...
8
by: zufie | last post by:
Hi, I created some forms using the "Create form using Wizard". However, I cannot view the forms I created with Wizard unless I click on the Design View icon. I checked the forms' properties...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.