Hello,
I am learning VB and got stuck..I found nothing in books and tutorials.
I want to make simple quiz (100 questions), structure: 1 lbl = question, 3 radio buttons (answers) and 1 button(next question).
I added access database to DataSource, binded data to lbl and radio buttons, it automaticaly added the code:

'TODO: This line of code loads data into the 'VBtestasDataSet.answers' table. You can move, or remove it, as needed.
        Me.AnswersTableAdapter.Fill(Me.VBtestasDataSet.answers)
        'TODO: This line of code loads data into the 'VBtestasDataSet.Questions' table. You can move, or remove it, as needed.
        Me.QuestionsTableAdapter.Fill(Me.VBtestasDataSet.Questions)

The form is loaded ok with the data I need but I can't write the code for the button "next" to load next row from database!
The question is: HOW TO LOAD NEXT ROW FROM ACCESS? I would appreciate if you could help me :) Thank you in advance.

Recommended Answers

All 14 Replies

Hi!

One way as I see:

Use this funtion:

Public Function LoadNewQuestion(ByVal QuestionNumber As Integer) As DataRow
        Dim row As DataRow = VBtestasDataSet.Questions.Rows(QuestionNumber)
        'You will find Rows() method under "Table" of "DataSet"
        Return row
    End Function

and on every "Next" button click call:
LoadNewQuestion(0)
LoadNewQuestion(1)
LoadNewQuestion(2)
.
.
.

This method will return you a row of Type "DataRow" and you can use its "ItemArray" property to make use of it.

Hope it helps!!

Regards,
Shahan

commented: Solved :) +15

Thank you.
As the number of questions is not limited, I found different solution (for me easier):
just adding

Me.AnswersBindingSource.MoveNext()
        Me.QuestionsBindingSource.MoveNext()

It works very well! Thank you once again :)

Now I have different problem..Debug, select checkbox, click "next question" button, load new questions and answers, then click "previous question" button, it loads question and 2 answers correctly but 1 answer which was selected before displays "True" insted of text from DataSet.
Where could be the problem?

Did you update your dataset when click "Next" button ?

No, just load new question and answers from the next row.
Should I update dataset?

Thanks, you were right, it automaticaly changed the value into "True" in the dataset. I made it "Readonly" but now when I click 'previous' button, it forgets which radio button was selected before.

Hi!

You should update your dataset to see the current progress. Also In this link you will see this part of code:

private void button1_Click(object sender, EventArgs e)
		{
			// If you are not at the end of the list, move to the next item
			// in the BindingSource.
			if (BindingSource1.Position + 1 < BindingSource1.Count)
				BindingSource1.MoveNext();

			// Otherwise, move back to the first item.
			else
				BindingSource1.MoveFirst();

			// Force the form to repaint.
			this.Invalidate();
		}

Did you put this condition:

// Force the form to repaint.
			this.Invalidate();

Here is a complete demonstration of BindingSource navigation

maybe I am doing something wrong but Me.Invalidate() doesn't help.

How you display the data OR read from the source to display data ?

Ok, here is part of the code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        'Load data from DataSet
        Me.AnswersTableAdapter.Fill(Me.VBtestasDataSet.answers)
        Me.QuestionsTableAdapter.Fill(Me.VBtestasDataSet.Questions)

        'Loading not checked radio buttons
        _1_optionRadioButton.Checked = False
        _2_optionRadioButton.Checked = False
        _3_optionRadioButton.Checked = False

    End Sub

    ''Next' button click
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click


        'Find out which radio button is selected. I need it for calculating result later
        If _1_optionRadioButton.Checked = True Then
            iOpt = 1
        ElseIf _2_optionRadioButton.Checked = True Then
            iOpt = 2
        ElseIf _3_optionRadioButton.Checked = True Then
            iOpt = 3
        End If

        'Renew the result
        If RightAnswer = iOpt Then
            Result = Result + 1
        Else : Result = Result
        End If

        lblResult.Text = Result


        'Load next question and answers
        Me.QuestionsBindingSource.MoveNext()
        Me.AnswersBindingSource.MoveNext()

        'Loading not checked radio buttons
        _1_optionRadioButton.Checked = False
        _2_optionRadioButton.Checked = False
        _3_optionRadioButton.Checked = False

    End Sub

    ''Prevoius' question button click
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        'Load previous question and answers
        Me.QuestionsBindingSource.MovePrevious()
        Me.AnswersBindingSource.MovePrevious()

    End Sub

Hi!

I mean how you display data on the form ?? In Labels ?
So, I want that piece of code where you read data from source and display on form.

Question - Label;
3 Answers - 3 Radio buttons.

Just dragged titles from "Data Sources" tab to the form design and it automaticaly added label, radio buttons, binded it.

The code automaticaly was added (I did nothing more):

'Load data from DataSet
Me.AnswersTableAdapter.Fill(Me.VBtestasDataSet.answers)
Me.QuestionsTableAdapter.Fill(Me.VBtestasDataSet.Questions)

Thanks, you were right, it automaticaly changed the value into "True" in the dataset. I made it "Readonly" but now when I click 'previous' button, it forgets which radio button was selected before.

In response to your this post:
You can maintain a list:

Dim lst as new List<int>()

This list will keep track of all the option number you have selected.

Lets say you have 5 question, each having four options and user have selected options a,b,c,c,d.

The 'lst' will contain:
lst(0)=1
lst(1)=2
lst(2)=3
lst(3)=3
lst(4)=4

and on every Button click (either 'Next' or 'Prev') you will access the corresponding indices of 'lst'.

Got it ?

Thank you for your answer. Yes, I got the idea but still not very lucky. It shows errors on the lines that was correct before, ignores most of the code, shows buttons which are set not visible and even I can't get loadded next row (next 3 answers).
Sorry for very stupid questions but:
1. How can I bind list with data set?
2. How can I bind list with radio buttons?
3. Is it enough to make list under Private Sub Form1_Load or I should write something more under 'next' button click?

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.