简体   繁体   English

如何从Access数据库中随机选择多项选择题

[英]How to randomly select multiple choice questions from Access database

I have an Access Database containing about 30 questions. 我有一个包含大约30个问题的Access数据库。 The database is divided in 3 tables; 该数据库分为3个表。 Questions, Possible Answers and Answer. 问题,可能的答案和答案。 The questions have from 2 to 5 possible answers. 问题有2到5个可能的答案。 How can I randomly select 10 questions from my database and add them to my vb form? 如何从数据库中随机选择10个问题并将其添加到vb表单中?

PS: This is my first time doing this PS:这是我第一次这样做

Here is my code 这是我的代码

Dim provider As String Dim dataFile As String Dim connString As String Public myConnection As OleDbConnection = New OleDbConnection Public dr As OleDbDataReader

 Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" dataFile = "C:\\Users\\Phil\\Desktop\\Questions.accdb" connString = provider & dataFile myConnection.ConnectionString = connString myConnection.Open() Dim str As String str = "SELECT Top 10 ID_Question From Questions ORDER BY RND(ID_Question)" Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection) dr = cmd.ExecuteReader While dr.Read() TextBox1.Text = dr("ID_Question").ToString End While myConnection.Close() MsgBox("fsafa") End Sub 

The Textbox does not change and the msgBox does not show 文本框不会更改,msgBox也不会显示


Solution that worked for me if anyone is interested 对任何人有用的解决方案

SELECT Top 10 ID_Question, Question_Name 
FROM tblQuestions 
ORDER BY RND(-(100000*ID_Question)*Time())

I have to assume that your questions have an AutoNumber field, your possible answers has a one-to-many join based on that AutoNumber field and your answers have a one-to-one join based on that AutoNumber field? 我必须假设您的问题有一个“自动编号”字段,您可能的答案基于该“自动编号”字段具有一对多联接,而您的答案具有基于该“自动编号”字段的一对一联接? That would be the best way to associate the tables. 这将是关联表的最佳方法。

If so, try something like this: 如果是这样,请尝试以下操作:

SELECT Top 10 Question_ID FROM tblQuestions ORDER BY RND(Question_ID)

This should give you the top 10 randomly selected Question_IDs (or whatever you're calling that AutoNumber field I spoke about above), and then you can left join to the Questions/Possible Answers/Answers tables based on that ID. 这应该为您提供前10个随机选择的Question_ID(或您在我上面所说的“自动编号”字段中所说的任何名称),然后您可以根据该ID加入“问题/可能的答案/答案”表。 You would simply populate a form or subform based on the SQL above in order to display the questions. 您只需根据上面的SQL填充表单或子表单即可显示问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM