SQL包括条件在Where if not not null

时间:2017-06-20 13:28:42

标签: sql ms-access subform

在访问中,我一直在尝试在子表单中显示的内容上设置用户过滤器,子表单是检查列表。我尝试过的其他方法没有工作,但我已经成功使用下面的代码,如果用户填写任何其他过滤器选项,它将过滤。我能弄清楚的是如何让它接受多个过滤器,除非我拼写出每个可能的盒子组合

那么无论如何这实际上是可能的,还是我需要查看其他选项?

WHERE [STATUS] = "OPEN" 
AND 
(ANY FORM FILTERS is not Null [Filter by all those that are not null to its matching column])

有效的是,如果表单过滤器不为空,则包含条件的任何简单方法

SELECT Inspections.INS_ID
    ,Inspections.Category
    ,Inspections.Assigned_Officer
    ,Inspections.Raised_For
    ,Inspections.Account
    ,Inspections.Number
    ,Inspections.Street
    ,Inspections.Area
    ,Inspections.Postcode
    ,Inspections.Date_Raised
    ,Inspections.Reason
    ,Inspections.INS_Comments
FROM Inspections
WHERE (
        ((Inspections.STATUS) = "Open")
        AND (([Forms]![Manage_Open]![Filter_ID]) IS NULL)
        AND (([Forms]![Manage_Open]![Filter_account]) IS NULL)
        AND (([Forms]![Manage_Open]![Filter_officer]) IS NULL)
        AND (([Forms]![Manage_Open]![Filter_Number]) IS NULL)
        AND (([Forms]![Manage_Open]![Filter_Postcode]) IS NULL)
        AND (([Forms]![Manage_Open]![Filter_Category]) IS NULL)
        AND (
            (([Forms]![Manage_Open]![Filter_From]) IS NULL)
            AND (([Forms]![Manage_Open]![Filter_To]) IS NULL)
            )
        )
    OR (
        ([Forms]![Manage_Open]![Filter_ID]) IS NOT NULL
        AND ([Forms]![Manage_Open]![Filter_ID]) = [Inspections].[INS_ID]
        )
    OR (
        ([Forms]![Manage_Open]![Filter_account]) IS NOT NULL
        AND ([Forms]![Manage_Open]![Filter_account]) = [Inspections].[Account]
        )
    OR (
        ([Forms]![Manage_Open]![Filter_officer]) IS NOT NULL
        AND ([Forms]![Manage_Open]![Filter_officer]) = [Inspections].[Assigned_Officer]
        )
    OR (
        ([Forms]![Manage_Open]![Filter_Number]) IS NOT NULL
        AND ([Forms]![Manage_Open]![Filter_Number]) = [Inspections].[Number]
        )
    OR (
        ([Forms]![Manage_Open]![Filter_Postcode]) IS NOT NULL
        AND ([Forms]![Manage_Open]![Filter_Postcode]) = [Inspections].[Postcode]
        )
    OR (
        ([Forms]![Manage_Open]![Filter_Category]) IS NOT NULL
        AND ([Forms]![Manage_Open]![Filter_Category]) = [Inspections].[Category]
        )
    OR (
        (
            (([Forms]![Manage_Open]![Filter_From]) IS NOT NULL)
            AND (([Forms]![Manage_Open]![Filter_To]) IS NOT NULL)
            )
        AND ([Inspections].[Raised_For]) BETWEEN (
                    ([Forms]![Manage_Open]![Filter_From])
                    AND ([Forms]![Manage_Open]![Filter_to])
                    ) )
        );

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您的表单中有一些用户将填写的未绑定控件。您可以根据用户输入的内容过滤子表单。 在这种情况下,您尝试使用的方法将变得非常复杂且难以维护,更不用说大量数据的速度慢。 最好的办法是在代码中构建过滤器。 我经常做的是将控件命名为与字段名称相同,并将字段类型放在控件的标记属性中。然后遍历控件以构建过滤器。这是航空代码,但应该给你一个开始。 Dim strFilter as string Dim ctL as Access.control For Each ctL in Me.Controls If Not ISNull(ctL) And ctL.Tag <> "" Then strFilter = strFilter & " AND " & ctl.Name & " = " If ctL.Tag = "Text" Then strFilter = strFilter & "'" & Replace(ctl, "'","''") & "'" ElseIf ctL.Tag = "Date" Then strFilter = strFilter & "#" & ctl & "#" Else strFilter = strFilter & ctlEnd If Next If strFilter <> "" Then strFilter = mid(strFilter, 6) strFilter = " Where " & strFilter End If Me.yourSubForm.Form.Recordsource = "Select * From <yourQuery> " & strfilter