Random selection

G

Guest

I would like to create a database of questions from which I can randomly
select any given number.

The idea is to use each line for a question. Over time this will grow into
a database of questions. Then when compiling a quiz I would like to randomly
select any given number of questions from the database.

Could anyone please advise me how to do the ramdom selection macro?

Thank you
 
M

Marshall Barton

Neil Sheath said:
I would like to create a database of questions from which I can randomly
select any given number.

The idea is to use each line for a question. Over time this will grow into
a database of questions. Then when compiling a quiz I would like to randomly
select any given number of questions from the database.

Could anyone please advise me how to do the ramdom selection macro?


You use a query to retrieve data from a table. A macro or
VBA procedure would be used to control how the data is
presented. Here's an SQL statement to do what you asked
for:

SELECT TOP <N> questionfield
FROM questionstable
ORDER BY Rnd(questionfield)

You don't say what you want to do with the data that the
query returns. Perhaps print a report?? Regardless, be
sure to execute a Randomize statement before the query runs.
 
A

Allen Browne

Use a form.
Assign its RecordSource to the number of questions you want.
Use the ORDER BY clause to choose the records.

This is the kind of event procedure (code, not macro) that you will need.
Replace 99 with the number of questions you want.
The Rnd() causes them to be randomly selected.

Private Sub Form_Open(Cancel As Integer)
Dim strSql As String

Randomize
strSql = "SELECT TOP 99 tblQuestion.* FROM tblQuestion " & _
"ORDER BY Rnd(tblQuestion.QuestionID);"
Me.RecordSource = strSql
End Sub
 
G

Guest

Thank you for the replies. I do not quite understand them but I know someone
who does!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top