Query finding minimum date

A

Alison

Hi, I have a database of projects with four different date columns (DOB,
First LWS, First SLDWS, and First SA). Some of these fields are blank.
I want to run a query that will give me the earliest date from these four
columns, but I don't know how.
Can anyone help me?
Very much appreciated!
Alison
 
C

Clifford Bass

Hi Alison,

Some time ago I wrote a function to do this. You will need to create a
regular (not class, not form, not report) module in your database. Copy and
paste this code into it:

=========================================

Public Function MinimumAny(ParamArray var() As Variant) As Variant

' Minimum function that accepts any number of parameters.

' Parameters: Any number of items accepted
' Returns: The mimimum of all values, if there are any non-null values,
' otherwise Null

' Author: Clifford Bass

Dim i As Integer
Dim j As Integer
Dim varMinimum As Variant
Dim varSubMinimum As Variant

On Error GoTo MinimumAny_Error

varMinimum = Null
For i = LBound(var()) To UBound(var()) Step 1
If Not IsNull(var(i)) Then
If IsArray(var(i)) Then
For j = LBound(var(i)) To UBound(var(i)) Step 1
varSubMinimum = MinimumAny(var(i)(j))
If Not IsNull(varSubMinimum) Then
If IsNull(varMinimum) Then
varMinimum = varSubMinimum
Else
If varSubMinimum < varMinimum Then
varMinimum = varSubMinimum
End If
End If
End If
Next j
Else
If IsNull(varMinimum) Then
varMinimum = var(i)
Else
If var(i) < varMinimum Then
varMinimum = var(i)
End If
End If
End If
End If
Next i
MinimumAny = varMinimum

MinimumAny_Exit:
Exit Function

MinimumAny_Error:
MinimumAny = strError
Resume MinimumAny_Exit

End Function

=========================================

To use it in a query you will enter something like this in the top row
where you normally place the field names:

MinimumDate: MinimumAny([DOB], [First LWS], [First SLDWS], [First SA])

When used in a query you can enter up to 29 (or maybe 30) fields. You
can also use it from VBA code. In which case you can have many more
parameters (fields or values), including arrays of items. It will work with
any kind of data, but you may get strange results if you mix types of data.

Hope that helps,

Clifford Bass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top