2016-07-29 59 views
-1

我想創建一個查詢來標識長文本字符串不包含任何子字符串列表的條目。MS Access變量字符串搜索

什麼我是...

SELECT [other necessary data], [Long text string] 
WHERE [Long String] NOT LIKE "*[substring1]*" 
    AND [Long string] NOT LIKE "*[substring2]*" 
    AND [Long string] NOT LIKE "*[substring3]*" 

這工作得很好。然而!我希望排除的子串列表是可變的,例如項目1不包括子字符串1和3,項目2不包括子字符串2,3和17等。

有關如何執行此操作的任何建議?

+0

請演示您是如何嘗試解決問題和出錯的地方。瞭解如何提出問題http://stackoverflow.com/help – alexi2

+0

項目是您選擇的表格中的一列嗎? –

回答

0

也許這樣?然後你只需要調用它,最多可以有四個字符串來排除和使用sql,無論你想要做什麼。

Function strsql(ex1 as string,Optional ex2 as string = "", _ 
Optional ex3 as string = "", Optional ex4 as string = "") as string 
strsql = "SELECT [other necessary data], [Long text string] _ 
WHERE [Long String] NOT LIKE " & chr(34) & "*" & ex1 & "*" & _ 
chr(34) 
If ex2 <> "" then 
strsql = strsql & " AND [Long String] NOT LIKE " & chr(34) & _ 
"*" & ex2 & "*" & chr(34) 
End if 
If ex3 <> "" then 
strsql = strsql & " AND [Long String] NOT LIKE " & chr(34) & _ 
"*" & ex3 & "*" & chr(34) 
End if 
If ex4 <> "" then 
strsql = strsql & " AND [Long String] NOT LIKE " & chr(34) & _ 
"*" & ex4 & "*" & chr(34) 
End if 
strsql = strsql & ";" 
End Function