Problem with divided by zero

J

Jeff

Hi,

The following codes still return an "overflow" message. I need your help to
fix it. Thank you.

Private Sub Test_Click()
Dim NSD As Long
Dim CS As Long
Dim Total As Long
Dim NSDRate As Single

NSD = 0
CS = 0
Total = NSD + CS
NSDRate = IIf(Total = 0, 0, NSD / Total)

MsgBox NSDRate
End Sub--

Jeff
 
J

Jack Leach

The VBA Iif function evaluates both the True and False arguments, no matter
which one it needs. Therefore you will have to switch this to a conventional
If/Then statement instead of IIf:

If Total <> 0 Then
NSDRate = NSD / Total
Else
NSDRate = 0
End If


hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
D

Douglas J. Steele

Just a comment.

Jack's correct that the IIf statement evaluates both the True and False
arguments when executing in VBA. This isn't the case, though, when an IIf
statement is used in a query. Using IIf(Total = 0, 0, NSD / Total) as a
computed field in a query would work as desired.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top