Display earliest date from several fields (1 Viewer)

smally

Registered User.
Local time
Today, 19:30
Joined
Mar 29, 2005
Messages
71
Hi, I need to use a function that works out the earliest date from different fields.

I'm currently using the following code provided from a Microsoft webpage:
Code:
Function Minimum(ParamArray FieldArray() As Variant)
   ' Declare the two local variables.
   Dim i As Integer
   Dim currentVal As Variant

   ' Set the variable currentVal equal to the array of values.
   currentVal = FieldArray(0)

   ' Cycle through each value from the row to find the smallest.
   For i = 0 To UBound(FieldArray)
      If FieldArray(i) < currentVal Then
         currentVal = FieldArray(i)
      End If
   Next i

   ' Return the minimum value found.
   Minimum = currentVal

End Function
The problem I'm having is I need to show the earliest date, but some fields can be null. So if the 1st parameter is null, the result is null even though there is a date in the second parameter.

I need it to show the earliest date even if the 1, 2 or 3 parameters are null, and the result to be null if all parameters are null.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:30
Joined
Feb 28, 2001
Messages
27,323
Code:
   For i = 0 To UBound(FieldArray)
      If not IsNull( FieldArray(i)) Then
          If FieldArray(i) < currentVal Then
             currentVal = FieldArray(i)
          End If
      End If
   Next i

However, this doesn't address the case where all of your fields are null.
That would be on YOU to determine a default value or a method of "escaping" the situation altogether.
 

smally

Registered User.
Local time
Today, 19:30
Joined
Mar 29, 2005
Messages
71
Still not working.
If parameter 1 is null, and 2 is not null, the result is null.
 

static

Registered User.
Local time
Today, 19:30
Joined
Nov 2, 2015
Messages
823
Code:
Function Minimum(ParamArray FieldArray() As Variant)
    Dim v As Variant
    Minimum = Null
    
    ' Cycle through each value from the row to find the smallest.
    For Each v In FieldArray
         If IsNull(Minimum) Then
             Minimum = v
         Else
             If Not IsNull(v) Then
                 If v < Minimum Then
                     Minimum = v
                 End If
             End If
         End If
    Next

End Function
 

Users who are viewing this thread

Top Bottom