Query Expression that would find the Earliest Date in a Row of Dat

G

Guest

I am working on a query that will select rows of data where each row contains
as many as 35 different dates. I would like to create a new field at the end
of each row of data where the earliest date in the row is returned in the new
field.

Example:
TransRef Instrua Instrub Instruc Instrud Instrue Earliest Date
1785 10/5/06 9/15/06 11/8/06 1/1/07 4/30/07 9/15/06

Any ideas?
 
G

Guest

You need to normalize you table structure to look like this --
TransRef Instru InstruDate
1785 a 10/5/2006
1785 b 9/15/2006
1785 c 11/8/2006
1785 d 1/1/2007
1785 e 4/30/2007
1785 f 9/15/2006
1785 g 9/16/2006
1785 h 9/17/2006
1786 a 9/15/2006
1786 b 11/8/2006
1786 c 1/1/2007
1786 d 4/30/2007
1786 e 9/15/2006
1786 f 9/16/2006

Then a query can find the earilest date but you should not store it but just
run the query each time.
 
J

John Spencer

In a query I could do it with up to 29 arguments, but with 35 arguments any
function that I know of would fail in a query.

And that many fields is too probably too many to use in a normalizing query.

A workaround that could do it.

Paste the following into a module and use it twice in the query in a
calculated field, that would look something like the following.

fgetmaxnumber(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,,FgetmaxNumber(20,21,22,23,24,25,26,27,28,29,30,31,32,33,34))

'------------- Code Starts --------------
Public Function fGetMaxNumber(ParamArray Values()) As Variant
'John Spencer UMBC CHPDM
'Last Update: April 5, 2000
'Returns the Maximum Number of a group of values passed to it.
'Sample call: myMax = GetMaxNumber("-21","TEST","2", "3",4,5,6,"7",0)
returns 7
'Ignores values that cannot be treated as numbers.

Dim i As Integer, vMax As Variant, tfFound As Boolean, dblCompare As Double

vMax = -1E+308 'very large negative number
For i = LBound(Values) To UBound(Values)
If IsNumeric(Values(i)) Then
dblCompare = CDbl(Values(i))
If dblCompare > vMax Then
vMax = dblCompare
tfFound = True
End If
End If
Next

If tfFound Then
fGetMaxNumber = vMax
Else
fGetMaxNumber = Null
End If

End Function

Dale Fye's Version
Public Function MaxVal(ParamArray MyArray()) As Variant

Dim varMax As Variant
Dim intLoop As Integer
varMax = Null

For intLoop = LBound(MyArray) To UBound(MyArray)
If IsNull(MyArray(intLoop)) Then
'do nothing
ElseIf IsNull(varMax) Or MyArray(intLoop) > varMax Then
varMax = MyArray(intLoop)
End If
Next
MaxVal = varMax

End Function
 
G

Guest

John,
Where do I place the field names in this module setup? You can tell that
I'm new at this.
Thanks
 

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