Setting a negative number to 0 (1 Viewer)

plasma

Registered User.
Local time
Yesterday, 20:41
Joined
Nov 6, 2006
Messages
22
Hello all, i have a calculation in a report that sometimes results in a negative number. is there anyway to make that negative number = 0 or equal to a specific text?
 

rainman89

I cant find the any key..
Local time
Yesterday, 20:41
Joined
Feb 12, 2007
Messages
3,015
=iff((calculationfield<0), 0,[calculationfield])

or
=iff((calculationfield<0), "thats a negative number ",[calculationfield])
 

plasma

Registered User.
Local time
Yesterday, 20:41
Joined
Nov 6, 2006
Messages
22
never mind, i figured it out. here's what I did:

=IIf([ReorderLevel]-[Units in Stock]>=0,[ReorderLevel]-[Units in Stock],0)
 

Moniker

VBA Pro
Local time
Yesterday, 19:41
Joined
Dec 21, 2006
Messages
1,567
As a Switch (because the Switch saves lines of code):

YourVariableName = Switch([ReorderLevel]-[Units in Stock]>=0,[ReorderLevel]-[Units in Stock],True,0)

The same thing as an If/Then in code:

Code:
If [ReorderLevel]-[Units in Stock]>=0 Then
    YourVariableName = [ReorderLevel]-[Units in Stock]
Else
    YourVariableName = 0
End If

The immediate If will work the same as the Switch in a query, but if you have more than one possible outcome (instead of it's either this or that), then Switch saves you from using multiple nested IIf statements.

For example, this situation:

If [ReorderLevel]-[Units in Stock] > 0 Then you have stock
ElseIf [ReorderLevel]-[Units in Stock] = 0 Then reorder
Else [ReorderLevel]-[Units in Stock] < 0 Then reorder now!

Using IIf, you'd need to nest two of them. Using Switch, you wouldn't. Just some good knowledge to know.
 

Users who are viewing this thread

Top Bottom