|
-
Jul 28th, 2006, 02:30 AM
#1
Thread Starter
New Member
Retrieve different fields based on input?
I am writing a query to find costs in the years after a certain delivery date.
I have a table with fields "Delivery Date" and 1998-2005 Yearly Costs.
If the delivery date is after any of the 1998-2005 cost fields (such as a 2003 delivery date), the previous fields are left as 0 (in this case 1998-2003).
I need to write an field in a query that can retrieve costs 1 or 2 or 3 or etc years after the delivery date. So for a 2000 delivery date, the field "1 Year After" would retrieve the 2001 cost field from my table, the field "2 Years After" would retrieve the 2002 cost field from my table, and etc.
The problem is that my delivery dates vary, and I don't know how to add a set number of years to my dates and retrieve the corresponding field from the table.
I am currently using a bulky and inefficient switch statement foe each field:
Code:
Year 1: Switch(Year([Delivery])=1996,0,Year([Delivery])=1997,[Raw Data]![Act 1998 EUR],Year([Delivery])=1998,[Raw Data]![Act 1999 EUR],Year([Delivery])=1999,[Raw Data]![Act 2000 EUR],Year([Delivery])=2000,[Raw Data]![Act 2001 EUR],Year([Delivery])=2001,[Raw Data]![Act 2002 EUR],Year([Delivery])=2002,[Raw Data]![Act 2003 EUR],Year([Delivery])=2003,[Raw Data]![Act 2004 EUR],Year([Delivery])=2004,[Raw Data]![Act 2005 EUR],Year([Delivery])>=2005,0,Year([Delivery])<1996,0)
Year 2:Year 2: Switch(Year([Delivery])=1996,[Raw Data]![Act 1998 EUR],Year([Delivery])=1997,[Raw Data]![Act 1999 EUR],Year([Delivery])=1998,[Raw Data]![Act 2000 EUR],Year([Delivery])=1999,[Raw Data]![Act 2001 EUR],Year([Delivery])=2000,[Raw Data]![Act 2002 EUR],Year([Delivery])=2001,[Raw Data]![Act 2003 EUR],Year([Delivery])=2002,[Raw Data]![Act 2004 EUR],Year([Delivery])=2003,[Raw Data]![Act 2005 EUR],Year([Delivery])=2004,0,Year([Delivery])>=2005,0,Year([Delivery])<1996,0)
Etc up to year 5.
But this system is obviously not very efficient because i have to modify 5 switch statements every time a new year of data is entered into the system.
Is there any way to write a function that can do this for me in VB?
For example, a function in the format:
RetrieveCosts( [column with the delivery dates], # of years to add)
Where the function would return 0 if the column it tries to retrieve does not exist (such as retrieving year 5 costs for a 2005 delivery date).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|