Results 1 to 5 of 5

Thread: [RESOLVED] Can't divide by 0

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    71

    Resolved [RESOLVED] Can't divide by 0

    I have a program where it divides two numbers from text boxes. Whenever I have both the numbers at 0 I get "Run time 6 Overflow". Thats aboviously because you can't divide by 0. However I'm trying to make it so when you try to divide by 0 then nothing happens instead of just closing the program. I've tried
    If text1.text = 0 and text2.text = 0 then text3.text = 0 else: text3.text = val(text1.text) / val(text2.text)

    But I still come up with the error, and when I try to debug it highlights the else statement. Any know what to do?

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Can't divide by 0

    try just:

    VB Code:
    1. If Val(Text2.Text) = 0 Then Text3.Text = "0" Else Text3.Text = Val(Text1.Text) / Val(Text2.Text)

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Can't divide by 0

    The simplest thing you can do is something like the following sample:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If IsNumeric(Text1.Text) And IsNumeric(Text2.Text) Then
    3.         If Val(Text2.Text) > 0 Then
    4.             Text3.Text = Val(Text1.Text) / Val(Text2.Text)
    5.         End If
    6.     End If
    7. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    71

    Arrow Re: Can't divide by 0

    Alright thanks both of those worked.

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

    Re: [RESOLVED] Can't divide by 0

    You could also trap the error, display a message to your user indicating what happened and why, then have your program move on.

    Error trapping is something your should have in your code anyway.

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