Results 1 to 3 of 3

Thread: God I hate VBA... (Overflow error question)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    God I hate VBA... (Overflow error question)

    Why is it that when I type

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

    Code:
    ? 240 * 65536
    I don't?

    (The numbers are just examples by the way, not special cases)
    Using Visual Studio .NET 2005

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: God I hate VBA... (Overflow error question)

    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.

    Code:
    ? 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.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: God I hate VBA... (Overflow error question)

    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.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width