2

我正在开发一个招生系统。但我收到此错误:You tried to execute a query that does not include the specified expression..

我正在使用以下代码:

Private Function RefreshAdvisoryList()

    Dim vRS As New ADODB.Recordset
    Dim sSQL As String

    'clear list
    txtsection.Clear

    'On Error GoTo ReleaseAndExit

        sSQL = "SELECT tblSection.SectionID, tblSection.SectionTitle, tblAdviser.SchoolYear, tblDepartment.DepartmentTitle, tblYearLevel.YearLevelTitle, tblAdviser.TeacherID" & _
                " FROM (tblDepartment INNER JOIN (tblYearLevel INNER JOIN tblSection ON tblYearLevel.YearLevelID = tblSection.YearLevelID) ON tblDepartment.DepartmentID = tblSection.DepartmentID) INNER JOIN tblAdviser ON (tblSection.SectionID = tblAdviser.SectionID) AND (tblDepartment.DepartmentID = tblAdviser.DepartmentID)" & _
                " GROUP BY tblSection.SectionID, tblSection.SectionTitle, tblAdviser.SchoolYear, tblDepartment.DepartmentTitle, tblYearLevel.YearLevelTitle, tblAdviser.TeacherID" & _
                " HAVING (((tblTeacher.TeacherID)='" & curTeacher.TeacherID & "') AND tblSection.SchoolYear='" & Me.txtSchoolYear.Text & "')" & _
                " ORDER BY tblAdviser.SchoolYear DESC;"


    If ConnectRS(con, vRS, sSQL) = False Then
        'fatal
        'temp
        MsgBox "Unable to connect Teacher's Advisory Recordset.", vbCritical
        'GoTo ReleaseAndExit
    End If

    If AnyRecordExisted(vRS) = True Then
        While vRS.EOF = False
        txtsection.AddItem vRS!SectionTitle
        vRS.MoveNext
   Wend
    End If

'Exit Function
'ReleaseAndExit:
'    Set vRS = Nothing
End Function

看看这个截图: 在此处输入图像描述

4

2 回答 2

3

HAVING子句引用了这两个字段:

  • tblTeacher.TeacherID
  • tblSection.SchoolYear

SELECT字段列表包括:

  • tblAdviser.TeacherID
  • tblAdviser.SchoolYear

更改查询,使所有引用TeacherID都来自同一个表。对 做同样的事情SchoolYear

顺便说一句,tblTeacher甚至不包含在查询的数据源中。

如果可能,请启动 Access 会话并使用查询设计器来构建此查询。它将帮助您避免此类错误。一旦你有一个在 Access 中工作的查询,然后调整你的代码以生成相同的工作 SQL 语句。

于 2013-01-24T05:34:29.683 回答
0

Group by 必须与聚合函数一起使用,以便可以检索组的组合结果,您没有使用任何聚合函数。

参考 - http://www.w3schools.com/sql/sql_groupby.asp

从查询中删除 group by 并在 where 子句中添加 having 子句。

说明您期望的数据类型,以便我们在查询中为您提供帮助。

于 2013-01-24T05:03:54.180 回答