ashwin ashwin - 1 month ago 6
SQL Question

Adding a new field depending on the condition in MS Access

I have five fields in a Query named Status. Lets consider the names of the fields for an easy demonstration as A,B,C,D,E. The Status Query looks something like this

A B C D E
1 a d
2 n o
3 t z
4 r m d
q f j


I am looking to add a field to the above Query named Description which looks through each record and adds a description. For example if the field A is empty then in the description it must show Failed A. if the field B is empty then it must show Failed B and in the similar way Failed C, Failed D and Failed E. There may also be multiple Fields which are empty for a record. But it must describe which field is empty first for that record. It must show something like this :

A B C D E Description
1 a d Failed C
2 n o Failed B
3 t z Failed D
4 r m d Failed E
q f j Failed A


I have tried to use IIF function in the following way

IIF(A is null,Failed A,IIF(B is null),Failed B, IIF(C is null), Failed C,IIF(D is null), Failed D,Failed E)


It doesn't work in this way. How Can I achieve the above? Or where am I going wrong in the code?

Answer

I guess you need this:

"Failed " & IIF(A is null,"A",IIF(B is null),"B", IIF(C is null), "C",IIF(D is null), "D","E")
Comments