在 VBA 访问子窗体中应用错误的日期过滤器
Posted
技术标签:
【中文标题】在 VBA 访问子窗体中应用错误的日期过滤器【英文标题】:Wrong Date Filter Applying in VBA Access Subform 【发布时间】:2015-11-27 14:01:33 【问题描述】:我在将日期字段上的过滤器应用于我的子表单时遇到了一点问题,我在表格/表单和 PC 上的日期和时间被格式化为 dd/mm/yyyy。我有一个函数是按下按钮时调用。我的代码如下所示:
Function ThisMonth()
Dim strFilter3 As String
Dim dDate as String
dDate = Format(Date - Day(Date) + 1, "dd/mm/yyyy")
strFilter3 = "[Data] > #" & dDate & "#"
' Or I am trying to apply it like this , I tried to see what I get from dDate and dDate2 with MsgBox.
' SECOND OPTION :
'dDate = "01/" & Month(Date) & "/" & Year(Date)
'dDate2 = Format(dDate, "dd/mm/yyyy")
'strFilter3 = "[Data] > #" & dDate2 & "#"
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter3
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True
'Date return the date of today 27/11/2015 while Day(date) return the day 27
End Function
当使用这两个选项中的任何一个应用过滤器时,我看到过滤器的应用如下:> 11/01/2015 [mm/dd/yyyy] insteed of 11/01/2015 [dd/mm/yyyy ].
我尝试将 dDate 和 dDate2 声明为字符串或日期,但没有任何变化...... 我希望我对细节很明确,我的事情很容易格式化,我读了很多东西,但找不到合适的选项....我将尝试从文本框和看看我用这样的命令弄清楚了什么:
strFilter3 = "[Data] > #" & Me.txtASD & "#"
最后,我想对 Today/This Week/This Month/All Time 的子表单进行过滤。我的 Today 函数运行良好,看起来像这样:
Function Today()
Dim dDate As String
dDate = Format(Date, "dd/mm/yyyy")
strFilter2 = "[Data] = #" & dDate & "#"
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter2
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True
End Function
【问题讨论】:
我将格式更改为:dDate2 = Format(dDate, "mm/dd/yyyy") from dDate2 = Format(dDate, "dd/mm/yyyy") 现在它可以工作了,我不知道'不明白,因为我将字段格式化为 dd/mm/yyyy .... 【参考方案1】:我发现了问题所在,现在它可以工作了,必须更改 Format(dDate, "mm/dd/yyyy") 并用 BETWEEN & AND 创建一个括号。我终于让这些功能按照我的意愿工作了,我发布了代码希望能帮助别人:D
frmLogistica - 主窗体
sfrmLogistica - 子表单
Forms!MainForm!SubForm.Form.Filter 可以替换为 Me.SubForm.Form.Filter
Function Today()
Dim dDate As String
dDate = Format(Date, "dd/mm/yyyy")
strFilter2 = "[Data] = #" & dDate & "#"
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter2
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True
End Function
Function ThisWeek()
dDate = Format(Date - 2, "mm/dd/yyyy")
dDate2 = Weekday(dDate)
dDate3 = Date - dDate2
dDate4 = Format(Date - 1, "mm/dd/yyyy")
dDate5 = Weekday(dDate4)
dDate6 = 7 - dDate5
dDate7 = Date + dDate6
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate7 & "#"
Me.Refresh
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter3
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True
End Function
Function ThisMonth()
dDate = Date - Day(Date) + 1
dDate2 = DateSerial(Year(Date), Month(Date) + 1, 0)
dDate3 = Format(dDate, "mm/dd/yyyy")
dDate4 = Format(dDate2, "mm/dd/yyyy")
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate4 & "#"
Me.Refresh
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter3
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True
End Function
Function ThisYear()
dDate = "1/1/" & Year(Date)
dDate2 = "31/12/ " & Year(Date)
dDate3 = Format(dDate, "mm/dd/yyyy")
dDate4 = Format(dDate2, "mm/dd/yyyy")
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate4 & "#"
Me.Refresh
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter3
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True
End Function
Function All()
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = False
End Function
【讨论】:
以上是关于在 VBA 访问子窗体中应用错误的日期过滤器的主要内容,如果未能解决你的问题,请参考以下文章
如何在 VBA 访问的子窗体中显示 Select SQL 语句的结果?