Question : Question on Query Grouping

Question: tblMain is used in a query.
I want to be able to count records of some GroupID together and I am having some problems with it. I strated with the SQL below but cant figure it out.

For example, I want a query that will count all records belonging to GroupID ASL, STU WHERE
course = "MTH" aka. Math.

In the same token, I want to be able to count all records of GroupID LT, STU WHERE Course = "GYM"

Also, I want to count all group records of STU WHERE theire courses are "PHY" and "CHM"

I started with a query that produced result in "QueryRaw" but the result I actually wanted should be the one
with QueryResult (See All.doc - attached word file)

I have also attacheed a small db that show the query raw in the sample. Any assistance that will help me get the result as shown in Query_Result of All.doc will be appreciated.

Answer : Question on Query Grouping

Sorry again about that one. I hope this set should do it. Here's the updated function in your module:

Public Function GetGroupIDName(ByVal courseGroup As String) As String
    Dim db As Database
    Dim rs As Recordset
    Dim sql As String
   
    sql = "SELECT IIf([Course] In (""Physics"",""Chemistry""),""Physics, Chemistry"",[Course]) AS CourseGroup, tblMain.GroupID, Count(tblMain.RID) AS CountOfRID " & _
    " FROM tblMain INNER JOIN tblCourse ON tblMain.CourseID = tblCourse.CourseID " & _
    " WHERE IIf([Course] In (""Physics"",""Chemistry""),""Physics, Chemistry"",[Course])=""" & Replace(courseGroup, """", """""") & """" & _
    " GROUP BY IIf([Course] In (""Physics"",""Chemistry""),""Physics, Chemistry"",[Course]), tblMain.GroupID;"

   
    Set db = CurrentDb()
    Set rs = db.OpenRecordset(sql)
   
    Dim i As Integer
    Dim returnString As String
    returnString = ""
    If rs.RecordCount > 0 Then
        rs.MoveFirst
        For i = 0 To rs.RecordCount - 1
            If i <> rs.RecordCount - 1 Then
                returnString = returnString & rs!GroupID & ", "
            Else
                returnString = returnString & rs!GroupID
            End If
            rs.MoveNext
        Next i
    End If
       
    GetGroupIDName = returnString
End Function


And here's the updated query:

SELECT GetGroupIDName(IIf([Course] In ("Physics","Chemistry"),"Physics, Chemistry",[Course])) AS GroupID, IIf([Course] In ("Physics","Chemistry"),"Physics, Chemistry",[Course]) AS CourseGroup, Count(tblMain.RID) AS CountOfRID
FROM tblMain INNER JOIN tblCourse ON tblMain.CourseID = tblCourse.CourseID
WHERE (((tblMain.Appdate)>=[forms]![frmReportDateRange]![BeginDate] And (tblMain.Appdate)<=[forms]![frmReportDateRange]![EndDate]))
AND Not NZ(tblMain.CCon,false)
GROUP BY GetGroupIDName(IIf([Course] In ("Physics","Chemistry"),"Physics, Chemistry",[Course])), IIf([Course] In ("Physics","Chemistry"),"Physics, Chemistry",[Course])
ORDER BY Last(tblCourse.CourseID);
Random Solutions  
 
programming4us programming4us