Results 1 to 4 of 4

Thread: Overflow Error:

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Question Overflow Error:

    I am casting from string to int using both the Val and CLng functions. The program must deal with 6 digit numbers and I was under the impression that CLng could handle these but I am getting the error,

    Heres the code:

    If txtRentalPrice.Text = "" Then
    intRentalPrice = "0"
    Else: intRentalPrice = CLng(Val(txtRentalPrice.Text))
    End If

    Any ideas? Thanks Mark

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Overflow Error:

    It worked fine for me. You said you were dealing with six digit numbers so I used 999999 and had no problems.

    I do have one question though. Why is intRentalPrice (which implies integer, lng implies a Long) being set to a string? "0" is string...0 is an integer. Here is what I tested
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngRentalPrice As Long
    3. If txtRentalPrice.Text = "" Then
    4.     lngRentalPrice = 0
    5. Else
    6.     lngRentalPrice = CLng(Val(txtRentalPrice.Text))
    7. End If
    8. MsgBox lngRentalPrice
    9. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: Overflow Error:

    Ahh

    I was declaring the variable as type Integer rather than Long!

    Thanks for your help Hack

  4. #4
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Overflow Error:

    As Hack said, it should easily handle any 6-digit value. In fact, it's good up to any 9-digit value... Unless the value in the TextBox ends with a "%" (e.g., "999999%"). In such a case, Val will try to convert to an Integer - causing an overflow.

    As an aside, the whole IF check is unnecessary as the Val function will convert "" to zero. All you need is:
    VB Code:
    1. lngRentalPrice = CLng(Val(txtRentalPrice.Text))
    no conditional check for "" is required.

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