【发布时间】:2015-05-09 21:31:16
【问题描述】:
我在 MS Access 2013 表单中的单击按钮事件中有以下代码,其数据源来自具有以下字段“EnrNo、FirstName、LastName 和 RegDate”的查询。
表单有三个文本框和一个命令按钮:
- txt关键字
- txtDateFrom
- txtDateTo
- cmdSearch
表单中的单击按钮 (cmdSearch) 旨在根据三个条件过滤查询,这些条件可以是两个“EnrNo、FirstName、LastName”和文本中输入的日期范围中的任何一个框 txtDateTo 和 cmdSearch
我的代码仅成功过滤了 EnrNo。请帮帮我...感谢您的宝贵时间。
Private Sub cmdSearch_Click()
Dim strWhere As String 'The criteria string.
Dim Where As String
Dim lngLen As Long 'Length of the criteria string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
'Text field example. Use quotes around the value in the string.
If Not IsNull(Me.txtFilter) Then
strWhere = strWhere & "([EnrNo] = """ & Me.txtFilter & """) OR "
strWhere = strWhere & "([FirstName] = """ & Me.txtFilter & """) AND "
End If
'Date field example. Use the format string to add the # delimiters and get the right international format.
If Not IsNull(Me.txtFrom) Then
strWhere = strWhere & "([RegDate] >= " & Format(Me.txtFrom, conJetDate) & ") AND "
End If
'Another date field example. Use "less than the next day" since this field has times as well as dates.
If Not IsNull(Me.txtTo) Then 'Less than the next day.
strWhere = strWhere & "[RegDate] BETWEEN #" & Format(Me.txtFrom, "mm/dd/yyyy") & "# AND #" & Format(Me.txtTo, "mm/dd/yyyy") & "# "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
'Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
【问题讨论】:
-
你可以粘贴或调试。一旦填充了过滤器,请打印整个 sql 过滤器?更容易告诉你问题是什么,我怀疑它的东西没有正确括起来
-
它显示“运行时错误 '3075': 查询表达式中的数据语法错误 '([EnrNo] = “MS-12/IT-004”) 或 ([FirstName] = “MS -12/IT-004”) AND ([RegDate] >= #06/16/2014#) AND [RegDate] BETWEEN #06/16/2014# AND #09/23/2'