Solved

Having difficulties with a Calculated field using IIF

Posted on 2008-06-23
8
1,045 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 - Access MVP) 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

809 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