Overflow Message

  • Thread starter Thread starter Frank Wagner
  • Start date Start date
F

Frank Wagner

I am developing a system for teaching multiplication.
When I multiply 94 * 895 in the visual basic code, I get
an overflow message. I am familiar with the need to use
the Long variable with large numbers, but I didn't know
that you can't multiply integers to produce large numbers.

Any help would be appreciated

Thanks

Frank Wagner
 
The Integer data type limits you to -32,768 to 32,767 - while your answer
would be higher. Somewhere, you are defining the result of the calculation
as an Integer. Just make it Long instead. (...if you're still unsure -
post your code...)
 
Rob:

That's what I've always figured, but this one has got me
stumped. Here is the code.

Dim CAnswer As Long

CAnswer = 0

CAnswer = 94 * 895

It's as simple as I can make it and it still produces an
overflow error.

Any help would be appreciated

Frank Wagner
 
Nasty. I can't swear to what the problem is but I think Access is thinking
"oooh...a calculation between two integers...I think I'll make the answer an
integer... oh dear, it doesn't fit... I'll throw an error."

This works...

Function test() As Long
Dim CAnswer As Long
Dim x As Long, y As Long
x = 94
y = 895
CAnswer = x * y
test = CAnswer
End Function

....while this doesn't...

Function test() As Long
Dim CAnswer As Long
CAnswer = 94 * 895
test = CAnswer
End Function

Just a "feature" of Access I think.
 
You could convert the numbers to long integers with the CLng() function:

Dim CAnswer As Long
CAnswer = 0
CAnswer = CLng(94) * CLng(895)

You can create a function with the variables explicitly set as Long:
Function MultiplyNumbers(lngX As Long, lngY As Long)
MultiplyNumbers = lngX * lngY
End Function

Sub TestMulitply()
Dim strMsg As String
strMsg = "The answer is: " & vbCr & Format(MultiplyNumbers(94, 895),
"#,###")
MsgBox strMsg, vbInformation, "Multiplication"
End Sub
 
Thank you both. Evidently I not only need to make the
result variable long, but also make the two multipliers
long.
Thanks Again

Frank Wagner
 
Back
Top