Help with Prevention of Dividing by Zero
:wave:
I need a bit of help in preventing division by zero in my calculator. :confused:
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.
Re: Help with Prevention of Dividing by Zero
Try using the "or" condition.
vb Code:
Private Sub Form_Load()
'To stop yourself from dividing by zero.
If Val(Text1.Text) And Val(Text2.Text) = 0 or If Val(Text1.Text) And Val(Text2.Text) <> 0 Then
cmdDivide.Enabled = False
else
cmdDivide.Enabled = True
End If
End Sub
I haven't tested the code but it should work.
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
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
Re: Help with Prevention of Dividing by Zero
Quote:
Originally Posted by
LaVolpe
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
Re: Help with Prevention of Dividing by Zero
Thank you Nightwalker83! It works now.