...

View Full Version : Displaying # records



Ted Varnson
07-22-2004, 04:09 PM
Instead of looping through my access database, I was would like to display the most recent five records posted in the database. how would I go about doing this?

allida77
07-22-2004, 04:18 PM
In sql server I know you can do

Select TOP 5 * FROM tbFOO ordery by datefield

I would guess this works in Access too.

edit:
put in an order by predicate for the date

Ted Varnson
07-22-2004, 04:24 PM
I dont think the server I'm working on has SQL server

Ted Varnson
07-22-2004, 06:33 PM
ok I got that to work, though being a novice I dont know how well its coded.

What I need ultimatly is to ave it display the 5 newest records. My database table has three fields (id, title, post). Im not really sure how to set up the date. Here is my code, maybe someone can show me what to replace or what to add?


<%
' Declaring variables
Dim rs, data_source
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open Server.MapPath("admin/data/data.mdb")
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select TOP 5 * from news", conn


' Looping through the records to show all of them
While Not rs.EOF
Response.Write "<table width=""700"" border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td valign=""top""><table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td class=""cat-2""> <table border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td><table width=""100%"" border=""0"" cellpadding=""0"" cellspacing=""0"" class=""cat-border""><tr><td class=""cat-title"" width=""37""><img src=""images/cat_symbol.gif"" alt="""" width=""38"" height=""22"" /></td><td class=""frame-title"">"
Response.Write rs("title")
Response.Write "</td></tr></table></td></tr></table></td></tr><tr><td><table width=""90%"" border=""0"" align=""center"" cellpadding=""3"" cellspacing=""0""><tr><td class=""boxtext"" valign=""top"">"
Response.Write(Replace(rs("post"), vbCrLf, "<br />"))
Response.Write "</td></tr></table></td></tr></table><br /></td></tr></table>"
rs.MoveNext
Wend
' Done. Now close the Recordset
rs.Close
Set rs = Nothing
%>

Ted Varnson
07-22-2004, 06:36 PM
Nevermind I think I have it. Still feel free to let me know if it is coded poorly. I dont wanna take down the server!


<%
' Declaring variables
Dim rs, data_source
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open Server.MapPath("admin/data/data.mdb")
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select TOP 5 * from news ORDER BY id DESC", conn


' Looping through the records to show all of them
While Not rs.EOF
Response.Write "<table width=""700"" border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td valign=""top""><table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td class=""cat-2""> <table border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td><table width=""100%"" border=""0"" cellpadding=""0"" cellspacing=""0"" class=""cat-border""><tr><td class=""cat-title"" width=""37""><img src=""images/cat_symbol.gif"" alt="""" width=""38"" height=""22"" /></td><td class=""frame-title"">"
Response.Write rs("title")
Response.Write "</td></tr></table></td></tr></table></td></tr><tr><td><table width=""90%"" border=""0"" align=""center"" cellpadding=""3"" cellspacing=""0""><tr><td class=""boxtext"" valign=""top"">"
Response.Write(Replace(rs("post"), vbCrLf, "<br />"))
Response.Write "</td></tr></table></td></tr></table><br /></td></tr></table>"
rs.MoveNext
Wend
' Done. Now close the Recordset
rs.Close
Set rs = Nothing
%>

startbar
07-22-2004, 08:32 PM
looks ok!

i wouldent show the path to your database tho if i were you. :)

glenngv
07-23-2004, 04:27 AM
Or put the db in a directory that's not downloadable, usually just above the web root. Then, you would access it this way:

Server.MapPath("../admin/data/data.mdb")



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum