473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with nested IIF Statement using DateADD.

14
Hello All,

Recently I was asked to make some changes to some business logic for a report generator I created for our office. Basically what I'm trying to do is this:

1)Evaluate a Date Field
2)If it's populated, leave it alone
3)If it's null, Evaluate a Shipping port field
4)If it's a California port add 50 days
5)If it's a NJ port add 70 days
6)If it's anything else add 60 days.

Can someone help me with the format please? Here is what I'm thinking of using for this, but I'm not sure if it will work. I also would like a "trap all" condition for anything that might sneak in there other then the 3 I know for sure can occur.

Thanks to any and all who can help me out with this, I sincerely appreciate it.

Expand|Select|Wrap|Line Numbers
  1. zdatecalc: IIf(Eval(IsNull([ETA Date])),IIf([portcalc]="Long Beach, CA",DateAdd("d",50,[Prod Date]),IIf([portcalc]="Newark, NJ",DateAdd("d",70,[Prod Date]),IIf([portcalc]="TBD",DateAdd("d",60,[Prod Date]),[ETA Date]))))
Oct 23 '13

✓ answered by metronj

Here was the solution:

1)Use an Update query to change the ETA Data Type to Text.

ALTER TABLE Table1 ALTER COLUMN [ETA Date] TEXT


2 Use an Update query with a IIF on the "update to:" line to determine whether or not the ETA date field is NULL. If it is NULL perform the function, if not just redisplay whatever date is in there.

Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([Table1]![ETA Date]),fCalcDate([ETA Date],[Port],[Prod Date]),[Table1]![ETA Date])
3)Here is the function with some tweaks:

Expand|Select|Wrap|Line Numbers
  1. Public Function fCalcDate(ETA_Date As Variant, Port As String, Prod_Date As Variant) As Date
  2.  
  3.     Select Case Port
  4.       Case "Long Beach, CA"
  5.         fCalcDate = DateAdd("d", 51, Prod_Date)      'California
  6.       Case "Newark, NJ"
  7.         fCalcDate = DateAdd("d", 72, Prod_Date)      'New Jersey
  8.       Case Else
  9.         fCalcDate = DateAdd("d", 51, Prod_Date)      'All Other States
  10.     End Select
  11.     End Function
  12.  
  13.  
4) Use an Update Query to change the ETA Date Datatype back to a Date format.

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE Table1 ALTER COLUMN [ETA Date] DATE
The reason I went this way is because Access treats a NULL date as 0.0 (12\30\1899) so after investigating that for a few hours I came up with the conclusion that the easiest way to deal with it was to just do the check and calculations on the field as straight text and then just format it back to date. It turns out that situation is pretty tricky to deal with and this was the easiest way (for me) I could come up with, and it works like a charm.

Hope this helps someone someday.


Thanks guys!

6 2817
ADezii
8,834 Expert 8TB
  1. Create an UPDATE Query that will calculate ETA Date based on whether or not it is NULL. If already has a Value, leave it alone. When it doesn't have a Value, calculate the ETA Date based on the Shipping Port.
  2. SQL for the UPDATE Query:
    Expand|Select|Wrap|Line Numbers
    1. UPDATE Table1 SET Table1.[ETA Date] = fCalcDate([ETA Date],[PortCall]);
  3. Function definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcDate(varDate As Variant, strPort As String) As Date
    2. If IsNull(varDate) Then Exit Function
    3.  
    4. Select Case strPort
    5.   Case "CA"
    6.     fCalcDate = DateAdd("d", 50, Date)      'California
    7.   Case "NJ"
    8.     fCalcDate = DateAdd("d", 70, Date)      'New Jersey
    9.   Case Else
    10.     fCalcDate = DateAdd("d", 60, Date)      'All Other States
    11. End Select
    12. End Function
  4. The above Logic is based on the assumption that the Shipping Port cannot be NULL (Required = Yes).
Oct 24 '13
metronj
14
Hi ADezii,

Thanks for your reply. I tried that and it set all of my dates to 12/30/1899 lol. I guess you are on the right track but something needs tweaking :)

Let me try to clear up the exact field names I'm using:
[ETA Date] = the date I'm trying to modify.
[Prod Date] = the date I have for sure. This is the date I want to add 50 or 70 days to if [ETA Date] is null and then put that calculated date into the [ETA Date] field.
[Port] = The field I need to inspect to determine how many days to add to the [Prod Date] if [ETA Date] is Null. Port names can be Long Beach, CA \ Newark, NJ \ TBD (to be determined) \ All others.

The logic is: If the [ETA Date] hasn't been given to us from the factory, we want to look at the [Prod Date] and add the appropriate number of days to it so our dealers have a rough estimate of when new equipment will arrive for them to sell to the end user. [Prod Date] however must not be touched as it is a reference field, the resultant date from the calculation must be put into [ETA Date]. Consequently if we have the [ETA Date] then it must be left alone as it is a much more accurate reflection of when product will actually arrive instead of our guesstimated date.

The reason we do this is because if we don't show our dealers SOMETHING they call all day long wondering when product will arrive. :)

Thanks a bunch for your help!
Oct 24 '13
ADezii
8,834 Expert 8TB
  1. You now need to pass the Production Date to the Function in order to Recalculate the ETA Date if it is NULL.
  2. SQL Statement:
    Expand|Select|Wrap|Line Numbers
    1. UPDATE Table1 SET Table1.[ETA Date] = fCalcDate([ETA Date],[PortCall],[Prod Date]);
  3. New Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcDate(varDate As Variant, strPort As String, dteProdDate As Date) As Date
    2. If IsNull(varDate) Then Exit Function
    3.  
    4. Select Case strPort
    5.   Case "CA"
    6.     fCalcDate = DateAdd("d", 50, dteProdDate)      'California
    7.   Case "NJ"
    8.     fCalcDate = DateAdd("d", 70, dteProdDate)      'New Jersey
    9.   Case Else
    10.     fCalcDate = DateAdd("d", 60, dteProdDate)      'All Other States
    11. End Select
    12. End Function
Oct 24 '13
metronj
14
Hi ADezii,

Same thing is happening. Here are some screenshots and the code I'm trying:



Expand|Select|Wrap|Line Numbers
  1. UPDATE Table1 SET Table1.[ETA Date] = fCalcDate([ETA Date],[Port],[Prod Date]);
Expand|Select|Wrap|Line Numbers
  1.  Public Function fCalcDate(varDate As Variant, strPort As String, dteProd_Date As Date) As Date
  2.     If IsNull(varDate) Then Exit Function
  3.  
  4.     Select Case strPort
  5.       Case "Long Beach, CA"
  6.         fCalcDate = DateAdd("d", 50, dteProd_Date)      'California
  7.       Case "Newark, NJ"
  8.         fCalcDate = DateAdd("d", 70, dteProd_Date)      'New Jersey
  9.       Case Else
  10.         fCalcDate = DateAdd("d", 60, dteProd_Date)      'All Other States
  11.     End Select
  12.     End Function
  13.  
  14.  
  15.  

Thanks Again!
Oct 24 '13
ADezii
8,834 Expert 8TB
Are both Date Fields defined as DATE?TIME Data Types?
Oct 25 '13
metronj
14
Here was the solution:

1)Use an Update query to change the ETA Data Type to Text.

ALTER TABLE Table1 ALTER COLUMN [ETA Date] TEXT


2 Use an Update query with a IIF on the "update to:" line to determine whether or not the ETA date field is NULL. If it is NULL perform the function, if not just redisplay whatever date is in there.

Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([Table1]![ETA Date]),fCalcDate([ETA Date],[Port],[Prod Date]),[Table1]![ETA Date])
3)Here is the function with some tweaks:

Expand|Select|Wrap|Line Numbers
  1. Public Function fCalcDate(ETA_Date As Variant, Port As String, Prod_Date As Variant) As Date
  2.  
  3.     Select Case Port
  4.       Case "Long Beach, CA"
  5.         fCalcDate = DateAdd("d", 51, Prod_Date)      'California
  6.       Case "Newark, NJ"
  7.         fCalcDate = DateAdd("d", 72, Prod_Date)      'New Jersey
  8.       Case Else
  9.         fCalcDate = DateAdd("d", 51, Prod_Date)      'All Other States
  10.     End Select
  11.     End Function
  12.  
  13.  
4) Use an Update Query to change the ETA Date Datatype back to a Date format.

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE Table1 ALTER COLUMN [ETA Date] DATE
The reason I went this way is because Access treats a NULL date as 0.0 (12\30\1899) so after investigating that for a few hours I came up with the conclusion that the easiest way to deal with it was to just do the check and calculations on the field as straight text and then just format it back to date. It turns out that situation is pretty tricky to deal with and this was the easiest way (for me) I could come up with, and it works like a charm.

Hope this helps someone someday.


Thanks guys!
Oct 25 '13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
2
by: Gadrin77 | last post by:
as a newbie to XSL, is it possible to mimic a SELECT/CASE statement using XSL? I tried a quickie and I kept getting errors either using PARAM or WITH-PARAM in the wrong place or VARIABLE. I...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
3
by: rkc | last post by:
Hi, I need to design the following pattern using just the 'for' and the 'if, else' statement. Please provide some help.. thanks in advance: ********** !********* !!******** !!!*******...
0
by: eonblue20 | last post by:
Hello I\\\'m new to DB2 and I\\\'ve been trying to create the MERGE statement using PreparedStatements in Java as a requirement for a project I\\\'m working on, but I get the following message: ...
1
by: sds50 | last post by:
Error with Select Statement using the SQLDataAdapter Wizard -------------------------------------------------------------------------------- I am needing to setup the data adapter for my SQL...
3
by: mutaher | last post by:
geography(name, region, area, population, gdp) Table Name = Geography Column Name = Name, Region, Area, Population, GDP Help me execute this statement List the name and region of countries...
2
by: lyle777 | last post by:
I need help with using continue in my loop. they say that when using continue in the loop that is nested, depending on the condition of the continue statement, execution should jump to the...
3
by: John Crozier | last post by:
Hello everyone, I was recently tasked to convert some of the functions employees use Excel for into Access. One of these functions is seeing which invoices are due to be paid this month. Surely...
2
by: Jazz Balmain | last post by:
I'm trying to write a query that says "If = "Probationary Review 1" then add 14 days to ; "If = "Probationary Review 2" then add 3 months to ; "If = "Probationary Review 3" then add 5 months to ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.