简体   繁体   English

MS Access,根据多个条件表单输入选择记录

[英]MS Access, Selecting records based on multiple criteria form inputs

I've got an Access form connected to a query that's based on the input from 3 different boxes.我有一个 Access 表单连接到一个查询,该查询基于来自 3 个不同框的输入。 If box1 is filled, it needs to hold true for the subsequent boxes but can be ignored if empty.如果 box1 被填充,它需要对后续的框保持 true 但如果为空可以忽略。 The other two boxes (box2 and box3) can either have data or be empty and if empty should be ignored.其他两个框(box2 和 box3)可以有数据,也可以为空,如果为空,则应忽略。

To use a broad example, if I'm searching for books contained within a library system that has 5 different libraries, I want to either select all the books within a specific library by John Doe and Jane Deer or the books by John Doe and Jane Deer held at all libraries or just books by Jane Deer, etc.举一个广泛的例子,如果我要搜索包含在具有 5 个不同图书馆的图书馆系统中的书籍,我想选择特定图书馆中 John Doe 和 Jane Deer 的所有书籍或 John Doe 和 Jane 的书籍鹿在所有图书馆或只是简鹿的书等。

I'd been trying to follow what's described here , and it works unless I leave any of the input fields empty.我一直在尝试遵循此处描述的内容,除非我将任何输入字段留空,否则它会起作用。

WHERE ((tblBooks.LibraryName LIKE [Forms]![frmSearchAuthors]!Box1 & "*") OR ([Forms]![frmSearchAuthors]!Box1 IS NULL)) 
AND (((tblBooks.Author Like "*" & [Forms]![frmSearchAuthors]!Box2 & "*") OR ([Forms]![frmSearchAuthors]!Box2 IS NULL)) 
OR ((tblBooks.Author Like "*" & [Forms]![frmSearchAurthors]!Box3 & "*") OR ([Forms]![frmSearchAuthors]!Box3 IS NULL)))

Alternatively, I have also tried using an IIF statements but couldn't figure out how to ignore any boxes that were left empty.或者,我也尝试过使用 IIF 语句,但无法弄清楚如何忽略任何留空的框。

WHERE (tblBooks.LibraryName Like [Forms]![frmSearchAuthors]!Box1 & "*" OR [Forms]![frmSearchAuthors]!Box1 IS NULL) 
AND (IIF (ISNULL([Forms]![frmSearchAuthors]!Box2), (tblBooks.Author IS NULL), (tblBooks.Author Like "*" & [Forms]![frmSearchAuthors]!Box2 & "*")) 
OR IIF (ISNULL([Forms]![frmSearchAuthors]!Box3), (tblBooks.Author IS NULL), (tblBooks.Author Like "*" & [Forms]![frmSearchAuthors]!Box3 & "*")))

Is there something wrong with my syntax or is there another way I should be doing this?我的语法有问题还是我应该这样做?

The linked technique only works if all search boxes have the same "rank" and are combined with AND.链接技术仅在所有搜索框具有相同的“排名”并与 AND 组合时才有效。

If you look logically, the whole author block is combined with OR, so each empty search box in there will make the whole block TRUE.如果从逻辑上看,整个作者块与 OR 组合在一起,因此其中的每个空搜索框都会使整个块为 TRUE。

So in your case you need to build the search string with VBA, pseudo code:因此,在您的情况下,您需要使用 VBA 伪代码构建搜索字符串:

for i = 1 to n
  if not isnull(authorsearchbox(i)) then
    strSearch = StrAppend(strSearch, CSql(authorsearchbox(i)), " OR ")
  end if
next i

'-------- with --------

' Append sAppend to sBase, use sSeparator if sBase wasn't empty
Public Function StrAppend(sBase As String, sAppend As Variant, sSeparator As String) As String
    If Len(sAppend) > 0 Then
        If sBase = "" Then
            StrAppend = Nz(sAppend, "")
        Else
            StrAppend = sBase & sSeparator & Nz(sAppend, "")
        End If
    Else
        StrAppend = sBase
    End If
End Function

and
CSql数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM