Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

傳回 Variant ( 數) 指出 字串運算式的結果。

語法

StrComp ( string1, string2 [, compare ] )

StrComp 函數語法具有下列自變數:

引數

描述

string1

必要。 任何有效的字串表達式。

string2

必要。 任何有效的字串表達式。

compare

選擇性。 指定字串比較的類型。 如果 比較引數 為 Null,則會發生錯誤。 如果省略 compare[選項比較] 設定會決定比較的類型。

設定

比較自變數設定為:

常數

描述

vbBinaryCompare

0

執行二進位比較。

vbTextCompare

1

執行文字比較。

vbDatabaseCompare

2

Microsoft Office Access 2007 。 根據資料庫中的資訊執行比較。

傳回值

StrComp 函數具有下列傳回值:

If

StrComp 傳

string1 小於 string2

-1

string1 等於 string2

0

string1 大於 string2

1

string1 string2Null

Null

查詢範例

Expression

結果​​

SELECT ProductSales.ProductID, StrComp (ProductID,“PRO_XYZ10”) AS ComparisonResult FROM ProductSales;

傳回 「ProductID」 字段中的值、將 [ProductID] 中的值與 「PRO_XYZ1」 進行比較,然後在 CompareResult 欄中傳回結果。 “ProductID” 等於 “PRO_XYZ10”,StrComp 函數會傳回 0。 如果 「ProductID」 小於 「PRO_XYZ10」,則 StrComp 函數會傳回 -1。 如果 「ProductID」 大於 「PRO_XYZ10」,則 StrComp 函數會傳回 1。 如果 「ProductID」 或 「PRO_XYZ10」 為 NULL,則 StrComp 函數會傳回 NULL。

VBA 範例

附註: 下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。

此範例使用 StrComp 函數傳回字串比較的結果。 如果第三個自變數為 1,則會執行文字比較;如果第三個自變數為0或省略,則會執行二進位比較。

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1) ' Returns 1.

字串函數及其使用方法

Need more help?

Want more options?

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

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×