I try to order a resultset with a query in Access randomly.
I use the following code:
DIM TentamenQuestionID, Tentame开发者_运维问答nQuestionResult, TentamenQuestionQuery, arrTentamenQuestion, arrTentamenQuestionIndex
Set TentamenQuestionResult = Server.CreateObject("ADODB.Recordset")
TentamenQuestionQuery = "SELECT TentamenQuestion.TentamenQuestionID, TentamenQuestion.TentamenQuestion, TentamenQuestion.TentamenQuestionSort " &_
"FROM TentamenQuestion " &_
"WHERE TentamenQuestion.TentamenID=" & TentamenID & " " &_
"ORDER BY Rnd(TentamenQuestion.TentamenQuestionID)"
TentamenQuestionResult.Open TentamenQuestionQuery, Connect, adOpenStatic, adLockReadOnly, adCmdText
If NOT TentamenQuestionResult.EOF then
arrTentamenQuestion = TentamenQuestionResult.GetRows()
End If
TentamenQuestionResult.Close
Set TentamenQuestionResult = Nothing
response.write(arrTentamenQuestion(0,0)) & "<br />"
response.write(arrTentamenQuestion(1,0)) & "<br />"
response.write(arrTentamenQuestion(2,0)) & "<br />"
response.write(arrTentamenQuestion(0,1)) & "<br />"
response.write(arrTentamenQuestion(1,1)) & "<br />"
response.write(arrTentamenQuestion(2,1)) & "<br />"
When I run the query in Access, the records are selected randomly but when I use this code and response write the array. The array is sorted the same every time and not randomly. How can I get the result array randomly?
You might try using the Randomize command somewhere in your ASP code prior to each call to the Rnd() function.
Otherwise, I'd be interested to know if the sorting is occurring during the select, or when the records are retrieved into the variant array. Have you tried iterating the records using the standard ADO syntax (MoveNext) to isolate the problem?
精彩评论