By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Overflow? What gives?

P: n/a
I'm puzzled.

The following code is designed to take a date and a weekday count value, and
return the future date.

For example, if I supply it a starting date of 4/25/2007 and a count of 37,
it should return a date that is 37 weekdays only from 4/25/2007. It should
not count any weekends, only Monday-Friday days.

When I ran this code, it came back with an OVERFLOW error. Huh?

Function ForwardDate(ByVal EndDate As Date, _
ByVal DaysForward As Double) As Date

Dim RoundedDays As Long
Dim x As Long
Dim CalendarDays As Long
Dim Weeks As Integer
Dim Remainder As Single
Dim TimeDay As Date

RoundedDays = CInt(DaysForward)

Weeks = Fix(RoundedDays / 5) 'trading day weeks

Remainder = RoundedDays Mod 5 'days left over

'***** THIS IS THE LINE THAT GIVES THE ERROR *****
'when Weeks = 5731. That comes out to 40117 which should be within
'the LONG range of CalendarDays.

CalendarDays = Weeks * 7 'convert trading day weeks to calendar days

TimeDay = EndDate + CalendarDays 'expand out by calendardays. should
fall on same weekday

'Now add the remainder days excluding weekends
For x = 1 To Remainder
TimeDay = TimeDay + 1
If Weekday(TimeDay) = vbSaturday Then: TimeDay = TimeDay + 2 'shift
from Saturday to Monday
Next x
ForwardDate = TimeDay

End Function

======================

Program stops with an Overflow error on the line CalendarDays = Weeks * 7.
Looking at Weeks, it was equalling 5731. That is within range for an INT.
And when multiplied by 7, that should be within range of a LONG.

Can anyone see what I did wrong here?

Thanks.

Webbiz
Apr 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Dim Weeks As Integer
>
'***** THIS IS THE LINE THAT GIVES THE ERROR *****
'when Weeks = 5731. That comes out to 40117 which should be within
'the LONG range of CalendarDays.

CalendarDays = Weeks * 7 'convert trading day weeks to calendar days
Because Weeks is declared as an Integer and 7 can fit into an Integer, VB
stores the intermediate calculation in as Integer, or at least it tries
to... 40117 is too big to fit in an Integer, so you get an overflow error.
There is almost no reason to declare variables as Integer (Longs are faster
in a 32-bit world), so I would simply declare Weeks as Long, which should
take care of your problem (as long as one of the values is bigger than an
Integer, VB will not perform the intermediate calculations as Integer, even
if the other value is an Integer). If you need Weeks to be an Integer for
other reasons, then simply change 7 to 7& (which make 7 a Long value); or
change the 7 to CLng(7) which has the same effect.

Rick
Apr 24 '07 #2

P: n/a
For future reference...

From a post by Jeff Johnson:

"You have posted this question individually to multiple groups.
This is called Multiposting and it's BAD. Replies made in one
group will not be visible in the other groups, which may cause
multiple people to respond to your question with the same answer
because they didn't know someone else had already done it. This
is a waste of time.

If you MUST post your message to multiple groups, post a single
message and select all the groups (or type their names manually
in the Newsgroups field, separated by commas) in which you want
it to be seen. This is called Crossposting and when used properly
it is GOOD."

Some additional comment previously posted by me:

"You may not see this as a problem, but those of us who volunteer
answering questions on newsgroups do see it as a problem. You can't
imagine how annoying it is for a volunteer to read a question,
research background material, test sample code and then formulate
and post an answer to the original question only to go to another
newsgroup and find the question posted and ALREADY answered over
there. On top of that, if you cross-post your question, all of the
readers in all the newsgroups it is cross-posted to see both the
original question and all of the answers given to it. This is
beneficial to you because then we can add additional material to,
add clarification to, as well as add additional examples to an
answer you have received previously... that means you end up with
a more complete solution to your problem. This is a win-win
situation for all of us."

Rick
Apr 24 '07 #3

P: n/a
Thanks Rick.

After some thought, I thought that maybe this was the case, that VB was
'temporarily' storing the result in Weeks thus causing the overflow. So I
had changed it to Long type.

Thanks for the advice to do so for all my Ints in the future.

Regards,

Webbiz
"Rick Rothstein (MVP - VB)" <ri************@NOSPAMcomcast.netwrote in
message news:UJ******************************@comcast.com. ..
>Dim Weeks As Integer

'***** THIS IS THE LINE THAT GIVES THE ERROR *****
'when Weeks = 5731. That comes out to 40117 which should be within
'the LONG range of CalendarDays.

CalendarDays = Weeks * 7 'convert trading day weeks to calendar days

Because Weeks is declared as an Integer and 7 can fit into an Integer, VB
stores the intermediate calculation in as Integer, or at least it tries
to... 40117 is too big to fit in an Integer, so you get an overflow error.
There is almost no reason to declare variables as Integer (Longs are
faster in a 32-bit world), so I would simply declare Weeks as Long, which
should take care of your problem (as long as one of the values is bigger
than an Integer, VB will not perform the intermediate calculations as
Integer, even if the other value is an Integer). If you need Weeks to be
an Integer for other reasons, then simply change 7 to 7& (which make 7 a
Long value); or change the 7 to CLng(7) which has the same effect.

Rick

Apr 24 '07 #4

P: n/a
Sorry about that.

I had lost all my newsgroups and wasn't sure which of the two VB newsgroups
was the one I used to participate in months back. I tried to find names I
recognized (like yours) and didn't see any in the initial list that came up
when I clicked on the newsgroup name.

In addition, I noted some 'not so nice' language used on one of the two and
figured it may not be the same newsgroup I participated in before. But I
wasn't sure. So I posted in both and thought to see which is actually the
one that has good participation.

Webbiz

"Rick Rothstein (MVP - VB)" <ri************@NOSPAMcomcast.netwrote in
message news:_Z******************************@comcast.com. ..
For future reference...

From a post by Jeff Johnson:

"You have posted this question individually to multiple groups.
This is called Multiposting and it's BAD. Replies made in one
group will not be visible in the other groups, which may cause
multiple people to respond to your question with the same answer
because they didn't know someone else had already done it. This
is a waste of time.

If you MUST post your message to multiple groups, post a single
message and select all the groups (or type their names manually
in the Newsgroups field, separated by commas) in which you want
it to be seen. This is called Crossposting and when used properly
it is GOOD."

Some additional comment previously posted by me:

"You may not see this as a problem, but those of us who volunteer
answering questions on newsgroups do see it as a problem. You can't
imagine how annoying it is for a volunteer to read a question,
research background material, test sample code and then formulate
and post an answer to the original question only to go to another
newsgroup and find the question posted and ALREADY answered over
there. On top of that, if you cross-post your question, all of the
readers in all the newsgroups it is cross-posted to see both the
original question and all of the answers given to it. This is
beneficial to you because then we can add additional material to,
add clarification to, as well as add additional examples to an
answer you have received previously... that means you end up with
a more complete solution to your problem. This is a win-win
situation for all of us."

Rick

Apr 24 '07 #5

P: n/a
I had lost all my newsgroups and wasn't sure which of the two VB
newsgroups was the one I used to participate in months back. I tried to
find names I recognized (like yours) and didn't see any in the initial
list that came up when I clicked on the newsgroup name.
You should consider using Microsoft's public newsgroups; specifically, set
up an account for

msnews.microsoft.com

and select from among the newsgroups with 'vb' in their names. Most activity
seems to occur in this one...

microsoft.public.vb.general.discussion

Rick
Apr 24 '07 #6

P: n/a
>I had lost all my newsgroups and wasn't sure which of the two VB
>newsgroups was the one I used to participate in months back. I tried to
find names I recognized (like yours) and didn't see any in the initial
list that came up when I clicked on the newsgroup name.

You should consider using Microsoft's public newsgroups; specifically, set
up an account for

msnews.microsoft.com

and select from among the newsgroups with 'vb' in their names. Most
activity seems to occur in this one...

microsoft.public.vb.general.discussion
But if you want to stay in the comp.lang newsgroups, I would suggest using

comp.lang.basic.visual.misc

which seems to get more activity than the two you selected.

Rick
Apr 25 '07 #7

P: n/a
Thanks again Rick. Will do!
"Rick Rothstein (MVP - VB)" <ri************@NOSPAMcomcast.netwrote in
message news:dI******************************@comcast.com. ..
>I had lost all my newsgroups and wasn't sure which of the two VB
newsgroups was the one I used to participate in months back. I tried to
find names I recognized (like yours) and didn't see any in the initial
list that came up when I clicked on the newsgroup name.

You should consider using Microsoft's public newsgroups; specifically, set
up an account for

msnews.microsoft.com

and select from among the newsgroups with 'vb' in their names. Most
activity seems to occur in this one...

microsoft.public.vb.general.discussion

Rick

Apr 27 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.