TehraniCHS said:
I have a table with records of students with their grades in various
assignments. I want to do a query to find the lowest grade in each record
based on the 4 fields (grade1, grade2, grade3, grade4). I know how to use
MIN function in EXCEL and do this, but I want to know how to do it
in ACCESS.
TehraniCHS,
Sample Table:
CREATE TABLE DenormalizedGrades
(GradeID AUTOINCREMENT
,StudentID LONG
,Grade1 DOUBLE
,Grade2 DOUBLE
,Grade3 DOUBLE
,Grade4 DOUBLE
,CONSTRAINT pk_DenormalizedGrades PRIMARY KEY (GradeID)
)
Sample Data
StudentID Grade1 Grade2 Grade3 Grade4
1 4.0 4.0 4.0 4.0
2 0.0 0.0 0.0 0.0
3 4.0 3.0 2.0 1.0
4 3.0 1.0 3.0 3.0
5 1.0 2.0 3.0 4.0
6 3.0 2.5 2.1 2.7
Note: There is no Foreign Key for StudentID because I skipped making
that table.
Query:
SELECT StudentID
,MultipleColumns_LowestGrade(T1.StudentID, T1.Grade1, T1.Grade2,
T1.Grade3, T1.Grade4) AS MinGrade
FROM DenormalizedGrades AS T1
The following VBA code:
Public Function MultipleColumns_LowestGrade _
(StudentID As Long _
, Grade1 As Double _
, grade2 As Double _
, grade3 As Double _
, grade4 As Double) As Double
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
Dim dblMinimumGrade As Double
Dim bytOuterLoop As Byte
Dim bytInnerLoop As Byte
bytOuterLoop = 0
bytInnerLoop = 0
dblMinimumGrade = 0
strSQL = strSQL & "SELECT * FROM DenormalizedGrades AS DG1 "
strSQL = strSQL & "WHERE DG1.StudentID = " & StudentID & ";"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
rs.MoveFirst
With rs
dblMinimumGrade = .Fields.Item(2).Value
For bytOuterLoop = 2 To 5
For bytInnerLoop = 2 To 5
If (.Fields.Item(bytOuterLoop).Value <=
..Fields.Item(bytInnerLoop).Value) And _
(.Fields.Item(bytOuterLoop).Value <= dblMinimumGrade) Then
dblMinimumGrade = .Fields.Item(bytOuterLoop).Value
End If
Next
Next
End With
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
MultipleColumns_LowestGrade = dblMinimumGrade
End Function
Results of Query:
StudentID MinGrade
1, 4
2, 0
3, 1
4, 1
5, 1
6, 2.1
Note: The above VBA code freezes the table. Any change to the Table
breaks this VBA code (badly). Any change to the VBA Code breaks the
Query. This defeats the entire purpose of SQL & Relational Databases.
The above is a stopgap useable to solve the current problem, and allow
time to successful normalize the database.
Note: This was tested on 6 rows for about ten minutes. Please
investigate further.
Sincerely,
Chris O.