Agreed with micron. It looks like your table isn't properly normalized. This would be easier to do and flexable if it were normalized.
Anyway here is a demonstration of how you can use queries in your code:
Code:
Public Function max_test(row_id) As Variant
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim results As Variant
Dim qry As String
qry = "SELECT Exam1, Exam2, Exam3, Exam4 FROM my_table WHERE my_id=" & row_id
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
If Not (rs.BOF And rs.EOF) Then
results = rs!Exam1
If rs!Exam2 > results Then results = rs!Exam2
If rs!Exam3 > results Then results = rs!Exam3
If rs!Exam4 > results Then results = rs!Exam4
Else
results = Null
End If
rs.Close
ExitHandler:
Set rs = Nothing
Set db = Nothing
max_test = results
Exit Function
ErrHandler:
MsgBox "Error #" & Err.Number & ": " & Err.Description, , "max_test() error"
Resume ExitHandler
End Function
Public Sub do_work()
Dim my_max As Variant
my_max = max_test(99)
If Not IsNull(my_max) Then
Debug.Print my_max
End If
End Sub