【问题标题】:ApplyFilter works until I hit the clear button which clears the filtersApplyFilter 一直有效,直到我点击清除过滤器的清除按钮
【发布时间】:2020-07-14 16:42:49
【问题描述】:

我创建了一个表单,该表单应按用户可以输入的日期进行过滤。输入所需日期后,用户单击“限制”按钮,以便数据可以过滤这些范围。在用户点击清除按钮之前,它工作得非常好。

过滤仍然有效,但用户输入日期的验证天气不起作用。它抛出错误“运行时错误3000。保留错误(-3201) 验证是检查输入日期的天气,如果没有,应该会出现一个消息框。

--这是显示日期范围的“限制按钮”--

Private Sub eingrenzen()

Dim strCriteria, task As String

'MsgBox if no entry    
' "Me.vonDatumFeld" is german and means "fromDate"    
' "Me.bisDatumFeld" means "untilDate"

Me.Refresh

If IsNull(Me.vonDatumFeld) Or IsNull(Me.bisDatumFeld) 

Then

    MsgBox "Grenzen sie bitte den Zeitraum 'von' und 'bis' ein", vbInformation, "Zeitraum eingrenzen"
    Me.vonDatumFeld.SetFocus        

Else

    strCriteria = "([Datum] >= #" & Format(Me.vonDatumFeld, "yyyy-mm-dd") & "# And [Datum] <= #" & Format(Me.bisDatumFeld, "yyyy-mm-dd") & "#)"
    task = "select * from TblTeile where (" & strCriteria & ") order by [Datum]"

    DoCmd.ApplyFilter task 'Wende ZeitraumFILTER an

End If

End Sub

--这就是“清除按钮”--

Private Sub CmdLösch_Click()

Me.KIDText = vbNullString    
Me.FirmaTxt = vbNullString    
Me.DatumTxt = vbNullString    
Me.ProdArttxt = vbNullString    
Me.BeschrText = vbNullString    
Me.MaterialText = vbNullString    
Me.vonDatumFeld = vbNullString    
Me.bisDatumFeld = vbNullString    
Me.Filter = vbNullString 

Me.FilterOn = False        

Me.Requery 

End Sub

【问题讨论】:

标签: vba ms-access


【解决方案1】:

使用过滤器方法:

strCriteria = "[Datum] >= #" & Format(Me.vonDatumFeld, "yyyy-mm-dd") & "# And [Datum] <= #" & Format(Me.bisDatumFeld, "yyyy-mm-dd") & "#"
' task = "select * from TblTeile where (" & strCriteria & ") order by [Datum]"
Me.Filter = strCriteria
Me.FilterOn = True

【讨论】:

  • 那么你所有的日期值都符合条件。
  • 有了这个建议,不再有错误,但现在它不再限制日期范围。因此我尝试了strCriteria = "[TblTeile.Datum] &gt;= #" &amp; Format(Me.vonDatumFeld, "yyyy-mm-dd") &amp; "# And [TblTeile.Datum] &lt;= #" &amp; Format(Me.bisDatumFeld, "yyyy-mm-dd") &amp; "#" Me.Filter = strCriteria Me.FilterOn = True 这有效,我得到了日期范围。但是当我将日期留空时,它会再次抛出运行时错误 3000。
  • 是的,Format 对于 Null 值将失败。因此,如果文本框为空,请调整条件以将其排除在外。
  • 您好 Gustav,IF 语句的 Null 值确实存在问题。我在If Len(Me.vonDatumFeld) = 0 Or Len(Me.bisDatumFeld) = 0 Then 中更改了它,现在它可以工作了。谢谢!
猜你喜欢
  • 2014-05-03
  • 1970-01-01
  • 2012-03-14
  • 2020-07-15
  • 2016-08-26
  • 2017-07-13
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多