473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Overflow? What gives?

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
7 4837
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
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
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
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
7
by: MLH | last post by:
?2944*24 gives me an overflow error, entered in the immediate window. Anybody else?
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
9
by: Mikael Svenson | last post by:
Sample code: static void Main(string args) { double inf = 0.1 / 0.0; int test2 = (int)(inf * 0.0F); Console.WriteLine( test2.ToString() ); } On a P3 this gives the result "0", on a P4 it...
3
by: tshad | last post by:
I have the following that works: public static void GetValueFromDbObject(object dbObjectValue, ref decimal destination) { destination = 2323; return; } This gives me an arithmetic overflow:
4
by: vir | last post by:
we use an E-office application where server is SQL server 7.0 and client side its MS access In our VB program we use ASP to synchronization and update client database from server and each table...
10
Robbie
by: Robbie | last post by:
Hi all, I'm relatively new to VB but I know that an overflow occurs when you try to make a variable be too high or low for what you've prepared it for, e.g. 300 when you've prepared it for use as a...
3
by: jer006 | last post by:
Hi I am writing a select statement that has an arithmetic function inside a case statement that uses logic to decide whether to divide or multiply and when I run the arithmetic statements outside...
6
by: Chris Becke | last post by:
I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.