Solved

Having difficulties with a Calculated field using IIF

Posted on 2008-06-23
8
1,052 Views
Last Modified: 2010-05-18
REF is a Text field in Table1
In Query1 - Calculated field --- > Test_Field: IIf([REF]="K","other",IIf(CInt([REF])=1,"One",IIf(CInt([REF])=7,"Seven",IIf(CInt([REF])=12,"Twelve",IIf(CInt([REF])=5,"Half","other")))))

It was just by chance that i saw K, as if It encounters another letter - which should then return False and therefore "other"  but actually returns #error !!!
0
Comment
Question by:7601105166084
  • 4
  • 2
  • 2
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 21847605
>if It encounters another letter - which should then return False
You don't have that logic anywhere.  That would be IsNumeric(Nz([Ref], "")
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 21847640
You can simplify that expression by using the Switch function, instead of all the embedded IIF's. Check Help for the syntax.

mx
0
 

Author Comment

by:7601105166084
ID: 21847809
jimhorn I dont understand that logic :  you are saying if [REF] is null then "blank" else is [ref] numeric ?

I would rather it be :
Test_Field: IIf(CInt([REF])=1,"One",IIf(CInt([REF])=7,"Seven",IIf(CInt([REF])=12,"Twelve",IIf([REF]="K "other",)))))

So IIF .... and then.... IIF[REF] is any other value or letter then it must show "other"

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 21847857
NULL is the absense of a value, and you have to trap that in any expression.  This is called 'null handling'.
In your expression, you also want to trap any letter value other than "K"

IsNumeric(Nz([Ref], "") will trap both NULL values, and any non-numbers.

Give this a whirl (air code, so you may have to modify)...
Test_Field: IIf(Nz([REF], "")="K","other",iif(IsNumeric(Nz([Ref], "")), False,  (IIf(CInt([REF])=1,"One",IIf(CInt([REF])=7,"Seven",IIf(CInt([REF])=12,"Twelve",IIf(CInt([REF])=5,"Half","other"))))))
0
 
LVL 75
ID: 21847880
'air code' jHo, lol.  Is that the same as 'vaporware' ?

Seriously, in addition to what jHo is showing you, check out the Switch function. It will make the expression waaaay more readable and easier to check the logic.

mx
0
 

Author Comment

by:7601105166084
ID: 21854286
a)syntax... ? Ok - how can I combine what jHo is saying with this :  Expr1:Switch([REF]=1,"One", [REF] =2,"two")  ?
and does Switch have an If..Then.. ?ElseIF? - If i can accomplish this with a Case statement in a function - I would rather do that - thats easier to read. but please see (b)

b)  Because the Field is a TEXT field and it has numbers and letters - is this the right or the only way??  by using Cint([REF]=1.. unfortunately I came across #errors and the only way around, which worked was to use Cint.

c)I have a similarish.. problem at the moment with excel - if i copy and paste to excel - will excel know the difference?

Thanks
0
 

Author Comment

by:7601105166084
ID: 21855304
The following is now working:

Refs: IIf(Val([REF])=1,"One",IIf(Val([REF])=7,"seven",IIf(Val([REF])=12,"dd",IIf(Val([REF])=5,"wedf",IIf(IsNumeric(Nz([REF])),"","other")))))

Except the NULL part (shows #Error  where there is a null value) Thanks jHo for enlightening me there - except that that part is now not working )-:



IsNumeric(Nz([Ref], "")
0
 

Author Comment

by:7601105166084
ID: 21855487
I got this working by using the following:

Refs: IIf(IsNull([REF]),"NULL",IIf(Val([REF])=1,"One",IIf(Val([REF])=7,"Seven",IIf(Val([REF])=2,"Two","other"))))

I'm goin to share points between you 2,  as now I understand the concept of the SWITCH function ..

Expr1: Switch(IsNull([REF]),"NULLl",IsNumeric([ATT_SRC_CD]),"NUMERIC")

and also always to check for NULL  values,

thanks guys!

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question