473,394 Members | 1,865 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,394 software developers and data experts.

Constant error with calculation

Dim lngWeights as long, lngBuckets as Long

Const STDWEIGHTS = 12

lngBuckets = 1000
lngWeights = lngBuckets * STDWEIGHTS

This causes an Overflow error. For lower values it works. Obviously the
result of 36000 is more than an Integer can take, but not a Long?? Yes, I
did double-check my code.

Change the constant declaration to -

Const STDWEIGHTS as Long = 12

And problem goes away.

Why must the constant be specifically declared as a Long?

Jeff
Nov 12 '05 #1
5 2578
Jeff, I cannot repro your error.

Firstly, the result of the calculation is < 32767, but even if you set the
constant to 120 I receive no overflow.

When one of the operands is a long, both are converted to long before the
multiplication takes place.

What version of Access are you seeing this in?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jeff" <je************@asken.com.au> wrote in message
news:0R******************@news.optus.net.au...
Dim lngWeights as long, lngBuckets as Long

Const STDWEIGHTS = 12

lngBuckets = 1000
lngWeights = lngBuckets * STDWEIGHTS

This causes an Overflow error. For lower values it works. Obviously the
result of 36000 is more than an Integer can take, but not a Long?? Yes, I
did double-check my code.

Change the constant declaration to -

Const STDWEIGHTS as Long = 12

And problem goes away.

Why must the constant be specifically declared as a Long?

Jeff

Nov 12 '05 #2
Hi Allen

After your comment I went back and checked it. I made a mistake with
lngBuckets. It is an Integer. The value is actually entered into a text box
by user and then passed to a function that contains the code, as below.

fResult = DoIt([txtMyValue])

The function dec

Function DoIt(pintBuckets as Integer) as Boolean
...
End function

I changed that to Long and problem went away.

But why does it still give an error? Why did changing the declaration for
the constant to a long fix it?

Jeff

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Jeff, I cannot repro your error.

Firstly, the result of the calculation is < 32767, but even if you set the
constant to 120 I receive no overflow.

When one of the operands is a long, both are converted to long before the
multiplication takes place.

What version of Access are you seeing this in?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jeff" <je************@asken.com.au> wrote in message
news:0R******************@news.optus.net.au...
Dim lngWeights as long, lngBuckets as Long

Const STDWEIGHTS = 12

lngBuckets = 1000
lngWeights = lngBuckets * STDWEIGHTS

This causes an Overflow error. For lower values it works. Obviously the
result of 36000 is more than an Integer can take, but not a Long?? Yes, I did double-check my code.

Change the constant declaration to -

Const STDWEIGHTS as Long = 12

And problem goes away.

Why must the constant be specifically declared as a Long?

Jeff


Nov 12 '05 #3
Okay, that makes sense.

Whole numbers in VBA default to Integer.
Multiplying two integers results in an integer, so overflow is very easy.
To see in the Immediate Window (Ctrl+G), enter:
? 200 * 200

If either one of the numbers is a Long, Access has to convert the other to a
Long before it can multiply, so you get no error from:
? CLng(200) * 200

A short-hand way to achieve this in your code is to use the type declaration
characters, e.g.:
& Long
# Double
@ Currency
$ String
% Integer

It is poor style to rely on variables that use these suffixes in their name,
but they are actually very useful for literal values. You get no overflow
from:
? 200& * 200

You could even use:
Const STDWEIGHTS = 12&
though I think your original suggestion is better (more readable):
Const STDWEIGHTS as Long = 12

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jeff" <je************@asken.com.au> wrote in message
news:rP******************@news.optus.net.au...

After your comment I went back and checked it. I made a mistake with
lngBuckets. It is an Integer. The value is actually entered into a text box by user and then passed to a function that contains the code, as below.

fResult = DoIt([txtMyValue])

The function dec

Function DoIt(pintBuckets as Integer) as Boolean
...
End function

I changed that to Long and problem went away.

But why does it still give an error? Why did changing the declaration for
the constant to a long fix it?

Jeff

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Jeff, I cannot repro your error.

Firstly, the result of the calculation is < 32767, but even if you set the
constant to 120 I receive no overflow.

When one of the operands is a long, both are converted to long before the multiplication takes place.

What version of Access are you seeing this in?

"Jeff" <je************@asken.com.au> wrote in message
news:0R******************@news.optus.net.au...
Dim lngWeights as long, lngBuckets as Long

Const STDWEIGHTS = 12

lngBuckets = 1000
lngWeights = lngBuckets * STDWEIGHTS

This causes an Overflow error. For lower values it works. Obviously the result of 36000 is more than an Integer can take, but not a Long??
Yes, I did double-check my code.

Change the constant declaration to -

Const STDWEIGHTS as Long = 12

And problem goes away.

Why must the constant be specifically declared as a Long?

Jeff

Nov 12 '05 #4
Why is that an integer is +- 32768 and long is +- 2147483648 big
enough to avoid an error

Use longs, integers cause problems tomorrow that don't exist today.
Nov 12 '05 #5
I get it, but I am stunned. I am surprised I have not come across this
before, and that I never realised this!! Oh well, that's life.

Thanks Allen.

Jeff

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Okay, that makes sense.

Whole numbers in VBA default to Integer.
Multiplying two integers results in an integer, so overflow is very easy.
To see in the Immediate Window (Ctrl+G), enter:
? 200 * 200

If either one of the numbers is a Long, Access has to convert the other to a Long before it can multiply, so you get no error from:
? CLng(200) * 200

A short-hand way to achieve this in your code is to use the type declaration characters, e.g.:
& Long
# Double
@ Currency
$ String
% Integer

It is poor style to rely on variables that use these suffixes in their name, but they are actually very useful for literal values. You get no overflow
from:
? 200& * 200

You could even use:
Const STDWEIGHTS = 12&
though I think your original suggestion is better (more readable):
Const STDWEIGHTS as Long = 12

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jeff" <je************@asken.com.au> wrote in message
news:rP******************@news.optus.net.au...

After your comment I went back and checked it. I made a mistake with
lngBuckets. It is an Integer. The value is actually entered into a text

box
by user and then passed to a function that contains the code, as below.

fResult = DoIt([txtMyValue])

The function dec

Function DoIt(pintBuckets as Integer) as Boolean
...
End function

I changed that to Long and problem went away.

But why does it still give an error? Why did changing the declaration for
the constant to a long fix it?

Jeff

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Jeff, I cannot repro your error.

Firstly, the result of the calculation is < 32767, but even if you set

the constant to 120 I receive no overflow.

When one of the operands is a long, both are converted to long before the multiplication takes place.

What version of Access are you seeing this in?

"Jeff" <je************@asken.com.au> wrote in message
news:0R******************@news.optus.net.au...
> Dim lngWeights as long, lngBuckets as Long
>
> Const STDWEIGHTS = 12
>
> lngBuckets = 1000
> lngWeights = lngBuckets * STDWEIGHTS
>
> This causes an Overflow error. For lower values it works. Obviously the > result of 36000 is more than an Integer can take, but not a Long??

Yes,
I
> did double-check my code.
>
> Change the constant declaration to -
>
> Const STDWEIGHTS as Long = 12
>
> And problem goes away.
>
> Why must the constant be specifically declared as a Long?
>
> Jeff


Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
1
by: Andrew Chanter | last post by:
I have an application that produces examination scores. Candidates have a choice of sitting 1 of 2 subjects or both. I have produced a query that gives all the results for candidates that sat...
3
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the...
4
by: vg-mail | last post by:
Hello all, I have identical design for form and report but I am getting calculation error on form and everything is OK on report. The form and report are build up on SQL statement. The...
10
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
11
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at...
6
by: Lara1 | last post by:
I'm trying to get certain cells to show a hovering alert message when I click on them. (I don't want an error-message style box to pop up, because I'll eventually want it to show for lots of cells...
3
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
34
by: jacob navia | last post by:
Hi I am adding an optimization to lcc-win: sqrt(2.0) will provoke now that the constant 1.4142... etc will be generated instead of generating an actual call. Details: -------
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.