Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding random numbers

Status
Not open for further replies.

medic133

Technical User
Apr 7, 2002
86
US
I have a simple form with three unbound text boxes named firstnum, secondnum, sumnum. I populate the firstnum and secondnum with random numbers according to the following code. The student is then to enter the sum of the numbers in sumnum. The code for the form is as follows:

Private Sub Form_Load()]
Dim x as Integer
Dim y as Integer
x=Int((10-0+1)*Rnd+0)
y=Int((10-0+1)*Rnd+0)
firstnum.value=x
secondnum.value=y
End Sub

Private Sub sumnum_LostFocus()
If sumnum.value=x+y Then
MsgBox ("Good job")
Else
MsgBox ("Bad job")
End If

End Sub

I am able to populate the firstnum and secondnum text boxes without any problem. My problem is that I can't get the if, then statement to evaluate to true to display the message box "Good job"; it always evaluates to false. Any suggestions as to what the problem is?
 
Medic,

Code:
Private Sub Form_Load()]
Dim x as Integer
Dim y as Integer
x=Int((10-0+1)*Rnd+0)
y=Int((10-0+1)*Rnd+0)
firstnum.value=x
secondnum.value=y
End Sub

Private Sub sumnum_LostFocus()
If sumnum.value = (Me.firstnum + Me.secondnum) Then
   MsgBox ("Good job")
Else
   MsgBox ("Bad job")
End If

End Sub

Wayne
 
I still get a message box "Bad job" even when the if, then value is true. Any other suggestions?
 
Hi medic133,

Try using Val(sumnum) (or some other way of making sure you have a numeric value such as Int or CDbl) instead of sumnum.value

Enjoy,
Tony
 
Tony,

Thanks!! It worked like a charm. I had never heard of the Val() function before. I can see where this would be helpful in a few other areas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top