我在运行此代码时遇到问题。它给我查询表达式
中的语法错误(缺少运算符)Function SearchCriteria()
Dim类,StateProvince,strAcademicYear作为字符串
Dim任务,strCriteria作为字符串
如果isNull(Forms!frmStudentList!cboClass)然后
Class =“ [Class]喜欢'*'”
其他
Class = [Class] =“&Forms!frmStudentList!cboClass
如果结束
如果isNull(Forms!frmStudentList!cboStateProvince)然后
StateProvince =“ [StateProvince]喜欢'*'”
其他
StateProvince = [StateProvince] =“&
Forms!frmStudentList!cboStateProvince
如果结束
如果isNull(Forms!frmStudentList!cboAcademicYear)然后
StrAcademicYear =“ [AcademicYear]喜欢'*'”
其他
StrAcademicYear = [AcademicYear] ='“&
Forms!frmStudentList!cboAcademicYear和“'”
如果结束
strCriteria =类&“ AND”&StateProvince&“ AND”&StrAcademicYear
task =“ SELECT * FROM QryStudentSearch位置和条件
Forms!frmStudentList.RecordSource =任务
Forms!frmStudentList.Requery
结束功能
这段代码有很多问题。
Variant
。LIKE '*'
选择数据。无需这样做,因为不应用过滤器意味着无论如何都将返回所有记录。Sub
而不是Function
。我将代码重写为:
Sub sSearch()
Dim strSearch As String
If Not IsNull(Forms!frmStudentList!cboClass) Then
strSearch = strSearch & " AND [Class]='" & Forms!frmStudentList!cboClass & "' "
End If
If Not IsNull(Forms!frmStudentList!cboStateProvince) Then
strSearch = strSearch & " AND [StateProvince]='" & Forms!frmStudentList!cboStateProvince & "' "
End If
If Not IsNull(Forms!frmStudentList!cboAcademicYear) Then
strSearch = strSearch & " AND [AcademicYear]='" & Forms!frmStudentList!cboAcademicYear & "' "
End If
If Left(strSearch, 4) = " AND" Then
strSearch = "WHERE " & Mid(strSearch, 6)
End If
strSearch = "SELECT * FROM qryStudentSearch " & strSearch
Forms!frmStudentList.RecordSource = strSearch
Forms!frmStudentList.Requery
End Sub
在每种情况下,我都假定每个组合框的绑定列是文本,因此需要在数据周围使用单引号。如果绑定的列是数字,则可以删除单引号。
问候,