Why is it that when I type
Into VBA's Immediate Window (in Access) I get an Overflow error? And why is it that when I typeCode:? 240 * 256
I don't? :confused:Code:? 240 * 65536
(The numbers are just examples by the way, not special cases)
Printable View
Why is it that when I type
Into VBA's Immediate Window (in Access) I get an Overflow error? And why is it that when I typeCode:? 240 * 256
I don't? :confused:Code:? 240 * 65536
(The numbers are just examples by the way, not special cases)
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.
In your 2nd example the value 65536 is implicittly typed as Long, so the product will also receive a Long type - hence no overflow.Code:? clng(240) * clng(256)
Just another reason why you should always use Option Explicit. ;)
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.