套用到
Microsoft 365 Access Access 2024 Access 2021 Access 2019 Access 2016

傳回 Variant (String),包含字串的指定字元數。

語法

( 字串中間,開始 [, 長度 ] )

Mid 函數語法具有下列引數:

引數

描述

string

必要。 字串運算式 從中傳回字元。 如果 字串 包含 Null,則會傳回 Null

開始

必要。 長。 字串中要取用的部分開始的字元位置。 如果 start 大於 字串中的字元數, Mid 會傳回零長度的字串 (“”) 。

length

可省略。 變體 () 。 要傳回的字元數。 如果省略,或文字中的字元少於 長度 (包括 開頭) 的字元,則會傳回從 串開始位置到結尾的所有字元。

註解

若要判斷 string 中的字元數,請使用 Len 函數。

附註: MidB 函數與字串中包含的位元組資料搭配使用,就像在雙位元組字元集語言中一樣。 引數不是指定字元數,而是指定位元組數。 如需使用 MidB 的範例程式碼,請參閱範例主題中的第二個範例。

查詢範例

Expression

結果​​

SELECT ProductID,中間 (ProductID,5) 作為 Expr1 FROM ProductSales;

傳回「ProductID」和 ProductID 從字元位置 5 開始的部分,並在 Expr1 欄中顯示結果。

SELECT ProductID,Mid (ProductID,5,4) AS testMid FROM ProductSales;

傳回 “ProductID” 和 ProductID 從字元位置 5 開始的部分,包含 4 個字元,並在資料行 testMid 中顯示結果。

VBA 範例

附註: 下列 VBA 範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此功能。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。

第一個範例使用 Mid 函數從字串傳回指定數量的字元。

Dim MyString, FirstWord, LastWord, MidWords
MyString = "Mid Function Demo"    ' Create text string.
FirstWord = Mid(MyString, 1, 3)    ' Returns "Mid".
LastWord = Mid(MyString, 14, 4)    ' Returns "Demo".
MidWords = Mid(MyString, 5)    ' Returns "Function Demo".

第二個範例使用 MidB 和使用者定義的函數 (MidMbcs) 也從字串傳回字元。 這裡的區別在於輸入字串是ANSI,長度以位元組為單位。

Function MidMbcs(ByVal str as String, start, length)
    MidMbcs = StrConv(MidB(StrConv(str, vbFromUnicode), _
              start, length), vbUnicode)
End Function
Dim MyString
MyString = "AbCdEfG"
' Where "A", "C", "E", and "G" are DBCS and "b", "d", 
' and "f" are SBCS.
MyNewString = Mid(MyString, 3, 4)
' Returns ""CdEf"
MyNewString = MidB(MyString, 3, 4)
' Returns ""bC"
MyNewString = MidMbcs(MyString, 3, 4)
' Returns "bCd"

另請參閱

字串函數及其使用方法

需要更多協助嗎?

想要其他選項嗎?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。