Solved

Access nested Iff between dates

Posted on 2014-09-25
5
214 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

867 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

19 Experts available now in Live!

Get 1:1 Help Now