【问题标题】:Opening form with subforms with whereCondition使用 whereCondition 打开带有子表单的表单
【发布时间】:2021-11-10 07:41:58
【问题描述】:

我有一个表单,其值为 dateFromdateTo。 单击按钮时,我想打开一个包含两个子表单的新表单。其中一个子表单显示过滤的记录。它显示了note_datedateFromdateTo 之间的记录。

note_date 是查询中的列之一,它是过滤子表单中的记录源

因此,其中一个子表单的过滤器不起作用。

这是我的代码,我认为它会如何工作

Overview_of_vacation_notes 是包含两个子表单的表单的名称

dtmFrom = Text56.Value 'start date
dtmTo = Text58.Value   'end date
Dim strCriteria As String

strCriteria = "[note_date] >= #" & Format(dtmFrom, "yyyy-mm-dd") & "# AND [note_date] <= #" & Format(dtmTo, "yyyy-mm-dd") & "#"
    
DoCmd.OpenForm "Overview_of_vacation_notes", whereCondition:=strCriteria

有没有办法让我做这样的事情?

DoCmd.OpenForm "Overview_of_vacation_notes", subformName.whereCondition:=strCriteria

因为我的代码不起作用,因为 whereCondition 在主窗体而不是子窗体上使用 strCriteria

【问题讨论】:

    标签: vba ms-access subform openform


    【解决方案1】:

    DoCmd.OpenForm "Overview_of_vacation_notes" 正在打开父表单。因此,您不能将 where 条件应用于此行。您需要为子表单设置过滤条件并过滤该子表单以显示过滤后的数据。试试下面的代码。

    dtmFrom = Text56.Value 'start date
    dtmTo = Text58.Value   'end date
    Dim strCriteria As String
    
    strCriteria = "[note_date] >= #" & Format(dtmFrom, "yyyy-mm-dd") & "# AND [note_date] <= #" & Format(dtmTo, "yyyy-mm-dd") & "#"
        
    DoCmd.OpenForm "Overview_of_vacation_notes"
    Forms![Overview_of_vacation_notes]![YourSubform].Form.FilterOn = False 'Clear previous filter.
    Forms![Overview_of_vacation_notes]![YourSubform].Form.Filter = strCriteria 'Set filter criteria
    Forms![Overview_of_vacation_notes]![YourSubform].Form.FilterOn = True 'Apply filter.
    

    【讨论】:

    • @jenshon 很高兴知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多