8 Replies - 2448 Views - Last Post: 28 November 2011 - 03:21 PM Rate Topic: -----

#1 SturChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Multiple choice quiz with access, want 3 levels of answers

Posted 22 November 2011 - 08:01 AM

I am working on a multiple choice quiz, admin screen, student login screen where they choose their level of difficulty. I have been able to start the quiz, have the questions and answers filled, user answers question, check if correct or not, if correct then the score is incremented.

I am able to set up a variable for 'level' and want to write some code so that whatever is selected then that table / questions will be selected and displayed to the user.

The way I'm thinking at the moment seems very long winded. I think I'll have to write an IF ELSE statement/case statement for each of the 'navigaterecords' and the sql statment for beginners, intermediate and advanced.

Does anyone know a much 'neater' way? PLEASE remember that I am relatively new to programming. Thanks.
here is the code so far for the quiz form.

Assume there is a variable 'LEVEL' - have not actually done this yet. Where the code refers to 'advanced' is where I have been trying it out to select questions from the advanced table.



Imports System.Data
Imports System.Random
Imports System.Data.OleDb
Public Class lblScore
    Dim sql As String
    Dim da As OleDb.OleDbDataAdapter
    Dim con As New OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim inc As Integer
    Dim ds As New DataSet
    Dim MaxRows As Integer
    Dim score As Integer
    Dim dtStart As Date

    Private Sub navigateRecords()

        TextBox1.Text = ds.Tables("Advanced").Rows(inc).Item(0)
        TextBox2.Text = ds.Tables("Advanced").Rows(inc).Item(1)
        TextBox3.Text = ds.Tables("Advanced").Rows(inc).Item(2)
        TextBox4.Text = ds.Tables("Advanced").Rows(inc).Item(3)
        TextBox5.Text = ds.Tables("Advanced").Rows(inc).Item(4)
        TextBox6.Text = ds.Tables("Advanced").Rows(inc).Item(5)
        TextBox7.Text = inc

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        label_1.Text += 1
        If label_1.Text = 60 Then
            label_1.Text = "00"
            Label_2.Text += 1
        ElseIf Label_2.Text = 60 Then
            Label_2.Text = "0"
            Label_3.Text += 1
        End If

    End Sub

    Private Sub BtnNextQ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNextQ.Click

        Timer1.Interval = "1000"
        If BtnNextQ.Text = "Next Question" Then
            Timer1.Enabled = True
        End If
        BtnNextQ.Visible = False
        btnCheck.Visible = True
        'navigateRecords()

        If inc <> MaxRows - 1 Then
            inc = inc + 1
            navigateRecords()
        Else
            MsgBox("No More Rows")
        End If

    End Sub

    Private Sub frmQuiz_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con As New OleDbConnection
        con.ConnectionString = "provider=microsoft.jet.OleDb.4.0;data source=z:/new.mdb;"

        con.Open()

        sql = "Select * From Advanced order by rnd(QuestionNumber)"
        da = New OleDbDataAdapter(sql, con)
        da.Fill(ds, "Advanced")

        con.Close()

        MaxRows = ds.Tables("Advanced").Rows.Count
        inc = -1

    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        Dim Myanswer As String
        txtCorrect.Text = ds.Tables("Advanced").Rows(inc).Item(6)

        If RbtnA.Checked = True Then
            Myanswer = "A"
            '  txtMyAnswer.Text = Myanswer
        ElseIf RbtnB.Checked = True Then
            Myanswer = "B"
            '  txtMyAnswer.Text = Myanswer
        ElseIf RbtnC.Checked = True Then
            Myanswer = "C"
            '  txtMyAnswer.Text = Myanswer
        ElseIf RbtnD.Checked = True Then
            Myanswer = "D"
            '   txtMyAnswer.Text = Myanswer

        End If

        If Myanswer = txtCorrect.Text Then
            score = score + 1
            lblS.Text = score
            MsgBox("Correct Answer")

        Else
            MsgBox("Incorrect Answer")
        End If

        If inc <> MaxRows - 1 Then
            inc = inc + 1
            navigateRecords()
        Else
            MsgBox("No More Rows")
        End If

    End Sub

    Private Sub BtnNextQ_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNextQ.VisibleChanged

    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple choice quiz with access, want 3 levels of answers

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,097
  • Joined: 12-June 08

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 22 November 2011 - 08:32 AM

The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for


I would suggest your database table should have a column for 'level'.. then use a 'where' clause to pull back the right set of questions.

I would also suggest the client app take care of the randomizing of the questions and not the database.
Was This Post Helpful? 0
  • +
  • -

#3 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 22 November 2011 - 08:38 AM

Would it be viable in this situation to add a "Level" column to your database table, and select your questions off of that? So if you had your level selection in a combobox, you could use the selected value in your sql statement. Selecting a level would fire off a query like SELECT * FROM wherever WHERE level = '" & cmbServices.SelectedItem.ToString & "'"
Was This Post Helpful? 1
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 22 November 2011 - 08:40 AM

You can set a choice someone selects either beginner, intermediate or advanced, and using only one query, you check what has been selected and know what table to deal with. This will give you least line of codes instead of having query for every level.

Note: Dont treat text boxes texts as numbers, they are string so you have to parse them to appropriate numeric data before doing numeric manipulations...
label_1.Text += 1 ' text is a string!!!
If label_1.Text = 60 Then
This is not a good vb programming practice
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • Das kann nicht sein
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,643
  • Joined: 24-July 11

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 22 November 2011 - 02:50 PM

View Postsmohd, on 23 November 2011 - 01:40 AM, said:

You can set a choice someone selects either beginner, intermediate or advanced, and using only one query, you check what has been selected and know what table to deal with. This will give you least line of codes instead of having query for every level.

Note: Dont treat text boxes texts as numbers, they are string so you have to parse them to appropriate numeric data before doing numeric manipulations...
label_1.Text += 1 ' text is a string!!!
If label_1.Text = 60 Then
This is not a good vb programming practice

Well yes, but I've found you can do Label1.Text = Label1.Text + 1 and it converts it to double and then changes the value. Also, it cannot be this: If label_1.Text = 60
It would have to be this: If Label_1.Text = "60"
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 22 November 2011 - 03:12 PM

View PostjimmyBo, on 23 November 2011 - 03:35 AM, said:

Well yes, but I've found you can do Label1.Text = Label1.Text + 1 and it converts it to double and then changes the value. Also, it cannot be this: If label_1.Text = 60
It would have to be this: If Label_1.Text = "60"

Everything is fine when option Strict Off, but it is not a good programming practices. And our aim is to teach people good programming practices. That is why I have pointed it out and show him the code where he did it.
Always remember, it is a string not a number
Was This Post Helpful? 0
  • +
  • -

#7 SturChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 23 November 2011 - 07:20 AM

View PostTryparse, on 22 November 2011 - 08:38 AM, said:

Would it be viable in this situation to add a "Level" column to your database table, and select your questions off of that? So if you had your level selection in a combobox, you could use the selected value in your sql statement. Selecting a level would fire off a query like SELECT * FROM wherever WHERE level = '" & cmbServices.SelectedItem.ToString & "'"


Thanks very much - I'm going to try this out. It makes a lot of sense as I will just have to adapt my SQL statement to add the 'where' in. Will let you know if it works.
Was This Post Helpful? 0
  • +
  • -

#8 SturChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 28 November 2011 - 03:05 PM

View PostTryparse, on 22 November 2011 - 08:38 AM, said:

Would it be viable in this situation to add a "Level" column to your database table, and select your questions off of that? So if you had your level selection in a combobox, you could use the selected value in your sql statement. Selecting a level would fire off a query like SELECT * FROM wherever WHERE level = '" & cmbServices.SelectedItem.ToString & "'"


private Sub frmQuiz_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con As New OleDbConnection
        con.ConnectionString = "provider=microsoft.jet.OleDb.4.0;data source=z:/quizdatabase.mdb;"

        con.Open()
        If strdifficulty = "beginner" Then
            sql = "Select * From Questions order by rnd(QuestionNumber)& WHERE level =B "

            da = New OleDbDataAdapter(sql, con)
            da.Fill(ds, "questions")

            con.Close()

            MaxRows = ds.Tables("Questions").Rows.Count
            inc = -1
        End If


    End Sub

Private Sub btnLoginStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoginStudent.Click
        FrmQuiz.Show()
        Me.Hide()
        If RadioButton1.Checked = True Then
            strdifficulty = "Beginner"
        ElseIf RadioButton2.Checked = True Then
            strdifficulty = "Intermediate"
        ElseIf RadioButton3.Checked = True Then
            strdifficulty = "Advanced"
        End If
        MsgBox(strdifficulty & " Difficulty Selected")

    End Sub



I tryed adapting the sequal but i am still having difficulties getting the code to run. its not a combo box im using but radion buttons to store the users input into the variable strdifficulty but every time i go to run the program it stops when trying to fill the quiz from the database. Any suggestions?

Variable strdifficulty

This post has been edited by smohd: 28 November 2011 - 03:10 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#9 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Multiple choice quiz with access, want 3 levels of answers

Posted 28 November 2011 - 03:21 PM

sql = "Select * From Questions order by rnd(QuestionNumber)& WHERE level =B "


What is this? First, strings have to be surrounded by single-quotes in SQL. Also, you're just querying for a static value, so your difficulty selection isn't doing anything. Try something like this
sql = "Select * From Questions order by rnd(QuestionNumber)& WHERE level = '" & strdifficulty & "'"

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1