p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

proasp_codeclinic thread: Pulling Random data out of a recordset

Message #1 by "Tim Thurlings" <tim.thurlings@m...> on Thu, 24 May 2001 15:14:00 +0200
How are your primary keys set up?  If their sequential, get a randmon 
between the low and high key then query on that key.  If that isn't
possible, do a Select * and then dump your recordset into an array with
.GetRows.  Then you can get the Ubound of the array and create a 
number between 0 and the Ubound for a random record. 

-----Original Message-----
From: Tim Thurlings [mailto:tim.thurlings@m...]
Sent: Thursday, May 24, 2001 9:55 AM
To: Code Clinic
Subject: [proasp_codeclinic] Re: Pulling Random data out of a recordset

ok, i have this working now, but what it does it pulling a random
number of recordsets. Like 6 the first time, then 9 the second time 
I just want to have 5 random recordsets.

The second problem i face with this coding is that they aren't random,
but just increasing.... like 6,7,8,9. The the second time 

Have you got a solution for this?

-----Original Message-----
From: Ben Meghreblian [mailto:benmeg@b...]
Sent: Thursday, May 24, 2001 04:20 PM
To: Code Clinic
Subject: [proasp_codeclinic] Re: Pulling Random data out of a recordset

Hi Tim,

This is off the top of my head, so apologies if it's no quite right, 
hopefully you'll get the gist:-

Dim intLowerBound, intUpperBound, intRandomInteger, intLoop

' Create DB connection, retrieve all records
strSQLQuery =3D "SELECT * FROM tbl_stuff;"
Set objDBConnection =3D Server.CreateObject("ADODB.Connection")
objDBConnection.Open "stuff", "", ""
Set objCount =3D Server.CreateObject("ADODB.Recordset")
objCount.Open strSQLQuery, objDBConnection, adOpenStatic, 

' Get the .RecordCount and use it as an upper bound to generate a 
intLowerBound =3D 1
intUpperBound =3D objCount.RecordCount
intRandomInteger =3D Int((intUpperBound - intLowerBound + 1) * Rnd +

' Iterate through records until the random record is reached
for intLoop =3D 0 to intRandomInteger

'Print contents of record

' Close DB objects and free variables
Set objCount =3D Nothing
Set objDBConnection =3D Nothing


At 16:01 24/05/01 +0200, you wrote:
>Hi Ben,
>thanx, but do you have a little sample coding for me,
>i am not that good in this ;-)
>-----Original Message-----
>From: Ben Meghreblian [mailto:benmeg@b...]
>Sent: Thursday, May 24, 2001 03:40 PM
>To: Code Clinic
>Subject: [proasp_codeclinic] Re: Pulling Random data out of a 
>Hi Tim,
>Why not do something like this:-
>Open Database
>Get the .RecordCount
>.MoveNext a random number of times, bounded by the .RecordCount.
>Extract data
>At 15:14 24/05/01 +0200, you wrote:
> >Dear All,
> >
> >I need to get data out of my recordset, but on random basis.
> >Does anyone knows how to do this?
> >
> >With high hopes,
> >
> >Tim Thurlings
> >
> >
> >

Home 020 8892 8744

  Return to Index