如果另一个字段为空/空,则选择不返回记录/结果的查询

问题描述 投票:1回答:2

我有一个包含各种字段的表单,用于过滤构建在单个表上的选择查询。问题是记录中的一些字段(也是潜在的标准字段)是空/空白,例如“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)
    );
sql vba ms-access access-vba access
2个回答
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。相反,您希望在应用过滤器之前单独检查每列的NULLity。

新版本 :

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] & "*")
        )
    )

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


0
投票
WHERE ((SAP_RECORD_T.FirstName) Like "*" & [Forms]![SearchBox_F]![txtFName] & "*") 
OR ((SAP_RECORD_T.FirstName) Is Null)
AND ((SAP_RECORD_T.LastName) Like "*" & [Forms]![SearchBox_F]![txtLName] & "*") 
OR((SAP_RECORD_T.LastName) Is Null)

同样,对于其他列,检查列是否与条件匹配或者为null。

© www.soinside.com 2019 - 2024. All rights reserved.