Solved

Access nested Iff between dates

Posted on 2014-09-25
5
221 Views
Last Modified: 2014-09-25
Hi,

I would like a query in Access that does the following logic:

If ([CTFImport.DOB] >=#01/09/2014#) And [CTFImport.DOB]<=#31/08/2015#) then -5
else If ([CTFImport.DOB] >=#01/09/2013#) And [CTFImport.DOB]<=#31/08/2014#) then -4
else If ([CTFImport.DOB] >=#01/09/2012#) And [CTFImport.DOB]<=#31/08/2013#) then -3
else If ([CTFImport.DOB] >=#01/09/2011#) And [CTFImport.DOB]<=#31/08/2012#) then -2
else If ([CTFImport.DOB] >=#01/09/2010#) And [CTFImport.DOB]<=#31/08/2011#) then -1
else CTFImport.NCyearActual

I can do this in Crystal Reports but I am struggling in Access.. can you help please?

Thanks,

Tom
0
Comment
Question by:tom_optimum
5 Comments
 
LVL 10

Accepted Solution

by:
JEaston earned 500 total points
ID: 40343817
I think you will need a series of IIF statement which use the Between comparator.

For example:
IIF([CTFImport.DOB] Between #01/09/2014# AND #31/08/2015#,-5, IIF([CTFImport.DOB] Between #01/09/2013# AND #31/08/2014#,-4, IIF([CTFImport.DOB] Between #01/09/2012# AND #31/08/2013#,-3, IIF([CTFImport.DOB] Between #01/09/2011# AND #31/08/2012#,-2, IIF([CTFImport.DOB] Between #01/09/2010# AND #31/08/2011#,-1,CTFImport.NCyearActual)))))

Alternately you could write your own function is VBA and use that.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40343821
try:

SELECT
    Switch(
        ([CTFImport.DOB] >=#01/09/2014#) And [CTFImport.DOB]<=#31/08/2015#), -5,
        ([CTFImport.DOB] >=#01/09/2013#) And [CTFImport.DOB]<=#31/08/2014#) , -4,
        ([CTFImport.DOB] >=#01/09/2012#) And [CTFImport.DOB]<=#31/08/2013#), -3,
		([CTFImport.DOB] >=#01/09/2011#) And [CTFImport.DOB]<=#31/08/2012#), -2,
        ([CTFImport.DOB] >=#01/09/2010#) And [CTFImport.DOB]<=#31/08/2011#) , -1
        ) AS result
from tabel_name

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40343833
IIf([CTFImport.DOB] >=#01/09/2014# And [CTFImport.DOB]<=#31/08/2015#,-5,
IIf([CTFImport.DOB] >=#01/09/2013# And [CTFImport.DOB]<=#31/08/2014#,-4,
IIf([CTFImport.DOB] >=#01/09/2012# And [CTFImport.DOB]<=#31/08/2013#,-3,
IIf([CTFImport.DOB] >=#01/09/2011# And [CTFImport.DOB]<=#31/08/2012#,-2,
IIf([CTFImport.DOB] >=#01/09/2010# And [CTFImport.DOB]<=#31/08/2011#,-1,
CTFImport.NCyearActual)))))

Open in new window

0
 

Author Closing Comment

by:tom_optimum
ID: 40343863
This worked perfect - thank you.

I didn't try the others - I thought it was fair to start with the first suggested one and work down.. but the first one worked great.

Thanks everyone for looking at this for me.

Cheers

Tom
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 40343865
WIth the addition of one test, you can eliminate 5 tests from the formula.

Perhaps better from a performance standpoint.

IIF( [CTFImport.DOB]>=#01/09/2015#,CTFImport.NCyearActual,
IIf([CTFImport.DOB] >=#01/09/2014# ,-5,
IIf([CTFImport.DOB] >=#01/09/2013# ,-4,
IIf([CTFImport.DOB] >=#01/09/2012# ,-3,
IIf([CTFImport.DOB] >=#01/09/2011# ,-2,
IIf([CTFImport.DOB] >=#01/09/2010# ,-1,
 CTFImport.NCyearActual))))))

Open in new window


Since by the fieldname these are probably dates of birth the first test could be eliminated since there shouldn't be any in the data that are greater than 1 Sep 2015.

mlmcc
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

856 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