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

RANDOM RECORD

Status
Not open for further replies.

nwt002

MIS
Jun 16, 2005
58
US
Hello,

Can anyone help me with this script. I'm trying to create a webpage that displays one random record from a database everytime the asp webpage is freshed. I used frontpage to create the webpage, and some members from other forums have given me some script and queries to use. below is some of the script and query that i recently recieved, but I am not familiar with opening or creating database connections or recordsets and I keep getting errors. the original code that i was given was this:

<%
' Initialize ASP RND() function
Randomize()
intRandomNumber = Int (1000*Rnd)+1

' Return 3 random records
strSQL = "SELECT TOP 1 Key, quote, Rnd(" & -1 * (intRandomNumber) & "*Key)" & "FROM Results" & "ORDER BY 3"

Set objRS = objConn.Execute(strSQL)
%>


but it gives me an "Object required:" error. so i looked in some other forums to see how to open database connections and recordset, and i've tried different variation, but i get different errors each time. The code that i have now is listed below. Does any of it look close to being right? Please let me know if anyone has any suggestions or if you need any more info. Thanks!

Code:
<%
Dim objConn
Dim objRS

objConn = Server.CreateObject("ADODB.Connection")
objconn.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};" & "DBQ=/fpdb/quotes.mdb"


'Initialize ASP RND() function
Randomize()
intRandomNumber = Int(1000*Rnd)+1

'Return 3 random records
strSQL="SELECT TOP 3 Key, quote, Rnd(" & -1 * (intRandomNumber)&"*Key)" & "FROM Results " & "ORDER BY 3"

objConn.Open
Set objRS=objConn.Execute(strSQL)
%>
 
>intRandomNumber = Int (1000*Rnd)+1
So intRandomNumber is some number ranging between [1,1000]. That's fine.

But what kind of animal is this, eventually appeared in the sql statement?
[tt] Rnd(-1 * (intRandomNumber)*Key)[/tt]
 
I'm not sure, that is what the other member gave me. The other query that a different member gave me is:

Code:
SELECT TOP 1 key, quote 
FROM Results, (Select Min(key) as MinValue FROM Results) TMin 
WHERE Key>=(((Select Max(key) FROM Results) - TMin.MinValue) * Rnd + TMin.MinValue)

I have tested this query in Access and it appears to work great. Everytime I rerun it, it returns a different random record. The problem is, whenever I add this query to the webpage it returns the same record everytime I refresh the page. It's like it's not rerunning the query when I refresh, even when I clear the cache.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top