Click here to Skip to main content
15,910,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I am developing a holy bible trivia game.
I have a QUESTIONS table with QUESTIONID(integer),QUESTIONSTMT(string) 2 columns, and some rows in it. I want to select random 10 questions for executing the game.

I've searched some page, and see a solution:
SQL
SELECT TOP n * FROM Table WHERE [conditions] ORDER BY RND([number col])


but when I use it in my code, it throws an exception that I didn't give a value for a parameter.

How could I solve this problem?
Posted

You could use something like this
SQL
SELECT TOP 10 QUESTIONID,QUESTIONSTMT FROM QUESTIONS ORDER BY RND(1217*QUESTIONID)

1217 is just a number, could be anything except zero.
 
Share this answer
 
Try ORDER BY new guid (you'll have to look up how to generate new GUID for Access). It will generate unique guid each time and thus sort your top n query in random fashion.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Please, have a look at your query:
SQL
SELECT TOP n *
FROM Table
WHERE [conditions]
ORDER BY RND([number col])


I see few issues:
1) If n is parameter, you need to define it (see below).
2) Table is reserved word[^]. I'd suggest to change the table name.
3) [conditions] - to be able to use conditions in WHERE statement[^], you should provide at least a column name and comparison operator
SQL
WHERE ColumnName <operator> [condition]

4) RND([number col]) is not known for MS Access, because Rnd[^] does not accept any input parameter.
By The Way: ORDER BY statement[^] with parameters behaves like WHERE statement.

So...
If you want to get 10 random numbers, you need to write a query, as is described here:
How to create a random number in an SQL query on Microsoft Access?[^]
Create a randon unique number field in a query[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900