Print Page | Close Window

random selection of records in a table

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=14124
Printed Date: 30 March 2026 at 10:06am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: random selection of records in a table
Posted By: moti
Subject: random selection of records in a table
Date Posted: 06 March 2005 at 2:42am
how could I make a code to on each page refresh select a record from a table in random

for example in first refresh it will select record with ID 5  and in second refresh it will select Record with ID 45  and ...
 
 
 



Replies:
Posted By: bootcom
Date Posted: 06 March 2005 at 2:53am
Here's a quick snippet of code I use, that basically pulls a random record from the database so you get a different record every refresh
 

' ADO Constant. Dont change this
Const adCmdText = &H0001
 
rs.open strSQL, adocon, 3, , adCmdText

' Generating random number from total number of records
Dim intRnd
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))
' Now moving the cursor to random record number
rs.Move intRnd

 ' Closing the database
rs.Close
Set rs = Nothing


Posted By: moti
Date Posted: 06 March 2005 at 2:58am
thanks but I can't understand above code could you send it
again with a little description
Big smile
Thanks


Posted By: Mart
Date Posted: 06 March 2005 at 3:41am
Not sure about in access but in SQL Server  you can use
 

SELECT * FROM YourTable ORDER BY NEWID()
 
as your SQL Query



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net