Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the largest value of a few columns? 1

Status
Not open for further replies.

lihuitsai

Technical User
Apr 25, 2005
1
US
I am trying to get the maximum out a few fields in the same table. For example: I have a table with three fields: D1, D2, and D3. I wanted to write a query that produce the Maximum [D1, D2, and D3] and assign it to D4. The built-in Max function does not work because it produces the largest value of one field over all records. I am trying to get the largest of a few fields from the same record.
Thanks,
LiHui
 
Create the following function in a standard code module:
Code:
'A generic function to get the max value of an arbirtrary numbers of same type values:
Public Function myMax(ParamArray Args())
Dim i As Long, rv
rv = Args(LBound(Args))
For i = 1 + LBound(Args) To UBound(Args)
  If IsNull(rv) Or rv < Args(i) Then rv = Args(i)
Next
myMax = rv
End Function
And now in the query grid:
D4: myMax([D1],[D2],[D3])

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top