Results 1 to 6 of 6

Thread: Calculating the score at the end of a test and then saving it into Access.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    9

    Calculating the score at the end of a test and then saving it into Access.

    Hi, I've got a problem with my spelling program where I'm not able to display the correct score at the end of the test. Also, after somebody's help with that, I'd greatly appreciate help from someone to show me how to then save that score into access. Thanks.

    Btw, the 'Test' form is the form which actually displays the test for the student. It includes these labels: 'Week number', 'lblWeekNo2', (This lable will actually change to a week number once the test starts, some bits of the test show up once the user actually clicks the start button and you can see that in the code below.) 'Definition', 'Answer', 'lblInfo' (This label changes as the the test progresses, it shows when the test starts and then simply displays the number of the questions, however, once the test is finished it changes to ''You have successfully completed the test! Well done!'') 'lblDef', (This label is linked with the database to actually show the definition to the user.) a text box which allows the student to actually type in the answer as well as two buttons. 'Start' (Starts the test and shows some bits and hides others[shown in the code below]) and 'Next' which basically just shows the next definition to the user.
    I've tried to display that score for the user but no matter what I do, it always shows the answers that the user has typed in and no score. (Even though I don't actually want these answers displaying there, just the score...)

    This is the code that I've so far:

    Code:
    Public Class frmTest
       Dim Conn As New System.Data.OleDb.OleDbConnection()
       Dim Score As Integer = 0
       Dim Ansrs(1, 9) As String
       Dim r As Integer = 1
       Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
           ' When a user will press the exit button, a message box will appear asking whether they want to close the program and if a user clicks 'Yes' then 
           ' the whole program will close but if they click 'No' then the message box will simply dissapear 
           Dim Result As DialogResult
           Result = MessageBox.Show("Are you sure you wish to close the program?", "Close program?", MessageBoxButtons.YesNo)
           If Result = Windows.Forms.DialogResult.Yes Then
               ' Makes sure that the program will actually fully close
               Application.Exit()
           Else
           End If
       End Sub
    
    
       Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
           ' Instead of just closing the form, it closes the form so that when you open it again, everything resets
           Me.Dispose()
       End Sub
    
    
       Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           ' Makes all the things invisible before the actual test starts
           lblDef.Visible = False
           lblWeekNo2.Visible = False
           lblInfo.Visible = False
           lblDefinition.Visible = False
           txtAns.Visible = False
           lblWeekNo.Visible = False
           btnNxt.Visible = False
           lblAnswer.Visible = False
           ' Opening the connection
           Conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Database for VB.accdb")
           Conn.Open()
       End Sub
    
    
       Private Sub btnNxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNxt.Click
           If r = 10 Then
               Ansrs(0, r - 1) = txtAns.Text
               txtAns.Text = ""
               FinishTest()
           Else
               Ansrs(1, r - 1) = txtAns.Text
               r = r + 1
               txtAns.Text = ""
               StartTest()
           End If
       End Sub
       Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
           ' Makes all the buttons that you will need in the test, visible. It'll show the field for the user to complete
           txtAns.Visible = True
           lblWeekNo.Visible = True
           lblWeekNo2.Visible = True
           lblDef.Visible = True
           lblDefinition.Visible = True
           btnStart.Visible = False
           lblAnswer.Visible = True
           ' When the button 'Start the test!' is clicked, the 'StartTest' sub routine will start
           StartTest()
       End Sub
       Private Sub StartTest()
           ' Making the 'Next' button visible to allow the user to complete the whole test
           btnNxt.Visible = True
           ' Reading data from the database
           Dim StringStart As String = "SELECT * FROM TestData WHERE WordNumber = " & r
           Dim Comm As New OleDb.OleDbCommand(StringStart, Conn)
           Dim RDR As OleDb.OleDbDataReader = Comm.ExecuteReader
           RDR.Read()
           lblDef.Text = RDR.Item("Definition")
           Ansrs(0, r - 1) = RDR.Item("Answer")
           lblWeekNo2.Text = RDR.Item("WeekNumber")
           lblInfo.Visible = True
           lblInfo.Text = r & "/10"
       End Sub
       Private Sub FinishTest()
           lblInfo.Text = "You have successfully completed the test! Well done!"
           lblInfo.Left = (Me.Width / 1.5) - (lblInfo.Width / 1.5)
           ' When the test is done, it makes all of the things that the user won't need anymore, invisible
           lblDef.Visible = False
           lblWeekNo2.Visible = False
           btnNxt.Visible = False
           lblWeekNo.Visible = False
           txtAns.Visible = False
           lblAnswer.Visible = False
           lblDefinition.Visible = False
           TestScore()
       End Sub
       Private Sub TestScore()
           For r As Integer = 0 To 9
               MsgBox(Ansrs(0, r))
               If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then
                   Score = Score + 2
                   MsgBox("You have scored " & Score)
               End If
           Next
       End Sub
    End Class
    P.S. 'Exit' as well as 'Previous' button don't have anything to do with this. Just thought that I might include the whole form instead of some bits though.

  2. #2
    Member
    Join Date
    Nov 2013
    Posts
    50

    Re: Calculating the score at the end of a test and then saving it into Access.

    The issues you are asking about are in the following Sub:

    Code:
       Private Sub TestScore()
           For r As Integer = 0 To 9
               MsgBox(Ansrs(0, r))
               If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then
                   Score = Score + 2
                   MsgBox("You have scored " & Score)
               End If
           Next
       End Sub
    SInce you have a MsgBox(Ansrs(0,r)) inside your For loop, you are seeing all of the answers the user provided.
    In addition, you message box to display the score is within the "If Ansrs" If statement. I would suggest moving it below your Next so that all the Next is doing is computing the Score.

    Hope that give you some ideas.

    Paul
    Please mark your threads Resolved if your thread has been answered..And don't forget to rate the post if the answer has helped you.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    9

    Re: Calculating the score at the end of a test and then saving it into Access.

    I thought that this is probably the problem but even when I change my code to look like this:
    Private Sub TestScore()
    For r As Integer = 0 To 9
    If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then
    Score = Score + 2
    End If
    Next
    MsgBox("You have scored " & Score)
    End Sub
    End Class

    It makes it even worse as the message box with the scores/answers doesn't even show up because the program crashes after I click that 'Next' button for the 11th time (To actually finish the test and supposedly see the score.)
    Crashes on this line:
    If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then

    This is what the description of that error says- ''Object reference not set to an instance of an object.''
    Thanks for the help.

  4. #4
    Member
    Join Date
    Nov 2013
    Posts
    50

    Re: Calculating the score at the end of a test and then saving it into Access.

    I think you are getting the error "Object reference not set to an instance of an object." because the Ansrs(1,r) item isn't being set properly.

    Look in the code here:

    Code:
       Private Sub btnNxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNxt.Click
           If r = 10 Then
               Ansrs(0, r - 1) = txtAns.Text
               txtAns.Text = ""
               FinishTest()
           Else
               Ansrs(1, r - 1) = txtAns.Text
               r = r + 1
               txtAns.Text = ""
               StartTest()
           End If
       End Sub
    In the Else section, you are setting Ansrs(1, r -1), and then you are incrementing r by 1, and then calling StartTest(). In StartTest() it does the following assignment: Ansrs(0, r - 1)

    So these two assignments are using different values for r. From what I can see that you are trying to do, r needs to be the same for both of these assignments.

    It may help to change the Else code to:

    Code:
           Else
               Ansrs(1, r - 1) = txtAns.Text
               txtAns.Text = ""
               StartTest()
               r = r + 1
    Paul
    Please mark your threads Resolved if your thread has been answered..And don't forget to rate the post if the answer has helped you.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    9

    Re: Calculating the score at the end of a test and then saving it into Access.

    No, this doesn't work either. Now what happens is when I type the answer and click next, the field clears and I have to click next again which basically means that the answer wasn't even entered in the first place. (I hope you know what I mean here.) Normally what happened was, I typed the answer in, clicked next and it went to the next question straight away. (Hope that helps. :P)
    Also, at the end, the program still crashes on the same line as before...

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    9

    Re: Calculating the score at the end of a test and then saving it into Access.

    @Ref

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width