DocmdApplyFilter语法错误

问题描述 投票:0回答:1

在我的Access表单中,我得到了一个

               Run-time error'3705:

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

每当我运行我的代码时,没有填充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

我期待如果我只是取消对话框,表单应保留在屏幕上。此外,如果“from”和“t / o”字段为空,我应该得到MsgBox结果。

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

access-vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.