Home All Groups Group Topic Archive Search About
Author
10 Oct 2005 11:31 PM
Arpan
One of the CommandButtons in a VB6 application is used to get the
square of a number. Now what I find is if this button is clicked
repeatedly, after sometime, VB generates the "Overflow" error. How do I
avoid this error? This is what I have done:

Private Sub cmdSqr_Click()
    Dim iSquare As Double
    iSquare = txtNum1.Text * txtNum1.Text
    .............
    .............
    .............

End Sub

Thanks,

Arpan

Author
10 Oct 2005 11:50 PM
Michael C
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128987105.406938.32240@o13g2000cwo.googlegroups.com...
> One of the CommandButtons in a VB6 application is used to get the
> square of a number. Now what I find is if this button is clicked
> repeatedly, after sometime, VB generates the "Overflow" error. How do I
> avoid this error? This is what I have done:
>
> Private Sub cmdSqr_Click()
>    Dim iSquare As Double
>    iSquare = txtNum1.Text * txtNum1.Text
>    .............
>    .............
>    .............

Trap the error using On Error Goto


Show quoteHide quote
>
> End Sub
>
> Thanks,
>
> Arpan
>
Author
10 Oct 2005 11:57 PM
Someone
Add error trapping like the following. Use Err.Clear when you expect an
error in the next line, and disable the error handler or direct it to other
than "Resume Next", otherwise VB will not popup an error message to see what
other problems you have in subsequent code.

Private Sub cmdSqr_Click()
On Error Resume Next
    Dim iSquare As Double

    Err.Clear ' Make sure that we don't have a previous error
    iSquare = txtNum1.Text * txtNum1.Text
    If Err.Number = 6 Then
        ' Overflow error
        Err.Clear
        txtNum1.Text = "Overflow"
    Else
        txtNum1.Text = iSquare
    End If

    On Error GoTo 0 ' Disable error handling so we can see any errors in the
rest of the code
    '
    '  Additional code
    '

End Sub


Show quoteHide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128987105.406938.32240@o13g2000cwo.googlegroups.com...
> One of the CommandButtons in a VB6 application is used to get the
> square of a number. Now what I find is if this button is clicked
> repeatedly, after sometime, VB generates the "Overflow" error. How do I
> avoid this error? This is what I have done:
>
> Private Sub cmdSqr_Click()
>    Dim iSquare As Double
>    iSquare = txtNum1.Text * txtNum1.Text
>    .............
>    .............
>    .............
>
> End Sub
>
> Thanks,
>
> Arpan
>
Author
11 Oct 2005 1:15 AM
Arpan
Thanks, Michael & Someone, for your suggestions.

I have another query pertaining to this - my VB application lets users
do a lot of calculations. Often the end result may turn out to be a
very big number. Under such circumstances, it happens that the number
is displayed to the user in the exponential form. For e.g.
282748441902441558837890625 will be displayed as 2.82748441902442E+26.
How do I make VB display the number as it is & not in the exponential
form?

Thanks once again,

Regards,

Arpan
Author
11 Oct 2005 1:36 AM
Someone
Double has about 15 digits accuracy, so it's better not to reach that. You
could limit the result to 15 digits by checking "If x >= 1e15 Then" and show
the user an error. If you want more accuracy, use a third party math
library.

If you want to get rid of "E", use the Format function, like the following.

? format(282748441902441558837890625,"################################")

See the Format function help, and click on "See Also", followed by
"User-Defined Numeric Formats" in Visual Basic Reference in MSDN.


Show quoteHide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128993350.589501.267970@g47g2000cwa.googlegroups.com...
> Thanks, Michael & Someone, for your suggestions.
>
> I have another query pertaining to this - my VB application lets users
> do a lot of calculations. Often the end result may turn out to be a
> very big number. Under such circumstances, it happens that the number
> is displayed to the user in the exponential form. For e.g.
> 282748441902441558837890625 will be displayed as 2.82748441902442E+26.
> How do I make VB display the number as it is & not in the exponential
> form?
>
> Thanks once again,
>
> Regards,
>
> Arpan
>
Author
11 Oct 2005 1:52 AM
Arpan
Thanks, Mike, for your detailed explanation. Here are the answers to
your queries:

>>An overflow error?  Or an "Out of stack space" error?

It's an overflow error only, not an "out of stack space" error.

>>Given your code, an
>>overflow error could be possible, but repeatedly >>clicking the command button
>>is more akin to an out of stack space error (of >>course, it would have to be
clicked a LOT in a VERY short time and probably >>impossible to do)
>>Therefore, I doubt it has anything to do with >>repeatedly clicking the
>>command button. My guess is you just don't know >>what an overflow error is
>>(no offense) and for whatever reason assumed it >>had to do with repeatedly
clicking the command button.

It's not a question of how many times the button has been clicked in
how much time. If the user has entered, say, 50 in the textbox & then
clicks the CommandButton, the result will be 2500. He again clicks the
button & gets the result 6250000. He goes on squaring the result again
& again; that is when the overflow error gets generated.

>>However, since the result (400000000) is much >>greater than an Integer can
>>hold, you get an Overflow.

That's exactly what is happening.

>>Given your code, this would be:
>>iSquare = CDbl(txtNum1.Text) * CDbl(txtNum1.Text)

I tried this out but it results in the overflow error.

>>Why are you using a prefix of "i" for a variable
>>of type Double? That makes no sense. The "i" >>prefix is almost unquestionably
>>used for an Integer data type. Using "I' as the prefix >>for a Double data
>>type is just making your code...well....bad

I will get rid of this bad practice sooner than later :-)

>>From what you posted, it
>>appears as if clicking this button might be doing >>more than just calculating
>>the square of a number (which is probably bad in >>itself if the button's
>>purpose is only to do this calculation).

The button's sole purpose is to calculate the square of a number,
nothing more, nothing less.

Lastly thanks once again for your comprehensive explanation.

Regards,

Arpan
Author
11 Oct 2005 2:26 AM
MikeD
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128995563.109608.69420@g14g2000cwa.googlegroups.com...

> It's not a question of how many times the button has been clicked in
> how much time. If the user has entered, say, 50 in the textbox & then
> clicks the CommandButton, the result will be 2500. He again clicks the
> button & gets the result 6250000. He goes on squaring the result again
> & again; that is when the overflow error gets generated.


Ah! So you're ultimately assigning the result BACK to the textbox and then
squaring that value again.  That was rather an important detail you
originally left out. See?  Post the complete code and tell us exactly what
you're doing.  <g>

>>>Given your code, this would be:
>>>iSquare = CDbl(txtNum1.Text) * CDbl(txtNum1.Text)
>
> I tried this out but it results in the overflow error.

With what value assigned to the textbox? If you're getting an overflow error
with Doubles, then you're talking about some REALLY large numbers.  At some
point, you're going to eventually even go out of the range of a Double
(given you continue to square the previous result, which you've only now
stated that you're doing).

Again, I compel you to post your EXACT code and provide whatever test values
you're entering into the textbox and tell us exactly what you then do to
cause this error.  The BEST way we can help is if we can reproduce the
error/problem.

>
>>>Why are you using a prefix of "i" for a variable
>>>of type Double? That makes no sense. The "i" >>prefix is almost
>>>unquestionably
>>>used for an Integer data type. Using "I' as the prefix >>for a Double
>>>data
>>>type is just making your code...well....bad
>
> I will get rid of this bad practice sooner than later :-)

Good!

>
>>>From what you posted, it
>>>appears as if clicking this button might be doing >>more than just
>>>calculating
>>>the square of a number (which is probably bad in >>itself if the button's
>>>purpose is only to do this calculation).
>
> The button's sole purpose is to calculate the square of a number,
> nothing more, nothing less.

That's not quite true.  Apparently, something else it's doing is then
assigning the calculated value back to the textbox. I don't know how much
more I can stress this, but when getting help, you cannot skimp on EXACTLY
what you're doing. Anytime there's an unknown or uncertainty, all we can do
is guess. Much of the time, our guesses are right on the mark.  But just as
many times, they're not. You're not helping yourself in that case, and it's
just going to take longer to get a "correct:" answer.

--
Mike
Microsoft MVP Visual Basic
Author
11 Oct 2005 3:02 AM
Arpan
Basically this application is somewhat like a calculator. The Form has
a Label, 2 hidden TextBoxes & apart from other CommandButtons, 1
CommandButton to get the square.

Now suppose the user enters the number 50. 50 gets displayed as the
caption of the Label. At the same time, the 1st hidden textbox (named
txtNum1) also gets populated with 50. Next the user clicks the
CommandButton to get the square of 50. When he does so, txtNum1 gets
populated with the square of 50 i.e. 2500. At the same time, the other
hidden textbox (named txtOutput) also gets populated with 2500. Finally
2500 is displayed to the user in the Label. This is the code (a small
one):

Private Sub cmdSqr_Click()
    Dim dblSquare As Double
    dblSquare = CDbl(txtNum1.Text) * CDbl(txtNum1.Text)
    txtOutput.Text = dblSquare
    txtNum1.Text = dblSquare

    cmdSqr.FontBold = True
    cmdSqr.BackColor = &H80000000
End Sub

That's it!

Arpan
Author
11 Oct 2005 3:45 AM
MikeD
Show quote Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128999753.427797.229560@o13g2000cwo.googlegroups.com...
> Basically this application is somewhat like a calculator. The Form has
> a Label, 2 hidden TextBoxes & apart from other CommandButtons, 1
> CommandButton to get the square.
>
> Now suppose the user enters the number 50. 50 gets displayed as the
> caption of the Label. At the same time, the 1st hidden textbox (named
> txtNum1) also gets populated with 50. Next the user clicks the
> CommandButton to get the square of 50. When he does so, txtNum1 gets
> populated with the square of 50 i.e. 2500. At the same time, the other
> hidden textbox (named txtOutput) also gets populated with 2500. Finally
> 2500 is displayed to the user in the Label. This is the code (a small
> one):
>
> Private Sub cmdSqr_Click()
>    Dim dblSquare As Double
>    dblSquare = CDbl(txtNum1.Text) * CDbl(txtNum1.Text)
>    txtOutput.Text = dblSquare
>    txtNum1.Text = dblSquare
>
>    cmdSqr.FontBold = True
>    cmdSqr.BackColor = &H80000000
> End Sub
>
> That's it!


OK. I tried that and stepped through it.  Once it got to "1E + 256', the
next time the command button was clicked, it overflowed. It exceeded the
range of a Double. Go to something larger. Not sure what that would be.
Maybe a Variant would allow a larger value (I make every attempt to avoid
variants so I'm not sure). But if you keep squaring the result, you're
eventually going to hit a value too large for VB to handle.  Nothing you can
do about that except trap the error (and take whatever action) or write your
program in another language that supports larger values for numeric data
types.

Those are really your only choices.

--
Mike
Microsoft MVP Visual Basic
Author
11 Oct 2005 4:07 AM
Arpan
Yup......that's right.........either trap the error or go for some
other programming language. I will stick to the former.

Arpan
Author
11 Oct 2005 12:49 PM
Duane Bozarth
Arpan wrote:
>
> Yup......that's right.........either trap the error or go for some
> other programming language. I will stick to the former.

Or find (or write) an extended precision package...I know of several for
Fortran, and suspect there are some for VB. 

You could look into the other numeric forms in VB as well.  They're not
fully supported for all applications but will handle simple arithmetic
operations (I think, altho I haven't used them myself and haven't tested
for this post).
Author
11 Oct 2005 1:58 AM
MikeD
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128993350.589501.267970@g47g2000cwa.googlegroups.com...
> I have another query pertaining to this - my VB application lets users
> do a lot of calculations. Often the end result may turn out to be a
> very big number. Under such circumstances, it happens that the number
> is displayed to the user in the exponential form. For e.g.
> 282748441902441558837890625 will be displayed as 2.82748441902442E+26.
> How do I make VB display the number as it is & not in the exponential
> form?


This really isn't related to the subject of your original post and therefore
should have been a new post with a new subject.

Look up the Format function (and be sure to click on the See Also link for
additional topics). You can use it to format the display of pretty much
anything you want in any way you want.  In the case of very large or small
numbers, VB defaults to displaying it using scientific notation. If that's
not what you want, you need to write code to format the number otherwise.

This kind of goes along with what I said before about not letting VB coerce
data types. VB will also display things in certain ways that may not be
desirable.  You have to control that yourself, just like you have to (or
should) control converting from one data type to another. VB does these
things for you to make the language easier to use. But that doesn't mean
it's better, and usually it doesn't give you exactly what you want.  To get
what you want, you have to write the code to do it.

--
Mike
Microsoft MVP Visual Basic
Author
11 Oct 2005 12:58 AM
MikeD
Show quote Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128987105.406938.32240@o13g2000cwo.googlegroups.com...
> One of the CommandButtons in a VB6 application is used to get the
> square of a number. Now what I find is if this button is clicked
> repeatedly, after sometime, VB generates the "Overflow" error. How do I
> avoid this error? This is what I have done:
>
> Private Sub cmdSqr_Click()
>    Dim iSquare As Double
>    iSquare = txtNum1.Text * txtNum1.Text
>    .............
>    .............
>    .............
>
> End Sub

An overflow error?  Or an "Out of stack space" error?  Given your code, an
overflow error could be possible, but repeatedly clicking the command button
is more akin to an out of stack space error (of course, it would have to be
clicked a LOT in a VERY short time and probably impossible to do).
Therefore, I doubt it has anything to do with repeatedly clicking the
command button. My guess is you just don't know what an overflow error is
(no offense) and for whatever reason assumed it had to do with repeatedly
clicking the command button.

As far as correcting a potential overflow error in your code, convert those
strings from the textbox to explicit numeric data types.  You should NEVER
be performing mathematical operations using strings.  Since you've declared
the variable which is assigned the result to be a Double, you should
probably convert the strings to Doubles as well, although that depends more
on what values could be typed into the textbox.

The reason you're getting the overflow is that you're letting VB coerce the
strings to whatever data type it "thinks" is best (which is to say that it
uses the smallest that it can). This frequently has very bad consequences.
Never let VB coerce data types (and it's poor practice anyway even if it
doesn't cause a problem).  ALWAYS convert them yourself so that you're
working with the exact data types you need.

For example, lets say the string assigned to txtNum1.Text is 20000.
Literally, what you have is 20000 * 20000 with a result of 400000000.
However, if you try this (you can even do it in the Immediate window: Print
20000 * 20000), you get an Overflow error.  To those that don't know better,
why you get the error probably doesn't make any sense, so I'll try to
explain as clearly as possible why you get it.  It's because 20000 is within
the range of an Integer data type, so that's what VB uses.  Now, when you
multiply those together, since both are Integers, VB attempts to store the
result in a temporary variable (that it creates itself BEFORE assigning the
result to the variable of type Double) that is also an Integer (because both
operands are Integer type, so VB naively expects the result to be within the
range of an Integer since only Integers are involved in the expression).
However, since the result (400000000) is much greater than an Integer can
hold, you get an Overflow.

To "fix" this (it's NOT a bug by the way; it's just the way VB works), you
need to convert at least one of the operands to a sufficiently large enough
data type to store the result.  Given your code, this would be:

iSquare = CDbl(txtNum1.Text) * CDbl(txtNum1.Text)

Notice that I converted both operands to Double.  This is because, as I
said, you should never perform mathematical operations using strings, just
like you really shouldn't perform string concatenation using numeric data
types (although that's not as much of a problem as long as you use the &
operator).  Strictly speaking, one of the operands could be converted to a
lesser numeric data type and maybe even Double is overkill (as mentioned, it
depends on what values the user could type into the textbox; hopefully,
you're checking for out-of-range values, but somehow I doubt that you are).

I have to ask something.  Why are you using a prefix of "i" for a variable
of type Double? That makes no sense. The "i" prefix is almost unquestionably
used for an Integer data type. Using "I' as the prefix for a Double data
type is just making your code...well....bad.

Getting back to preventing the user from clicking the command button
repeatedly while the Click procedure is running, just disable it as the very
first thing you do in the procedure and re-enable as the very last thing you
do in the procedure.  For example:

Private Sub cmdSqr_Click()
    Dim iSquare As Double

    cmdSqr.Enabled = False
    iSquare = txtNum1.Text * txtNum1.Text
    .............
    .............
    .............

    cmdSqr.Enabled = True
End Sub

One final comment, if none of this solves the problem, then you need to
provide COMPLETE code that reproduces the problem.  From what you posted, it
appears as if clicking this button might be doing more than just calculating
the square of a number (which is probably bad in itself if the button's
purpose is only to do this calculation). The problem might be in the code
you didn't include.

--
Mike
Microsoft MVP Visual Basic
Author
11 Oct 2005 6:21 PM
Marv Wade
Check out this site . It includes a routine that will handle values of  very
high number of digits.

http://www.karenware.com/powertools/

Marv

Show quoteHide quote
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128987105.406938.32240@o13g2000cwo.googlegroups.com...
> One of the CommandButtons in a VB6 application is used to get the
> square of a number. Now what I find is if this button is clicked
> repeatedly, after sometime, VB generates the "Overflow" error. How do I
> avoid this error? This is what I have done:
>
> Private Sub cmdSqr_Click()
>    Dim iSquare As Double
>    iSquare = txtNum1.Text * txtNum1.Text
>    .............
>    .............
>    .............
>
> End Sub
>
> Thanks,
>
> Arpan
>