mbennie
|
Posted: 02/04/2002, 7:14 AM |
|
I have a grid form that needs to display the records in random order. Any sample code?
|
|
|
Brent
|
Posted: 02/04/2002, 7:26 AM |
|
It might help if you tell us the language you're using (PHP, ASP, JSP) and the
database (MySQL, Access, Oracle). :)
|
|
|
mbennie
|
Posted: 02/04/2002, 7:33 AM |
|
Oops....sorry.
Using access DB and asp.
|
|
|
Brent
|
Posted: 02/04/2002, 7:40 AM |
|
I don't know if this is applicable to Access or not, but in MySQL you can use
select * from table order by rand() limit 10;
to select 10 records at random from the table. See if Access has a random number
function that can be used in the Order By statement. Now this works fine if
you don't have a huge table (>1000 rows) because it will add Rand() to each
row then it sorts it. So it would be slow for million row tables.
|
|
|
mbennie
|
Posted: 02/04/2002, 8:11 AM |
|
Thank you for the help...I'm almost there...
I found the Rnd() function in Access and built it into the SQL to sort randomly and it works fine in Access. Now the problem is that the page shows the records in random order but continues to show the records in the SAME random order each time. I need to show the records in a different random order each time the page is displayed.
|
|
|
Ken Hardwick
|
Posted: 02/04/2002, 9:31 AM |
|
mBennie,
One way you might consider is to add a column to your database. Then in the "open" event of your grid, update this new column with a "new" set of
random numbers and then retrieved the records in your grid using this new column as the sort column.
Not being one starting from scratch, I found some info at : http://visualbasic.about.com/gi/dynamic/offsite.htm?sit...sp_rndrec_1.asp
Then created the following code to generate random numbers.
kenSQL = "Select * from tbNumbers "
openrs rs3, kenSQL
cnt = 100000
while not rs3.EOF
Randomize Timer
intRnd = (Int(RND * Cnt))
kenWhere = " FLDNUMBERS = " & rs3("FLDNUMBERS")
kenSQL2 = "update tbNumbers set " & _
"[random1]=" & ToSQL(intRnd , "Number")
kenSQL2 = kenSQL2 & " where " & kenWhere
cn.execute (kenSQL2)
rs3.MoveNext
wend
Hope this helps. Let us know how this works out.
Ken Hardwick
Norman,Ok
|
|
|
Steven Sutton
|
Posted: 02/04/2002, 2:12 PM |
|
:I found the Rnd() function in Access and built it into the
:SQL to sort randomly and it works fine in Access. Now the
:problem is that the page shows the records in random order
:but continues to show the records in the SAME random order
:each time. I need to show the records in a different random
:order each time the page is displayed.
Multiply your Rnd() function by the current time in milliseconds if that is possible in Access. This should give you a new set of random numbers every time.
|
|
|
Jerry
|
Posted: 06/16/2003, 2:22 PM |
|
I am unable to get this to work, but I dont know how to do the current time in milliseconds.
I did try Rnd()*Now() but I still get the same results every time. I thought since Now() also shows seconds I might get lucky, but no luck.
Thanks for the help
|
|
|