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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a predifined number of records from the DB 1

Status
Not open for further replies.

Herminio

Technical User
May 10, 2002
189
PT
I have a page where i have to put some news, the problem is that i only want the 3 last news to be shown, how can i do that?

 

I believe this will work for you:

Select * from news order by news_id desc LIMIT 3
www.vzio.com
ASP WEB DEVELOPMENT



 
if i use your sugestion it gaves me an error on the order by clause

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][Microsoft Access de ODBC Controler] Syntax eroor in ORDER BY clause.

/opiniao/testnews.asp, line6
 
Just look at this, it's driving me nuts

<%
Set conn = Server.CreateObject(&quot;ADODB.connection&quot;)
strCon = &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;news.mdb&quot;)
conn.open strcon
sql = &quot;SELECT News.News, News.Data FROM News ORDER BY News.idNews DESC;&quot;
rsNews = conn.execute(sql)
%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<%
do while not rsNews.eof
%>
<%=rsNews(&quot;data&quot;)%><%=rsNews(&quot;news&quot;)%>
<%
rsNews.movenext
loop
%>
</BODY>
</HTML>

It just says this:

Microsoft VBScript error '800a01b6'

The object doesn't support this proprerty or method: 'eof'

/opiniao/testnews.asp, line14

Why is this happening?
 
SET rsNews = conn.Execute(sql)

You must use SET when creating an instance of an object.
 
Thanks, i've managed to see that, but i can't seem to be able to use LIMIT in the sql query

strNews = &quot;SELECT * FROM News ORDER BY idNews DESC LIMIT 3&quot;
set news = conn.execute(strNews)

This gives me a syntax error on the order by clause

Why is this happening?
 
LIMIT is not valid. Use TOP instead:

Code:
SELECT TOP 3 *
FROM news
ORDER BY idnews DESC

I have just tried using TOP in Access 97 and it worked OK.
 
Try
strNews = &quot;SELECT TOP 3 FROM News ORDER BY idNews DESC&quot;
_______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
You must include the * after the TOP keyword (or the column list if you only want certain columns):

Code:
SELECT TOP 3 col1, col2 FROM table

SELECT TOP 3 * FROM table
 
Doesn't work either

Error:

Microsoft OLE DB
Provider for ODBC Drivers error '80040e14'

The SELECT instruction includes a missing argument or reserved word .....
 
Doesn't work either

Error:

Microsoft OLE DB
Provider for ODBC Drivers error '80040e14'

The SELECT instruction includes a missing argument or reserved word .....
i don't know if that's important but one of the fields have a date
 
Thanks guys,

this is working fine

SELECT TOP 3 * FROM News ORDER BY idNews DESC

 
good typo catch JamesLean.

_______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
What version of Access are you using? Can you copy/paste the code you have now.
 
I'm using Access XP.

This is the code

<%
Set conn = Server.CreateObject(&quot;ADODB.connection&quot;)
strCon = &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;news.mdb&quot;)
conn.open strcon
strNews = &quot;SELECT TOP 3 * FROM News ORDER BY idNews DESC&quot;
set news = conn.execute(strNews)
%>
 
Sorry, I must have posted while you were saying it was all OK! Glad it's working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top