Hi; I am new to VB.net and am developing a memory quiz. The quiz contains more than 100 questions imported from access 2002 db. For the learning process, I need to run the game again and again. At the moment, I can't randomize the question order, I am getting the questions in same sequence as they are taken from db. Could nebody give me some idea, how can I pint to different tb records. My application has a timer for maintaining question timings. I would like to know where in the function I could utilize the randomize function. Code is
Public Function rungame() Try If t <= totq - 1 Then ' totq is total no of questions ' t is row of table Timer1.Enabled = True If con.State = ConnectionState.Open Then con.Close() con.Open() Dim adp As New OleDbDataAdapter("select * from tb2", con) Dim ds As New DataSet adp.Fill(ds) Dim dt As DataTable dt = ds.Tables(0) qbox.Text = dt.Rows(t).Item(1) PictureBox1.ImageLocation = dt.Rows(t).Item(2) PictureBox2.ImageLocation = dt.Rows(t).Item(3) PictureBox3.ImageLocation = dt.Rows(t).Item(4) PictureBox4.ImageLocation = dt.Rows(t).Item(5) t = t + 1 con.Close() Else Timer1.Enabled = False qbox.Text = "Match the given TEXT with the correct IMAGE option" optiona.Text = "Option A" optionb.Text = "Option B" optionc.Text = "Option C" optiond.Text = "Option D" Button1.Enabled = True MsgBox("Thanks for Participation : " & namebox.Text & "Your score is : " & scorebox.Text, MsgBoxStyle.Information, "Completed") End If Catch ex As Exception MsgBox(ex.Message) End Try End FunctionThe code for timer is
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Static i As Integer i = i + 1 If optiona.Checked = True Or optionb.Checked = True Or optionc.Checked = True Or optiond.Checked = True Then validans() GoTo ter End If timebox.Text = 30 - i If i >= 30 Then ter: Timer1.Enabled = False If t <= totq Then Label1.Text = "" optiona.Text = "" optionb.Text = "" optionc.Text = "" optiond.Text = "" optiona.Checked = False optionb.Checked = False optionc.Checked = False optiond.Checked = False i = 0 rungame() Else Exit Sub End If End If End SubBelow is the database table with data
Question No Question Image_Op1 Image_Opt2 Image_Op3 Image_Op4 Ans_Image 1 What the hell was that d:\Lost - 1x01_2118452.bmp d:\Lost - 1x01_2130297.bmp d:\Lost - 1x01_2287746.bmp d:\Lost - 1x01_2129046.bmp d:\Lost - 1x01_2118452.bmp 2 I'm having contraction d:\Lost - 1x01_291959.bmp d:\Lost - 1x01_289957.bmp d:\Lost - 1x01_296463.bmp d:\Lost - 1x01_319736.bmp d:\Lost - 1x01_289957.bmp
I am going to suggest that you modify your query you are executing to the database.
Lets assume you can uniquely identify each record in your database. Each record should have a UniqueIdentifier column that is auto incrementing.
You can use the following code to query this database:
Dim iRandom as New Random().Next(1, 100)
Dim adp As New OleDbDataAdapter("select * from tb2 where KeyColumnName = " & iRandom, con)
This will give you a single record at a time. The record will be a random number from 1 to 100.
Another way to accomplish this is to define a new OleDbDataAdapter to obtain the Minimum and Maximum values of the KeyColumnName you have in your database.
Dim adpMax as OleDBDataAdapter ("select Max(KeyColumnName) from tb2")
Dim adpMin as OleDBDataAdapter ("select Min(KeyColumnName) from tb2")
(Note: KeyColumnName is the name of your Primary Key AutoNumber field)
Hope this helps.