Avatar of tessmarin
tessmarin
 asked on

MS Access Query - Max of Three Date Field and Null Values

My head hurts from trying to figure this one out so thought I would ask the crew out here.  
I have a query with three date fields

Estimated_Start_Date
Revised_Start_Date
Estimated_Comp_Date

In a query format I need to add a new expression field named START which grabs the most current date of these three fields and these fields can and do have some null values.
Microsoft Access

Avatar of undefined
Last Comment
tessmarin

8/22/2022 - Mon
stevbe

StartDate: IIF(NZ([Estimated_Start_Date],0)>=NZ([Revised_Start_Date],0),IIF(NZ([Estimated_Start_Date],0)>=NZ([Estimated_Comp_Date],0), NZ([Estimated_Start_Date], 0, NZ([Estimated_Comp_Date], 0)

Steve
tessmarin

ASKER
Getting a bracketing error trying to locate it but if you find it sooner great
Paulo Pimenta

I don't know if this is what you want but here goes:
(see picture attached)
select
  case
    when Estimated_Start_Date >= Revised_Start_Date and Estimated_Start_Date >= Estimated_Comp_Date then Estimated_Start_Date
    when Revised_Start_Date >= Estimated_Start_Date and Revised_Start_Date >= Estimated_Comp_Date then Revised_Start_Date
    else Estimated_Comp_Date
  end as START,
  Estimated_Start_Date,
  Revised_Start_Date,
  Estimated_Comp_Date
from DATES

Open in new window

start-date.jpg
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
RgGray3

you could create a function (see attached) in a standard module
Simply NZ([EachFieldName])  in your query when calling the function

Try the following in the immediate window
?LargestOf3Dates NZ(Date()),NZ(Date()+2),NZ(Date()-2))

or

?LargestOf3Dates NZ(Date()),NZ(Null),NZ(Date()-2))

in your case you would have a field in your query

MaxDate:?LargestOf3Dates NZ([Estimated_Start_Date]),NZ([Revised_Start_Date]),NZ([Estimated_Comp_Date]))




Option Compare Database
 
Public Function LargestOf3Dates(Optional dat1 As Date, Optional dat2 As Date, Optional dat3 As Date) As Date
Dim datMaxDate As Date
 
 
    If IsDate(dat1) Then
        datMaxDate = dat1
    End If
    
    If IsDate(dat2) Then
        If IsNull(datMaxDate) Then
            datMaxDate = dat2
        Else
            If dat2 > datMaxDate Then
                datMaxDate = dat2
            End If
        End If
    End If
    
    If IsDate(dat3) Then
        If IsNull(datMaxDate) Then
            datMaxDate = dat3
        Else
            If dat3 > datMaxDate Then
                datMaxDate = dat3
            End If
        End If
    End If
    
    If IsNull(datMaxDate) Then
        'All three dates were null
        'Determine what you want to return
    Else
        LargestOf3Dates = datMaxDate
    End If
    
End Function

Open in new window

tessmarin

ASKER
Id rather steer clear of code on this one if possible.  
Paulo Pimenta

No code here.... Plain and simple SQL.
select
  case
    when Estimated_Start_Date >= Revised_Start_Date and Estimated_Start_Date >= Estimated_Comp_Date then Estimated_Start_Date
    when Revised_Start_Date >= Estimated_Start_Date and Revised_Start_Date >= Estimated_Comp_Date then Revised_Start_Date
    else Estimated_Comp_Date
  end as START,
  Estimated_Start_Date,
  Revised_Start_Date,
  Estimated_Comp_Date
from DATES  -- Change to your table name

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
tessmarin

ASKER
Ok I did this in two expressions for now:

EstStartDate: IIf(NZ([Revised_Start_Date],0),[Revised_Start_Date],[Estimated_Start_Date])
StartDate: IIf(NZ([Actual_Start_Date],0),[Actual_Start_Date],[EstStartDate])

The bottom one is the one I would be relying on to give me the most current start date.  
tessmarin

ASKER
The above can only work because the dates are entered in sequence of more current (well they are supposed to be anyway)  So the estimated date would be entered then revised then last would be the actual start date.  Make sense?

Paul the sql is giving me a syntax error on the when part.
ASKER CERTIFIED SOLUTION
stevbe

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tessmarin

ASKER
ty
Your help has saved me hundreds of hours of internet surfing.
fblack61