Home All Groups Group Topic Archive Search About

Overflow strange behavior-Urgent

Author
6 Jul 2009 3:43 PM
avi
Hello,

It looks as suddenly, large numbers (not huge ones!) returned by
function calls in vb6/vba create an overflow msg

For exemple:

Private Sub yyy()
    Call test(45)
End Sub
Function test(g)
    MsgBox 12336 * 4569
End Function

What am I doing wrong?

Please help
Avi

Author
6 Jul 2009 3:57 PM
Bob Butler
Show quote Hide quote
"avi" <avi***@bezeqint.net.il> wrote in message
news:760b075c-e727-4839-9ec0-c68f34e90768@g19g2000yql.googlegroups.com...
> Hello,
>
> It looks as suddenly, large numbers (not huge ones!) returned by
> function calls in vb6/vba create an overflow msg
>
> For exemple:
>
> Private Sub yyy()
>    Call test(45)
> End Sub
> Function test(g)
>    MsgBox 12336 * 4569
> End Function
>
> What am I doing wrong?

Not declaring your variables and functions as explicit types for one.  The
main problem though is that you are multiplying 2 Integer values together
and getting a result that is too large for an Integer data type.

Private sub yyy()
call test(45)
end sub

Private function test(byval G as long) as long
Msgbox 12336& * 4569
End function

Since one of the values is now a Long (the trailing & indicator) the math
will be performed using Long values and the overflow disappears.  You can
"promote" one or the other or both values or use the CLng function instead
of the type suffix but the key is that at least one operand must be a type
large enough for the result.
Author
6 Jul 2009 4:08 PM
Jan Hyde
avi <avi***@bezeqint.net.il>'s wild thoughts were released
on Mon, 6 Jul 2009 08:43:35 -0700 (PDT) bearing the
following fruit:

Show quoteHide quote
>Hello,
>
>It looks as suddenly, large numbers (not huge ones!) returned by
>function calls in vb6/vba create an overflow msg
>
>For exemple:
>
>Private Sub yyy()
>    Call test(45)
>End Sub
>Function test(g)
>    MsgBox 12336 * 4569
>End Function
>
>What am I doing wrong?

Using integers, both number can be stored as integers and
are, and the result is an integer. Hence the overflow

Try

MsgBox 12336& * 4569&

This & declares the numbers to be long values and hence the
return value will also be long.


--
Jan Hyde