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 
number
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 
randmon
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 
etc.
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 
6,7,8,9,10,11,12
etcetetc.

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, 
but
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, 
adLockReadOnly,
adCmdText

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

' Iterate through records until the random record is reached
for intLoop =3D 0 to intRandomInteger
         objCount.movenext
next
%>

'Print contents of record
<%=3DobjCount.Fields("whatever")%>

<%
' Close DB objects and free variables
objCount.Close
Set objCount =3D Nothing
objDBConnection.Close
Set objDBConnection =3D Nothing
%>
-

Cheers,

Ben
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 ;-)
>
>Reg.
>Tim
>
>-----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 
recordset
>
>
>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
>
>Cheers,
>
>Ben
>
>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
> >
> >
> >
http://benmeg.com

Home 020 8892 8744



  Return to Index