共用方式為


In 運算子 (Microsoft Access SQL)

適用於:Access 2013 | Access 2016

決定運算式的值是否等於指定清單中的任何值。

語法

expr [ Not ] In ( value1, value2, ... )

expr [ Not ] In ( SELECT ... )

註解

In 運算子的語法有以下部分:

部分 描述
expr 針對您想要評估的資料識別所屬欄位的運算式。
value1, value2 運算式或要計算 expr 的一連串的運算式。

如果在值清單中找到 exprIn 運算子會傳回 True;否則,它會傳回 False。 可以包含 Not 邏輯運算子來計算相反的情況 (也就是,不論 expr 是否不在值清單中)。

例如,可以使用 In 來決定運送到一段特定區域的訂單:

SELECT * 
FROM Orders 
WHERE ShipRegion In ('Avon','Glos','Som')

您也可以使用 In 來參考存在於外部資料庫檔案中的資料表或查詢:

SELECT qryValues 
FROM qryValues In 'c:\files\MyDB.accdb'

範例

下列範例會使用 Northwind.mdb 資料庫中的 [Orders] 資料表來建立一項查詢,而這項查詢會包含出貨至 Lancashire 和 Essex 的所有訂單以及出貨日期。

本範例會呼叫 EnumFields 程序,您可以在 SELECT 陳述式範例中找到該程序。

Sub InX() 
 
    Dim dbs As Database, rst As Recordset 
 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
 
    ' Select records from the Orders table that 
    ' have a ShipRegion value of Lancashire or Essex. 
    Set rst = dbs.OpenRecordset("SELECT " _ 
        & "CustomerID, ShippedDate FROM Orders " _ 
        & "WHERE ShipRegion In " _ 
        & "('Lancashire','Essex');") 
     
    ' Populate the Recordset. 
    rst.MoveLast 
     
    ' Call EnumFields to print the contents of 
    ' the Recordset. 
    EnumFields rst, 12 
 
    dbs.Close 
 
End Sub

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應