Type mismatch error - new to VBA help

N

Neal Ostrander

I have the following code and I keep getting a type mismatch error after
inputing the answer in the input box. I believe it may be caused by the way I
am trying to use the sOperator string but not sure. Just learning and haven't
had much experience with this. Thanks in advance for any help.
Neal
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 sOperator As String
Dim iOperator 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 0 and 100.
iOperand1 = Int(100 * Rnd)
iOperand2 = Int((100 * Rnd) + 1)

'Generate random number between 1 and 4
'use numbers in Select case to determine math operator to use +,-,*,/
iOperator = Int((4 * Rnd) + 1)

Select Case iOperator

Case 1
sOperator = "+"
Case 2
sOperator = "-"
Case 3
sOperator = "*"
Case 4
sOperator = "/"

End Select

'Generate question
sUserAnswer = InputBox("What is " & iOperand1 & " " & sOperator & "
" & iOperand2)

'Determine if user's answer was correct and add an
'appropriate item to the multi-column list box.

'****The line below is highlighted when debug selected from error message*****
If Val(sUserAnswer) = iOperand1 & sOperator & iOperand2 Then
lstResults.AddItem iOperand1 & sOperator & iOperand2 & ";" &
sUserAnswer & ";Correct"
Else
lstResults.AddItem iOperand1 & sOperator & iOperand2 & ";" &
sUserAnswer * ";Incorrect"
End If

Next iCounter

End If

End Sub
 
D

Dennis

Try using the Eval function

If Val(sUserAnswer) = Eval(iOperand1 & sOperator & iOperand2) Then
 
N

Neal Ostrander

Thanks Dennis that worked the book I'm using to learn hadn't covered the eval
function I'll look it up to see how it works. Thanks again
Neal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top