MS Access 2007/2010: Need to set Date field based on most recent of 5 other Date fields

StackOverflow https://stackoverflow.com/questions/22161488

  •  19-10-2022
  •  | 
  •  

Question

I seem to have a problem I haven't encountered before within the same record. I need to set a Date field value based on the most recent date of 5 other Date fields. I would like to do this at the query level so that I can assign the most recent date and store it in a field, but I'm not sure if it's possible. If I have to do it within a Form, then I'm not sure how to write out the Case statement. Any help is appreciated!

What I'm trying to do now is write an expression that pretty much goes as follows:

ESA Exp Date: IIf([1-Record Search Date]>[2-Site Reconnaissance Date] And [1-Record Search Date]>[3-Owner Interview Date] And [1-Record Search Date]>[4-Lien/AUL Search Date] And [1-Record Search Date]>[5-User Questionnaire Date],[1-Record Search Date],IIf([2-Site Reconnaissance Date]>[1-Record Search Date] 

And so on... However, expressions have a limit of characters, and so someone said in an unrelated question to write a function. I'm not sure how that would work in my case though.

Thanks again Rob

No correct solution

OTHER TIPS

If changing your table structure isn't an option then you can use a function like the one created by Allen Browne http://allenbrowne.com/func-09.html, there are fuller instructions on the webpage.

Call it in a query as: (may I suggest not putting spaces in whenever not required, will help further down the line in VBA)

ESAExpDate: MaxOfList([1-Record Search Date],_
[2-Site Reconnaissance Date],[3-Owner Interview Date],[4-Lien/AUL Search Date], _
[5-User Questionnaire Date])

I have pasted below in case the link dies.

Function MaxOfList(ParamArray varValues()) As Variant
    Dim i As Integer        'Loop controller.
    Dim varMax As Variant   'Largest value found so far.

    varMax = Null           'Initialize to null

    For i = LBound(varValues) To UBound(varValues)
        If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
            If varMax >= varValues(i) Then
                'do nothing
            Else
                varMax = varValues(i)
            End If
        End If
    Next

    MaxOfList = varMax
End Function

The reason this seems so difficult is because your table structure is not optimal for relational querying. Let's say your current table (date_values) has the columns d1 to d5, all dates. You need to normalise the table like so (note that this doesn't change your actual table, it just 'repackages' it in memory into an easier-to-query form):

select entity_id, 'type 1' as date_type, d1 as date_value
from date_values
where d1 is not null
union all
select entity_id, 'type 2' as date_type, d2 as date_value
from date_values
where d2 is not null
union all
select entity_id, 'type 3' as date_type, d3 as date_value
from date_values
where d3 is not null
union all
select entity_id, 'type 4' as date_type, d4 as date_value
from date_values
where d4 is not null
union all
select entity_id, 'type 5' as date_type, d5 as date_value
from date_values
where d5 is not null

Note that entity_id is any field which uniquely identifies a row in your table. Once you have data in a normalised form, querying is easy:*

update date_values as d
inner join (
  select ssq.entity_id, max(ssq.date_value) as max_date
  from (
    ... the query I show above ...
  ) as ssq
  group by ssq.entity_id
) as sq
on d.entity_id = sq.entity_id
set d.[ESA Exp Date] = sq.max_date
where entity_id = [... the entity you wish to update ...]

The last clause above, the where clause, restricts the modification to a single row you can specify. You can enter it manually when prompted by Access or you can have it refer to a field in a form. There are many options.

* OK, easi_er_.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top