我正在尝试构建一个VBA代码,以某种方式应用一个过滤器,该过滤器仅显示字段[Date]值介于&To日期(用户输入)之间的记录。下面是我的表格视图。
对象名称如下
设置范围按钮= btnSetRange,From TextBox = txtFromDate; To TextBox = txtToDate;子表单名称= ShipmentHistoryDataSheet; Subform-Source Object = Forms![Shipment-History子表单]。 Shipment-History子表单Record Source是表“Shipment-History”中的选择查询。
我曾尝试自己编写代码,但由于我不是访问或编码方面的专家,因此效果不佳。下面是我写的代码,但没有按预期工作:P
Private Sub btnSetRange_Click()
Dim subform2 As Form
Set subform2 = Me!ShipmentHistoryDataSheet.Form
subform2.Filter = "[Date]>" & Me.txtFromDate And "[Date]<" & Me.txtToDate
subform2.FilterOn = True
subform2.Requery
End Sub
有人可以帮忙吗?
尝试使用日期值的字符串表达式的特定格式:
Private Sub btnSetRange_Click()
Dim subform2 As Form
Set subform2 = Me!ShipmentHistoryDataSheet.Form
subform2.Filter = "[Date] >= #" & Format(Me.txtFromDate, "yyyy\/mm\/dd") & "# And [Date] < #" & Format(Me.txtToDate, "yyyy\/mm\/dd") & "#"
subform2.FilterOn = True
' subform2.Requery ' should not be needed.
End Sub