Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2007

Tip: Looking for answers? Try searching our database.

Math Quiz Program with Random Numbers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DrNoose - 06 Mar 2007 21:39 GMT
Hi!

I have a friend who sent me her code to take a look at. I'm pretty new
to learning Access VBA and not quite sure where here problem lies other
than she needs to line up her If statements a little better.

The initial program uses a list box that has the following headings:
Question, Your Answer, and Result. There are 3 command buttons - Start
Quiz, Remove Item and Quit.  When you start the program, you are asked
how many questions you want to answer. You tell it how many, it asks the
questions and tells you whether you are correct or incorrect. These are
all addition questions, btw.

What she has to do now is to use the exiting code and have the program
randomize not only numbers but the type of math problem - more
specifically, use an additional variable to hold a random number between
1 and 4 where each number represents add., sub., mult. & div.

I'm including her code. I tried running it and all of the buttons
worked, but it only gave one type of problem; in my case a division
problem, but all of the questions were the same. The next time I ran it,
it gave me a subtraction questions, but all were the same question.

I'm continuing to work on it with her, but if you see something obvious,
please let me know.

****************************************************************************

Option Compare Database
Option Explicit

Private Sub cmdQuit_Click()
    DoCmd.Quit
End Sub

Private Sub Form_Click()

End Sub

Private Sub Form_Load()
   Randomize
End Sub

Private Sub cmdRemoveItem_Click()

   ' Determine if an item has been selected first
   If lstResults.ListIndex = -1 Then
      MsgBox "Select an item to remove."
   Else
      lstResults.RemoveItem lstResults.ListIndex + 1
   End If

End Sub

Private Sub cmdStart_Click()

   Dim sResponse As String
   Dim sUserAnswer As String
   Dim iCounter As Integer
   Dim iOperand1 As Integer
   Dim iOperand2 As Integer

   ' Determine how many math questions to ask.
   sResponse = InputBox("How many math questions would you like?")

   If sResponse <> "" Then

      ' Add header to each column in the list box if one
      ' hasn't already been added.
      If lstResults.ListCount = 0 Then
         lstResults.AddItem "Question;Your Answer;Result"
      End If

      ' Ask predetermined number of math questions.
      ' For iCounter = 1 To Val(sResponse)

         ' Generate random numbers between 1 and 100.
         iOperand1 = Int(100 * Rnd)
         iOperand2 = Int(100 * Rnd)
         ' Generate case numbers between 1 and 4

    'sResponse = InputBox("If you want to add select 1, subtract select
2, multiply select 3, divide select 4")
        'If sResponse <> "" Then

      ' Add header to each column in the list box if one
      ' hasn't already been added.
      'If lstResults.ListCount = 0 Then
         'lstResults.AddItem "Question;Your Answer;Result"
     ' End If
         Dim iRandomNumber As Integer
         iRandomNumber = Int((4 * Rnd) + 1)
         Select Case iRandomNumber 'added for case numbers
         Case 1
         For iCounter = 1 To Val(sResponse)
         ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " + " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 + iOperand2 Then
            lstResults.AddItem iOperand1 & " + " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " + " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
          End If
    Next iCounter

         Case 2
         For iCounter = 1 To Val(sResponse)
          ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " - " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 - iOperand2 Then
            lstResults.AddItem iOperand1 & " - " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " - " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

         Case 3
          For iCounter = 1 To Val(sResponse)
                  ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " * " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 * iOperand2 Then
            lstResults.AddItem iOperand1 & " * " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " * " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

        Case 4
          For iCounter = 1 To Val(sResponse)
                     ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " / " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 / iOperand2 Then
            lstResults.AddItem iOperand1 & " / " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " / " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

        End Select
        'End If
        End If
        End Sub
DrNoose - 06 Mar 2007 23:25 GMT
Actually, the newest code she sent me is following:

Option Compare Database
Option Explicit

Private Sub cmdQuit_Click()
    DoCmd.Quit
End Sub

Private Sub Form_Click()

End Sub

Private Sub Form_Load()
   Randomize
End Sub

Private Sub cmdRemoveItem_Click()

   ' Determine if an item has been selected first
   If lstResults.ListIndex = -1 Then
      MsgBox "Select an item to remove."
   Else
      lstResults.RemoveItem lstResults.ListIndex + 1
   End If

End Sub

Private Sub cmdStart_Click()

   Dim sResponse As String
   Dim sUserAnswer As String
   Dim iCounter As Integer
   Dim iOperand1 As Integer
   Dim iOperand2 As Integer
   Dim iRandomNumber As Integer

   ' Determine how many math questions to ask.
   sResponse = InputBox("How many math questions would you like?")

   If sResponse <> "" Then

      ' Add header to each column in the list box if one
      ' hasn't already been added.
      If lstResults.ListCount = 0 Then
         lstResults.AddItem "Question;Your Answer;Result"
      End If

      ' Ask predetermined number of math questions.
      ' For iCounter = 1 To Val(sResponse)

         ' Generate random numbers between 1 and 4.
         iOperand1 = Int(4 * Rnd)
         iOperand2 = Int(4 * Rnd)
         ' Generate case numbers between 1 and 4
         iRandomNumber = Int(4 * Rnd)
         Select Case iRandomNumber 'added for case numbers
         Case 1
         For iCounter = 1 To Val(sResponse)
         ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " + " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 + iOperand2 Then
            lstResults.AddItem iOperand1 & " + " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " + " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
          End If
    Next iCounter

         Case 2
         For iCounter = 1 To Val(sResponse)
          ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " - " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 - iOperand2 Then
            lstResults.AddItem iOperand1 & " - " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " - " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

         Case 3
          For iCounter = 1 To Val(sResponse)
                  ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " * " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 * iOperand2 Then
            lstResults.AddItem iOperand1 & " * " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " * " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

        Case 4
          For iCounter = 1 To Val(sResponse)
                     ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " / " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 / iOperand2 Then
            lstResults.AddItem iOperand1 & " / " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " / " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

        End Select
        End If
        End Sub

> Hi!
>
[quoted text clipped - 161 lines]
>         End If
>         End Sub
John Spencer - 07 Mar 2007 13:21 GMT
Look for <<<<<<<<<<<<< in the following for some suggestions.
I can't say why the same set of questions is being generated every time, but
if putting randomize in the on load doesn't work then put it in the
Option Compare Database
Option Explicit

Private Sub cmdQuit_Click()
    DoCmd.Quit
End Sub

Private Sub Form_Load()
   Randomize 2  '<<<<<<<<<<<<<<<<<
End Sub

Private Sub cmdRemoveItem_Click()

   ' Determine if an item has been selected first
   If lstResults.ListIndex = -1 Then
      MsgBox "Select an item to remove."
   Else
      lstResults.RemoveItem lstResults.ListIndex + 1
   End If

End Sub

Private Sub cmdStart_Click()

   Dim sResponse As String
   Dim sUserAnswer As String
   Dim iCounter As Integer
   Dim iOperand1 As Integer
   Dim iOperand2 As Integer
   Dim iRandomNumber As Integer

   ' Determine how many math questions to ask.
   sResponse = InputBox("How many math questions would you like?")

    'Test for number response instead of any response
   If  IsNumeric(Response) = True Then  '<<<<<<<<<<<<
       Randomize 2  '<<<<<<<<<<<<<<<<<<<<<<<
      ' Add header to each column in the list box if one
      ' hasn't already been added.
      If lstResults.ListCount = 0 Then
         lstResults.AddItem "Question;Your Answer;Result"
      End If

      ' Ask predetermined number of math questions.
      ' For iCounter = 1 To Val(sResponse)

         ' Generate random numbers between 1 and 4.
'iOperand1 = Int(4 * Rnd) generates 0 to 3 not 1 to 4
         iOperand1 = Int(4 * Rnd) +1 '<<<<<<<<<<<<<<<<<<
         iOperand2 = Int(4 * Rnd) +1 '<<<<<<<<<<<<<<<<<
         ' Generate case numbers between 1 and 4
         iRandomNumber = Int(4 * Rnd) + 1 '<<<<<<<<<<<<<<<
'If user enters a non-number response or responds with blank
'You shoud be testing with IsNumeric to see if the response is
'numeric before doing any other processing.
         Select Case iRandomNumber 'added for case numbers
         Case 1
         For iCounter = 1 To Val(sResponse)
         ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " + " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 + iOperand2 Then
            lstResults.AddItem iOperand1 & " + " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " + " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
          End If
    Next iCounter

         Case 2
         For iCounter = 1 To Val(sResponse)
          ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " - " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 - iOperand2 Then
            lstResults.AddItem iOperand1 & " - " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " - " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

         Case 3
          For iCounter = 1 To Val(sResponse)
                  ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " * " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 * iOperand2 Then
            lstResults.AddItem iOperand1 & " * " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " * " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

        Case 4
'<<<<<<<<<<<<<<<<
'Problem here is fractional answers such as 1/3  = .3333333...

          For iCounter = 1 To Val(sResponse)
                     ' Generate question.
         sUserAnswer = InputBox("What is " & iOperand1 & _
            " / " & iOperand2)

         ' Determine if user's answer was correct and add an
         ' appropriate item to the multi-column list box.
         If Val(sUserAnswer) = iOperand1 / iOperand2 Then
            lstResults.AddItem iOperand1 & " / " & _
                iOperand2 & ";" & sUserAnswer & ";Correct"
         Else
            lstResults.AddItem iOperand1 & " / " & _
                iOperand2 & ";" & sUserAnswer & ";Incorrect"
         End If
      Next iCounter

        End Select
        End If
        End Sub

Signature

John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> Actually, the newest code she sent me is following:
>
[quoted text clipped - 289 lines]
>>         End If
>>         End Sub
DrNoose - 07 Mar 2007 14:11 GMT
John,

Hi!

Thanks for your help! I'll be talking to my friend today. Programming is
not her fortay and really not mine either, but together, maybe we can
make something work!

I fixed that and changed a couple of things. I'm not sure about what to
do for the division problems that come up as fractions.

Here is the code that I came up with. It seems to work. See what you think.
********************************************************************************************

Option Compare Database
Option Explicit

Private Sub cmdQuit_Click()
    DoCmd.Quit
End Sub

Private Sub Form_Load()
   Randomize
End Sub

Private Sub cmdRemoveItem_Click()

   'Determine if an item has been selected first
   If lstResults.ListIndex = -1 Then
      MsgBox "Select an item to remove."
   Else
      lstResults.RemoveItem lstResults.ListIndex + 1
   End If

End Sub

Private Sub cmdStart_Click()

   Dim sResponse As String
   Dim sUserAnswer As String
   Dim iCounter As Integer
   Dim iOperand1 As Integer
   Dim iOperand2 As Integer
   Dim iRandomNumber As Integer

   'Determine how many math questions to ask.
   sResponse = InputBox("How many math questions would you like?")

   If sResponse <> "" Then

      ' Add header to each column in the list box if one
      ' hasn't already been added.
      If lstResults.ListCount = 0 Then
         lstResults.AddItem "Question;Your Answer;Result"
      End If

         'Ask predetermined number of math questions.
         For iCounter = 1 To Val(sResponse)
         iRandomNumber = Int(4 * Rnd) + 1

         'Generate random numbers between 1 and 4.
         iOperand1 = Int(100 * Rnd)
         iOperand2 = Int(100 * Rnd)

         'Generate case numbers between 1 and 4
         Select Case iRandomNumber 'added for case numbers

            Case 1
            'Generate question.
                sUserAnswer = InputBox("What is " & iOperand1 & _
                 " + " & iOperand2)

            'Determine if user's answer was correct and add an
            'appropriate item to the multi-column list box.
                 If Val(sUserAnswer) = iOperand1 + iOperand2 Then
                  lstResults.AddItem iOperand1 & " + " & _
                    iOperand2 & ";" & sUserAnswer & ";Correct"
                Else
                  lstResults.AddItem iOperand1 & " + " & _
                   iOperand2 & ";" & sUserAnswer & ";Incorrect"
                End If

             Case 2

                'Generate question.
                 sUserAnswer = InputBox("What is " & iOperand1 & _
                    " - " & iOperand2)

                 'Determine if user's answer was correct and add an
                 'appropriate item to the multi-column list box.
                 If Val(sUserAnswer) = iOperand1 - iOperand2 Then
                    lstResults.AddItem iOperand1 & " - " & _
                      iOperand2 & ";" & sUserAnswer & ";Correct"
                 Else
                        lstResults.AddItem iOperand1 & " - " & _
                            iOperand2 & ";" & sUserAnswer & ";Incorrect"
                 End If

            Case 3

                 'Generate question.
                 sUserAnswer = InputBox("What is " & iOperand1 & _
                     " * " & iOperand2)

                 'Determine if user's answer was correct and add an
                 'appropriate item to the multi-column list box.
                 If Val(sUserAnswer) = iOperand1 * iOperand2 Then
                     lstResults.AddItem iOperand1 & " * " & _
                       iOperand2 & ";" & sUserAnswer & ";Correct"
                 Else
                    lstResults.AddItem iOperand1 & " * " & _
                        iOperand2 & ";" & sUserAnswer & ";Incorrect"
                 End If

            Case 4

                 'Generate question.
                 sUserAnswer = InputBox("What is " & iOperand1 & _
                     " / " & iOperand2)

                 'Determine if user's answer was correct and add an
                 'appropriate item to the multi-column list box.
                 If Val(sUserAnswer) = iOperand1 / iOperand2 Then
                     lstResults.AddItem iOperand1 & " / " & _
                      iOperand2 & ";" & sUserAnswer & ";Correct"
                 Else
                    lstResults.AddItem iOperand1 & " / " & _
                        iOperand2 & ";" & sUserAnswer & ";Incorrect"
                 End If

            End Select
         Next iCounter
   End If
 End Sub

> Look for <<<<<<<<<<<<< in the following for some suggestions.
> I can't say why the same set of questions is being generated every time, but
[quoted text clipped - 129 lines]
>          End If
>          End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.