D
DrNoose
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
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