从另一个过程调用语法错误3075

时间:2016-09-11 08:51:15

标签: vba ms-access-2007

我试图通过“搜索”按钮在拆分表单上的两个日期之间过滤记录,该按钮调用以下代码

Sub search()

Dim strCriteria ,taSk As String 

Me.Refresh
If IsNull(Me.txtDiaryDatefrom) Or IsNull(Me.txtDiaryDateTo) 
Then
    MsgBox "Please Enter Date Range", vbInformation, "Date Range Required"
    Me.txtDiaryDatefrom.SetFocus 
Else
    strCriteria = "([DiaryDate]>= # " & Me.txtDiaryDatefrom & " # And [DiaryDate] <= #" & Me.txtDiaryDateTo & " # )"
    taSk = "SELECT * from tblDiary where ( " & strCriteria & ")"
    DoCmd.ApplyFilter , taSk 
End If 

End Sub

执行时,它会给出运行时错误3075读数

  

查询表达式中的语法错误&#39;从tblDiary中选择* .....

有人能找出解决问题的方法吗?

1 个答案:

答案 0 :(得分:1)

您需要删除哈希之间的空格。

"([DiaryDate]>= #" & Me.txtDiaryDatefrom & "# And [DiaryDate] <= #" & Me.txtDiaryDateTo & "# )"

我会更改查询以便结束[DiaryDate] < Date + 1。这将确保具有日期/时间值的记录不会被意外过滤掉。

"([DiaryDate]>= #" & DateValue(Me.txtDiaryDatefrom) & "# And [DiaryDate] < #" & DateValue(Me.txtDiaryDateTo) + 1 & "# )"