# 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.

Thank you.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Author Commented:
I tried to input value on txtDiameter. Still giving me error. Am i missing something in my code?
0
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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
Author Commented:
Thank you LionKing for your help. It's working now.
Sorry it took me long to answer since holiday in Louisiana yesterday.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.