Use MIN and MAX to Compare Fields in Microsoft Access
Question details
Compare multiple field values in each Access record using calculations similar to Excel's MIN and MAX functions.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A user wants to compare forecast and history values stored in separate fields of the same record.
- Observed behavior
- Access's Min and Max aggregate functions calculate across records rather than comparing several fields within one record.
Identify the table and numeric fields you want to compare, and decide how missing values should be handled. To use a VBA function in a query, enable database content only if you trust the database.
Compare fields in each record with VBA functions
Use public VBA functions when you need the smallest and largest values across several fields in the same record.
The functions below accept any number of arguments, ignore Null and nonnumeric values, and return Null if none of the arguments is numeric. They convert numeric values to Double.
In Access, select Create > Module to open the VBA editor. Save the module with a name such as modFieldExtremes, different from the function names.
Paste this code into the standard module: Public Function GetMin(ParamArray values() As Variant) As Variant GetMin = FieldExtreme(False, values) End Function Public Function GetMax(ParamArray values() As Variant) As Variant GetMax = FieldExtreme(True, values) End Function Private Function FieldExtreme(ByVal findMax As Boolean, ByRef values() As Variant) As Variant Dim v As Variant Dim result As Double Dim found As Boolean For Each v In values If Not IsNull(v) Then If IsNumeric(v) Then If Not found Then result = CDbl(v) found = True ElseIf (findMax And CDbl(v) > result) Or (Not findMax And CDbl(v) < result) Then result = CDbl(v) End If End If End If Next v If found Then FieldExtreme = result Else FieldExtreme = Null End If End Function
Open Create > Query Design, add your table, and enter these expressions in separate Field cells: SmallestValue: GetMin([Field1],[Field2],[Field3]) and LargestValue: GetMax([Field1],[Field2],[Field3]). Replace the field names with your own.
Select Run on the Query Design tab to see the calculated minimum and maximum for each record.

Find minimum or maximum values across records
Use a Totals query when you need an aggregate for one field across records or groups, rather than a comparison between fields in one record.
Compare spreadsheet values with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with Office documents. Use WPS Spreadsheets to compare values with MIN and MAX formulas and move between compatible spreadsheet files. It does not replace Access database queries or VBA functions.

Frequently Asked Questions
Can Access Min and Max compare three fields in the same row?
Access's Min and Max aggregate functions operate across records. For a comparison within one record, add a public VBA function such as GetMin or GetMax to a standard module and call it from a calculated query field.
What happens if one of the fields is Null?
The VBA functions in this article ignore Null values and compare the remaining numeric values. If none of the arguments is numeric, they return Null.
How do I find the highest value for each group?
In Query Design, select Totals, add the grouping field with Group By in its Total row, and set the numeric field's Total row to Max. Select Run to view the results.
Why does my query not recognize the VBA function?
Check that the function is Public, is saved in a standard module with a different name from the function, and has no VBA compile errors. If Access has disabled database content, enable it only when you trust the database.
Should I save the calculated minimum or maximum in my table?
Usually, calculate it in a query, form, or report so it reflects the current source values. In Query Design, add an expression such as LargestValue: GetMax([Field1],[Field2],[Field3]).




