![](/img/trans.png)
[英]MS Access - Using Form Inputs to Filter on Multiple Criteria in a Query
[英]MS Access, Selecting records based on multiple criteria form inputs
我有一個 Access 表單連接到一個查詢,該查詢基於來自 3 個不同框的輸入。 如果 box1 被填充,它需要對后續的框保持 true 但如果為空可以忽略。 其他兩個框(box2 和 box3)可以有數據,也可以為空,如果為空,則應忽略。
舉一個廣泛的例子,如果我要搜索包含在具有 5 個不同圖書館的圖書館系統中的書籍,我想選擇特定圖書館中 John Doe 和 Jane Deer 的所有書籍或 John Doe 和 Jane 的書籍鹿在所有圖書館或只是簡鹿的書等。
我一直在嘗試遵循此處描述的內容,除非我將任何輸入字段留空,否則它會起作用。
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)))
或者,我也嘗試過使用 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 & "*")))
我的語法有問題還是我應該這樣做?
鏈接技術僅在所有搜索框具有相同的“排名”並與 AND 組合時才有效。
如果從邏輯上看,整個作者塊與 OR 組合在一起,因此其中的每個空搜索框都會使整個塊為 TRUE。
因此,在您的情況下,您需要使用 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
和
數據庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.