DocmdApplyFilter语法错误

时间:2019-04-30 03:26:33

标签: access-vba

在我的Access表格中,我得到

               Run-time error'3705:

日期查询表达式'(((([购买日期]> = ## And [购买日期] <= ##))'的语法错误。

每当我运行没有Me.TxtPurchaseDateTo和Me.TxtPurchaseDateTo的代码时 填充的字段,而不是应该运行的msg框。另外,当我单击“清除”按钮时,无论是否显示数据,都会弹出“输入参数值”对话框。为了清除数据形式,我必须在“输入参数值”对话框的输入框中单击空格键以清除数据形式。如果我单击“取消”,则会收到运行时错误2501,“ ApplyFilter操作已取消,如果调试了错误,则会将我带到代码表中,突出显示” DoCmd.ApplyFilter任务”。

我删除了代码的几部分,重新​​检查了拼写和空格

Option Compare Database


 Private Sub CmdSearch_Click()
 'Search button

    Call Search
    End Sub
        Sub Search()
        Dim strCriteria, task As String

    Me.Refresh
If IsNull(Me.TxtPurchaseDateFrom) Or IsNull(Me.TxtPurchaseDateTo) Then
    MsgBox "Please enter the date range", vbInformation, "Date Range 
Required"
    Me.TxtPurchaseDateFrom.SetFocus
Else
    strCriteria = "([Date of Purchase] >= #" & Me.TxtPurchaseDateFrom & "# 
And [Date of Purchase] <=#" & Me.TxtPurchaseDateTo & "#)"
    task = "select * From TblPurchases Where( " & strCriteria & ") order 
by [Date of Purchase] "
    DoCmd.ApplyFilter task
    'Me.TxtTotal = FindRecordCount

End If


End Sub

    Private Sub CmdClear_Click()

    Dim task As String

    Me.TxtPurchaseDateFrom = ""
    Me.TxtPurchaseDateTo = ""
    task = "select * from TblPurchases where PrimaryKey is null"
    DoCmd.ApplyFilter task

     'Me.TxtTotal = FindRecordCount



End Sub

    Private Sub CmdShowAll_Click()
Dim task As String

    Me.TxtPurchaseDateFrom = ""
    Me.TxtPurchaseDateTo = ""
    task = "select * from TblPurchases order by [Date of Purchase] "
    Me.RecordSource = task
     'Me.TxtTotal = FindRecordCount

End Sub

我希望,如果我只是取消对话框,该表格应该会保留在屏幕上。 另外,如果“发件人”和“ t / o”字段为空,我应该得到MsgBox结果。

我不确定“ DoCmd.ApplyFilter任务”周围的语法错误是什么 我没看到什么错误?

1 个答案:

答案 0 :(得分:0)

您的条件不能是完整的SQL语句,只能是条件:

strCriteria = "[Date of Purchase] >= #" & Me.TxtPurchaseDateFrom & "# And [Date of Purchase] <= #" & Me.TxtPurchaseDateTo & "#"
DoCmd.ApplyFilter strCriteria

您可以使用 Nz 来避免空文本框中的错误:

strCriteria = "[Date of Purchase] >= #" & Nz(Me!TxtPurchaseDateFrom.Value, Date) & "# And [Date of Purchase] <= #" & Nz(Me!TxtPurchaseDateTo.Value, Date) & "#"
DoCmd.ApplyFilter strCriteria

或者:

If IsNull(Me!TxtPurchaseDateFrom.Value) Then
    strCriteria = "[Date of Purchase] <= #" & Nz(Me!TxtPurchaseDateTo.Value, Date) & "#"
Else
    strCriteria = "[Date of Purchase] >= #" & Nz(Me!TxtPurchaseDateFrom.Value, Date) & "# And [Date of Purchase] <= #" & Nz(Me!TxtPurchaseDateTo.Value, Date) & "#"
End If
DoCmd.ApplyFilter strCriteria