选择查询,如果另一个文件为空白/空,则不返回记录/结果

时间:2019-02-25 01:52:04

标签: sql vba ms-access access-vba access

我有一个表单,其中包含用于过滤基于单个表的选择查询的各种字段。问题是记录中的某些字段(也是潜在的条件字段)为空/空白,例如“ FirstName”和“ LastName”可能都为空,但有一个“ StudentID”。

当输入与所有其他字段均为空/空白的记录关联的“ StudentID”时,查询将不返回记录,而是完全不返回记录。 我希望查询返回与特定条件关联的所有记录,尽管其他字段为空/空。

过去,我在所有新记录中都分配了一个默认值,例如“ NoName ...”,因此该字段不是空白,并且可以正常工作但很烦人……

我正在放屁...请让我知道是否需要更多信息。 TYIA

SELECT 
    SAP_RECORD_T.FirstName, 
    SAP_RECORD_T.LastName, 
    SAP_RECORD_T.StudentID,
    SAP_RECORD_T.Term, 
    SAP_RECORD_T.FileComplete, 
    SAP_RECORD_T.CampusSubmitted
FROM SAP_RECORD_T
WHERE 
    (
        ((SAP_RECORD_T.FirstName) Like "*" & [Forms]![SearchBox_F]![txtFName] & "*") 
        AND ((SAP_RECORD_T.LastName) Like "*" & [Forms]![SearchBox_F]![txtLName] & "*") 
        AND ((SAP_RECORD_T.StudentID) Like "*" & [Forms]![SearchBox_F]![StudentID] & "*") 
        AND ((SAP_RECORD_T.Term) Like "*" & [Forms]![SearchBox_F]![txtTerm] & "*") 
        AND ((SAP_RECORD_T.FileComplete) Like "*" & [Forms]![SearchBox_F]![txtFileCmplt] & "*") 
        AND ((SAP_RECORD_T.CampusSubmitted) Like "*" & [Forms]![SearchBox_F]![txtCampus] & "*")
    ) OR (
        ((SAP_RECORD_T.FirstName) Is Null) 
        AND ((SAP_RECORD_T.LastName) Is Null) 
        AND ((SAP_RECORD_T.StudentID) Is Null) 
        AND ((SAP_RECORD_T.Term) Is Null) 
        AND ((SAP_RECORD_T.FileComplete) Is Null) 
        AND ((SAP_RECORD_T.CampusSubmitted) Is Null)
    );

2 个答案:

答案 0 :(得分:1)

您需要在WHERE子句的条件下更改逻辑。

您的WHERE子句:

WHERE 
    (
        ((SAP_RECORD_T.FirstName) Like "*" & [Forms]![SearchBox_F]![txtFName] & "*") 
        AND ((SAP_RECORD_T.LastName) Like "*" & [Forms]![SearchBox_F]![txtLName] & "*") 
        AND ((SAP_RECORD_T.StudentID) Like "*" & [Forms]![SearchBox_F]![StudentID] & "*") 
        AND ((SAP_RECORD_T.Term) Like "*" & [Forms]![SearchBox_F]![txtTerm] & "*") 
        AND ((SAP_RECORD_T.FileComplete) Like "*" & [Forms]![SearchBox_F]![txtFileCmplt] & "*") 
        AND ((SAP_RECORD_T.CampusSubmitted) Like "*" & [Forms]![SearchBox_F]![txtCampus] & "*")
    ) OR (
        ((SAP_RECORD_T.FirstName) Is Null) 
        AND ((SAP_RECORD_T.LastName) Is Null) 
        AND ((SAP_RECORD_T.StudentID) Is Null) 
        AND ((SAP_RECORD_T.Term) Is Null) 
        AND ((SAP_RECORD_T.FileComplete) Is Null) 
        AND ((SAP_RECORD_T.CampusSubmitted) Is Null)
    )

实际上,您的条件允许在所有搜索框条件上都匹配的记录,或者所有过滤器列均为NULL的记录。相反,您希望在应用过滤器之前分别检查每一列的NULL有效性。

新版本:

WHERE 
    (
        ( 
            ((SAP_RECORD_T.FirstName) Is Null)  
            OR ((SAP_RECORD_T.FirstName) Like "*" & [Forms]![SearchBox_F]![txtFName] & "*") 
        )
        AND (
            ((SAP_RECORD_T.LastName) Is Null) 
            OR ((SAP_RECORD_T.LastName) Like "*" & [Forms]![SearchBox_F]![txtLName] & "*")  
        )
        AND (
            ((SAP_RECORD_T.StudentID) Is Null)
            OR ((SAP_RECORD_T.StudentID) Like "*" & [Forms]![SearchBox_F]![StudentID] & "*")
        ) 
        AND (
            ((SAP_RECORD_T.Term) Is Null) 
            OR ((SAP_RECORD_T.Term) Like "*" & [Forms]![SearchBox_F]![txtTerm] & "*") 
        )
        AND (
            ((SAP_RECORD_T.FileComplete) Is Null) 
            OR ((SAP_RECORD_T.FileComplete) Like "*" & [Forms]![SearchBox_F]![txtFileCmplt] & "*") 
        )
        AND (
            ((SAP_RECORD_T.CampusSubmitted) Is Null)
            OR ((SAP_RECORD_T.CampusSubmitted) Like "*" & [Forms]![SearchBox_F]![txtCampus] & "*")
        )
    )

注意:查询格式正确(缩进,...)时,这些内容更容易发现。

答案 1 :(得分:0)

Rental Date  | OUTBOUND | INBOUND | VEHICLE_SIZE | lolyRATE   | KAYRATE 
12/28/2019       223         333          small      33.5          NULL     
12/28/2019       223         333          small      NULL          33.5     

类似地,对于其他列,检查该列是否符合条件OR为空。