Solved

Having difficulties with a Calculated field using IIF

Posted on 2008-06-23
8
1,034 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
Comment Utility
>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 - Access MVP) earned 250 total points
Comment Utility
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
Comment Utility
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
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
'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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now