Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show Last Record 1

Status
Not open for further replies.

apex82

Programmer
Mar 2, 2009
127
GB
On my asp page I use this to loop through a recordset:

Code:
While Not obj_RS1.EOF
        Response.Write "<span class=”form”>" obj_RS1("Car") & "</ span>"
        obj_RS1.MoveNext
Wend

How do I just show the last record?

Thanks.
 
Thanks wvdba,

I've tried that but I receive an error:

Error Type:
Microsoft SQL Server Native Client 10.0 (0x80040E24)
Rowset does not support fetching backward.
 
if you want to keep your record set open to do more with it, then use something like this:

Code:
obj_RS.movefirst
ndx = 1 ' an index
ttlRecords = obj_RS1.recordcount
do while ndx < ttlRecords
obj_RS1.movenext
loop
response.write obj_RS1("Car")

If you are just trying to retrive the last record then close the recordset, adjust your query to do this by either sorting it so that the record you want is at the top then a movefirst will give you the record you want or select only the record you want -

For example, if you have a date field and you want the lastest date:

Select * from table order by dateField desc;

or

Select top 1 * from table order by dateField desc;



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
That last error seems that perhaps your recordset is in the default firehose forward-only style.

Try using a client side cursor and if that doesn't work post your connection string.

If you only want the last record and are not using thte other records elsewhere on the page then the best idea is to change your SQL so that your recordset only ever contains 0 or 1 record. Suppose you wanted to show the last created record you could change your SQL so it does something like this:

[tt]SELECT TOP 1 * FROM MyTable ORDER BY CreateDate DESC[/tt]

Obviously that was just an example but you write a query to do it in the SQL syntax to match your database type.
 
to answer the question directly : rs.movelast (recordset object only)

to answer the question more indirectly, you're better off not pulling a whole recordset for a single record display and that was covered by Sheco and vicvirk nicely

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never in the 'loop' " - DreX 2005
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top