views:

23

answers:

1

I am using the following expression in a local report for a WinForms application, but I receive "#Error" when the field value is null:

=IIf(Fields!MyField.Value = "", "NULL", Left(Fields!MyField.Value, Len(Fields!MyField.Value) - 2))

I am doing this to strip off a trailing comma and space of the value. The IIf() works, the Left() works, and the Len() works... but not when I add in the "- 2" part of the expression.

I assumed null values (or ZLSs) would be a non-issue since I am pulling them out before the latter half of the expression is evaluated.

Any ideas what might be causing this? Assistance is greatly appreciated!

A: 

It looks to me like your inline if is only screening for empty strings, and then assigning the value to null. Null values (and strings of length 1, actually) will break this code.

Try using this:

=IIf(IsNull(Fields!MyField.Value), "NULL", 
(IIf (Len(Fields!MyField.Value) < 2, "SHORT", 
Left(Fields!MyField.Value, Len(Fields!MyField.Value) - 2)))

I added the line breaks for readability. You can use whatever string in place of "SHORT", obviously. This prevents Len(Fields!MyField.Value) - 2 from evaluating to a negative number, which I bet is causing your issue.

iandisme