Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Random Select from Database
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Random Select from Database

 Post Reply Post Reply Page  <123>
Author
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: 18 October 2003 at 11:43am
When giving the Random function a seed it will also produce the same number and thus if your random seed is constant (in this case the ID of a record), it will always be sorted the same way. This may be different from the default sort, but the rows will always be in the same order sorted by Rnd(id). Remove the seed and sort by Rnd() and you should get random sorting. I don't use Access so I can't tell you explicitly that it will work.

An alternative would be to count the number of records in the database and use VBScript's Rnd() function to select the random record and then query for the that particular record. This is the method that most Ad program use.



NewID() only works with SQL Server so that would not help you with an Access database.
Back to Top
cosmic cat View Drop Down
Groupie
Groupie


Joined: 14 May 2002
Location: United Kingdom
Status: Offline
Points: 49
Post Options Post Options   Thanks (0) Thanks(0)   Quote cosmic cat Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2003 at 12:27pm

I've not tested the Access one either, sorry should have made that clear; it's just a snippet I picked up which looked like it should work. The SQL one works though, I use it a lot for randomly picking out records..

I'm taking a look in Access (2000) now to see what it does. Hmm, seems to always reverse the order of the records, so the top one would always be the last record.

I'll have a play later see if I can make it work!

Back to Top
zMaestro View Drop Down
Senior Member
Senior Member


Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
Post Options Post Options   Thanks (0) Thanks(0)   Quote zMaestro Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2003 at 12:44pm

it worked, thanks to MorningZ post.

 

<%
Dim Rand, RandS, RandHere
Rand = "SELECT id, Make, Model, Image FROM eCatalog "
set RandS = server.createobject("adodb.recordset")
RandS.open Rand, Conn, 3, 1

Dim rndMax
rndMax = CInt(RandS.RecordCount)
RandS.MoveFirst
Randomize Timer
Dim rndNumber
rndNumber = Int(RND * rndMax)
RandS.Move rndNumber

 

RandHere = RandHere & "<img border=""0"" onError=""src='GfX/clear.gif'""  src=""eShop/" & RandS(1) & ".gif""><BR>"
RandHere = RandHere &  "<img border=""0"" src=""eShop/" & RandS(3) & """>"
response.write RandHere
%>

 

 



Edited by zMaestro
Back to Top
zMaestro View Drop Down
Senior Member
Senior Member


Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
Post Options Post Options   Thanks (0) Thanks(0)   Quote zMaestro Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2003 at 12:53pm
Originally posted by cosmic cat cosmic cat wrote:

I've not tested the Access one either, sorry should have made that clear; it's just a snippet I picked up which looked like it should work. The SQL one works though, I use it a lot for randomly picking out records..

I'm taking a look in Access (2000) now to see what it does. Hmm, seems to always reverse the order of the records, so the top one would always be the last record.

I'll have a play later see if I can make it work!

 

thanks a lot.. it is working fine with the code i posted.

Back to Top
cosmic cat View Drop Down
Groupie
Groupie


Joined: 14 May 2002
Location: United Kingdom
Status: Offline
Points: 49
Post Options Post Options   Thanks (0) Thanks(0)   Quote cosmic cat Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2003 at 6:04pm

Cool

Shame there's not a simpler way in Access like SQL Server though, and when I was digging around in Access earlier, I remembered how much I've forgotten about Access (haven't used it for a couple of years now since we moved to MS SQL Server).

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: 18 October 2003 at 9:44pm

I remember testing this with Access a while back and we couldn't get it to reseed on the same connection, or was it a pool of connections....   There was some type of work around but I can't find where I saved it.

The SS one comes in quite handy though.

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
sam98 View Drop Down
Newbie
Newbie


Joined: 23 October 2003
Location: United Kingdom
Status: Offline
Points: 2
Post Options Post Options   Thanks (0) Thanks(0)   Quote sam98 Quote  Post ReplyReply Direct Link To This Post Posted: 23 October 2003 at 8:50am
how can we distribute rows in a database
Back to Top
sam98 View Drop Down
Newbie
Newbie


Joined: 23 October 2003
Location: United Kingdom
Status: Offline
Points: 2
Post Options Post Options   Thanks (0) Thanks(0)   Quote sam98 Quote  Post ReplyReply Direct Link To This Post Posted: 23 October 2003 at 8:51am
how can we distribute columns in a database
Back to Top
 Post Reply Post Reply Page  <123>

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.