Results 1 to 6 of 6

Thread: Help with Prevention of Dividing by Zero

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Location
    Canada
    Posts
    23

    Exclamation Help with Prevention of Dividing by Zero


    I need a bit of help in preventing division by zero in my calculator.
    Here's the code I currently have:

    Code:
    Private Sub Form_Load()
        'To stop yourself from dividing by zero.
        If Val(Text1.Text) And Val(Text2.Text) = 0 Then
        cmdDivide.Enabled = False
        If Val(Text1.Text) And Val(Text2.Text) <> 0 Then
        cmdDivide.Enabled = True
        End If
    End Sub
    I'm just not sure in how to combine the values of Text1 and Text2 together.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help with Prevention of Dividing by Zero

    Try using the "or" condition.
    vb Code:
    1. Private Sub Form_Load()
    2.     'To stop yourself from dividing by zero.
    3.     If Val(Text1.Text) And Val(Text2.Text) = 0 or  If Val(Text1.Text) And Val(Text2.Text) <> 0 Then
    4.     cmdDivide.Enabled = False
    5.     else
    6.     cmdDivide.Enabled = True
    7.     End If
    8. End Sub

    I haven't tested the code but it should work.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Help with Prevention of Dividing by Zero

    Code:
    Private Sub Form_Load()
    'To stop yourself from dividing by zero.
    If Val(Text1.Text) Then cmdDivide.Enabled = True Else cmdDivide.Enabled = False
    End Sub
    Doctor Ed

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Help with Prevention of Dividing by Zero

    Code Doc, I think it should be Text2, not Text1. From 1st post, it appears Text2 will be the divisor
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Help with Prevention of Dividing by Zero

    Quote Originally Posted by LaVolpe View Post
    Code Doc, I think it should be Text2, not Text1. From 1st post, it appears Text2 will be the divisor
    +1, Fox. You only have to check for the denominator (or divisor). Looks like Text2.Text contains the divisor. OP should advise. If so,
    Code:
    Private Sub Form_Load()
    'To stop yourself from dividing by zero.
    If Val(Text2.Text) Then cmdDivide.Enabled = True Else cmdDivide.Enabled = False
    End Sub
    Doctor Ed

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Location
    Canada
    Posts
    23

    Re: Help with Prevention of Dividing by Zero

    Thank you Nightwalker83! It works now.

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