PDA

Click to See Complete Forum and Search --> : God I hate VBA... (Overflow error question)


Barguast
Jan 16th, 2006, 02:50 PM
Why is it that when I type

? 240 * 256

Into VBA's Immediate Window (in Access) I get an Overflow error? And why is it that when I type

? 240 * 65536

I don't? :confused:

(The numbers are just examples by the way, not special cases)

DKenny
Jan 16th, 2006, 03:09 PM
Access assumes that both 240 and 256 as typed have an integer datatype. The product of these two numbers is greater that the max value for the integer datatype but because Access has classified them both as integers it tries to fit the product into an integer- hence the overflow.
If you type the following in the Immediate window, you don't get the error.

? clng(240) * clng(256)

In your 2nd example the value 65536 is implicittly typed as Long, so the product will also receive a Long type - hence no overflow.

Just another reason why you should always use Option Explicit. ;)

Webtest
Jan 17th, 2006, 10:39 AM
I LOVE VBA! (Though I didn't when I first started!)

You can also type:

? 256& * 333&

The "&" casts the entered number to Type Long, but don't bother trying to look up "&" in the (2003) HelpHeap! I have a note in the margin of "Definitive Guide to Excel VBA" (Kofler) about "use trailing & for Hex" (???) but I've forgotten exactly what that means. It does not mean hex in the above example.