Run-time Error '11' Division by zero in Visual Basic

I have 5 text boxes. 1 for Length, 1 for Diameter, 1 for Specific Gravity, 1 for the Total and 1 for Diameter Calculation textbox. The order of textboxes are Length, Diameter, Specific Gravity and Total. The txtDiameterCalculation is hidden and the calculation is based when key in txtDiameter.

Formula for the textbox when user key the data:
txtDiameterCalculation:
txtDiameterCalculation = Val(txtDiameter) * 25.4 / 2

txtLength:  
txtTotal = CInt(1555.2 * Val(txtLength) / (Val(txtDiameterCalculation) ^ 2) * 3.14159265359 * Val(txtSpecificGravity) / 25.4)

txtSpecificGravity:
txtTotal = CInt(1555.2 * Val(txtLength) / (Val(txtDiameterCalculation) ^ 2) * 3.14159265359 * Val(txtSpecificGravity) / 25.4)

Problem is when keying number to the txtLength, there is an error called Run-time error '11' Division by zero.

Can you please help with this error?

Thank you.
willhebertAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
LIONKINGConnect With a Mentor Commented:
Try removing the Val() and use CDbl() instead, which actually converts the value to a double format.
Do you have any code in the Change event for txtDiameter?
0
 
LIONKINGCommented:
You should validate that txtDiameter is not zero (0) when calculating txtlength / txtSpecificGravity...
If they are zero, then you shouldn't perform the operation, since it's always gonna divide by zero.

Something like:
If cdbl(txtlengt.text)=0 then
   msgbox "You have entered zero"
   Exit sub
End If
But obviously your validation process must be adequate for the calculation process.
Tell me if this is your problem.
0
 
willhebertAuthor Commented:
I tried to input value on txtDiameter. Still giving me error. Am i missing something in my code?
0
 
ee_rleeCommented:
hi

the value of txtDiameterCalculation is always zero because it is not being set.

you should put this under txtDiameter and not txtDiameterCalculation
txtDiameterCalculation = Val(txtDiameter) * 25.4 / 2
0
 
willhebertAuthor Commented:
Thank you LionKing for your help. It's working now.
Sorry it took me long to answer since holiday in Louisiana yesterday.
0
All Courses

From novice to tech pro — start learning today.