 | | | | |  | | | | | Guest | Overflow? What gives? -
06-04-2007, 10:35 AM
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 | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
> 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 | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
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 | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
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)" <EMAIL REMOVED> wrote in
message news:UJCdnXx-EMAIL REMOVED...
>> 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
> | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
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)" <EMAIL REMOVED> wrote in
message news:_EMAIL REMOVED. ..
> 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
> | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
> 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 | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
>> 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 | | | | | | | | Guest | Re: Overflow? What gives? -
06-04-2007, 10:35 AM
Thanks again Rick. Will do!
"Rick Rothstein (MVP - VB)" <EMAIL REMOVED> wrote in
message news:EMAIL REMOVED. ..
>> 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
> | | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |  |