无一个或多个必需参数的值

时间:2013-10-14 18:41:24

标签: ms-access vb6

我有这个SQL查询

cmd.CommandText = "SELECT [tblStudent]![LastName] & ', ' & [tblStudent]![FirstName] & ' ' & [tblStudent]![MiddleName] AS FullName," & _
" tblGrade.FirstGrading, tblGrade.SecondGrading, tblGrade.ThirdGrading, tblGrade.FourthGrading," & _
" Avg(b.GradeValue) AS AverageGrade" & _
" FROM ((((tblGrade INNER JOIN tblEnrolment ON tblGrade.EnrolmentID=tblEnrolment.EnrolmentID)" & _
    " INNER JOIN tblStudent ON tblStudent.StudentID=tblEnrolment.StudentID)" & _
    " INNER JOIN tblSubjectOffering ON tblSubjectOffering.SubjectOfferingID=tblGrade.SubjectOfferingID)" & _
    " INNER JOIN tblSubject ON tblSubject.SubjectID=tblSubjectOffering.SubjectID)" & _
    " INNER JOIN tblSection ON tblSection.SectionID=tblSubjectOffering.SectionID" & _
" WHERE tblSection.SectionTitle='" & s(0) & "' AND tblSubject.SubjectID='" + s2(0) + "'" & _
" GROUP BY [tblStudent]![LastName] & ', ' & [tblStudent]![FirstName] & ' ' & [tblStudent]![MiddleName], tblGrade.FirstGrading, tblGrade.SecondGrading, tblGrade.ThirdGrading, tblGrade.FourthGrading" & _
" ORDER BY [tblStudent]![LastName] & ', ' & [tblStudent]![FirstName] & ' ' & [tblStudent]![MiddleName]"

这段代码给了我一个运行时错误“没有一个或多个必需参数的值”。

2 个答案:

答案 0 :(得分:2)

我的预感是Access认为b.GradeValue是参数,因为b没有表名或别名:

Avg(b.GradeValue) AS AverageGrade

我建议您Debug.Print完成SELECT语句,或将其写入文本文件,然后在Access查询设计器中将其作为新查询进行测试。当它要求您提供参数值时,它还会显示“name”,无论它认为是什么参数。

答案 1 :(得分:1)

很难仅使用SQL语句提供建议,与其他代码和对象脱离上下文。

目前,我看到两个主要的潜在问题。首先是你有Avg(b.GradeValue)但是在命令的其他地方没有引用b表。为此,您需要删除b或确保引用有效。

其次,您有两个变量s()s2()可能会返回无效或空数据。您可能希望调试此步骤并在继续执行之前检查每个保留的值,然后将问题解决到代码的其他部分(如果一个是坏的)。