I'm creating a calculated field (Field3) in a query in MS Access. In this example, Field2 contains both numeric and character values. I want Field3 to contain only numeric values from Field2 and to convert all character values to Null values so that I can later perform calculations on Field3 as a numeric field. This is in an IIf function because I want Field3 to contain only values from Field2 if Field1 = "AA". This is what I tried typing in the Field row in the Query Design View:
Field3: IIf([Field1]="AA",[Field2]*1,NULL)
This works except where Field2 is a character value then Field3 reads "#Error" instead of being blank.
What is the proper syntax for assigning NULL values if the IIf condition is not met?
If you want the expression to return [Field2]*1
only when [Field1]="AA" and [Field2] contains a number, but otherwise return Null, use IsNumeric()
in the condition ...
IIf([Field1]='AA' And IsNumeric([Field2]), [Field2]*1, Null)
If the purpose of [Field2]*1
is to cast the [Field2]
text value as a number, consider the Val() function instead ...
IIf([Field1]='AA' And IsNumeric([Field2]), Val([Field2]), Null)