Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - HELP!! Select a record at random
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

HELP!! Select a record at random

 Post Reply Post Reply
Author
WebDever View Drop Down
Newbie
Newbie


Joined: 28 September 2003
Location: United Kingdom
Status: Offline
Points: 36
Post Options Post Options   Thanks (0) Thanks(0)   Quote WebDever Quote  Post ReplyReply Direct Link To This Post Topic: HELP!! Select a record at random
    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



Edited by WebDever
Back to Top
dj air View Drop Down
Senior Member
Senior Member
Avatar

Joined: 05 April 2002
Location: United Kingdom
Status: Offline
Points: 3627
Post Options Post Options   Thanks (0) Thanks(0)   Quote dj air Quote  Post ReplyReply Direct Link To This Post 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



Edited by dj air
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
WebDever View Drop Down
Newbie
Newbie


Joined: 28 September 2003
Location: United Kingdom
Status: Offline
Points: 36
Post Options Post Options   Thanks (0) Thanks(0)   Quote WebDever Quote  Post ReplyReply Direct Link To This Post Posted: 27 July 2004 at 6:44pm
Thanks dj air! Appreciate the help, I owe ya one m8
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.