PDA

Click to See Complete Forum and Search --> : Read from database in reverse.


sail3005
Mar 11th, 2001, 08:07 PM
I am trying to make just a very simple guestbook to fiddle around with. I want to read from the records in reverse so that the newest entry appears on top. This code gives me errors:

I set it to go to the last record, and then tell the loop to go backwards, but i get errors. Can anyone help me accomplish this task? Here is the code:


<%

Dim objRS, strName, strComment
Set objRS = Server.CreateObject("ADODB.Recordset")

objRS.Open "main", objConn, , adLockOptimistic, adCmdTable

strName = Request.Form("name")
strComment = Request.Form("comment")

objRS.AddNew
objRS("Name") = strName
objRS("Message") = strComment
objRS.Update
objRS.MoveLast


If objRS.EOF Then
Response.Write "Database is empty"

Else

Do While Not objRS.EOF
Response.Write "<pre>"
Response.Write "Name: " & objRS("Name") & "<br>"
Response.Write "Comment: " & objRS("Message") & "<br>"
Response.Write "<hr><p>"
Response.Write "</pre>"
objRS.MovePrevious
Loop

End If

objRS.Close
Set objRS = Nothing


objConn.Close
Set objConn = Nothing

%>

Active
Mar 12th, 2001, 12:51 AM
This Would Be simple If you used an SQL statement
Instead of using adcmdTable.

An SQL satement allows you to get Records in any ordered manner.

Suppose I am Requesting a recordset With Records
Odered such that the last entry is the first Record in the
Recordset

I would Use something like..

Select * from Myatble Ordered By RecordID Desc

Desc is descending !

So the change you have to make is..

SQL = "Select * from Mytable Ordered By RecordID Desc"

objRS.Open SQL,objConn,adOpenKeyset ,adLockOptimistic,adCmdText

and You have it...

sail3005
Mar 12th, 2001, 08:30 PM
cool, thanks for the info on the SQL statments, i will look into those more.

I figured it out by using a sort thing, and a cursor thing. I don't fully understand them yet tthough...