I think you can do this thing in VBA code instead of in a query with function.
or
modify the maximum function to use sample id as the parameter, and look for the max field:
Code:
Public Function MaxField(pKeyValue As Variant) As Double
Const tblName = "mytable"
Const pKey = "sample"
Const FieldList = "f2,f3,f5"
Dim rs As DAO.Recordset
Dim keystr As String
Dim i As Long
If VarType(pKeyValue) = vbString Then
keystr = pKey & "='" & pKeyValue & "'"
Else
keystr = pKey & "=" & pKeyValue
End If
Set rs = CurrentDb.OpenRecordset("select " & FieldList & " from " & tblName & " where " & keystr)
If Not rs.EOF Then
MaxField = -100000000#
For i = rs.Fields.Count - 1 To 0 Step -1
If MaxField < rs.Fields(i).Value Then MaxField = rs.Fields(i).Value
Next
Else
MaxField = Null
End If
End Function