Print Page | Close Window

HELP!! Select a record at random

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=11321
Printed Date: 30 March 2026 at 4:12am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: HELP!! Select a record at random
Posted By: WebDever
Subject: HELP!! Select a record at random
Date Posted: 27 July 2004 at 2:56pm

Hi all,

HELP!! - I'd like to select a record by not using a field name to filter. For example, here's what I'd usually do:

SELECT * FROM tblTest WHERE tblTest.ID = " & lngID & "

BUT I'd prefer to do it a bit like this:

SELECT TOP " & intRandom & " * FROM tblTest

As you can see here, this will sonly select 2 records, I want to select one only. Basically what I'm trying to is select a random record in the table. But I don't have a clue how to it, please help

NOTE: I'm using ASP/Vb-Script to code me pages

Thanks for reading



-------------
--Ashley



Replies:
Posted By: dj air
Date Posted: 27 July 2004 at 3:33pm

you could try this

bring in all records (without using TOP ....)

then use the following

'Max record value
rndMax = CInt(connection.RecordCount)
connection.MoveFirst

Dim rndNumber
Randomize Timer
rndNumber = Int(RND * rndMax)
connection.Move rndNumber

that will select a random record from the selected



Posted By: ljamal
Date Posted: 27 July 2004 at 5:41pm
Access or MSSQL?
MSSQL has a function called NEWID() that is randomly generated that can be used to sort.

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: WebDever
Date Posted: 27 July 2004 at 6:44pm
Thanks dj air! Appreciate the help, I owe ya one m8

-------------
--Ashley


Posted By: Bunce
Date Posted: 28 July 2004 at 4:42am
http://www.adopenstatic.com/faq/randomrecord.asp

-------------
There have been many, many posts made throughout the world...
This was one of them.



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