Solved

Access nested Iff between dates

Posted on 2014-09-25
5
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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