ComboBox.ValidationRule 屬性 (Access)ComboBox.ValidationRule property (Access)

您可以使用 ValidationRule 屬性來指定資料輸入至記錄、 欄位或控制項的需求。You can use the ValidationRule property to specify requirements for data entered into a record, field, or control. 當資料輸入違反 ValidationRule 設定時,您可以使用 ValidationText 屬性來指定要對使用者顯示的訊息。When data is entered that violates the ValidationRule setting, you can use the ValidationText property to specify the message to be displayed to the user. 可讀寫 的字串Read/write String.

語法Syntax

運算式驗證規則expression.ValidationRule

_運算式_代表**ComboBox** 物件的變數。expression A variable that represents a ComboBox object.

註解Remarks

輸入運算式 ValidationRule 屬性設定及 ValidationText 屬性設定的文字。Enter an expression for the ValidationRule property setting and text for the ValidationText property setting. ValidationRule 屬性設定的最大長度為 2048年個字元。The maximum length for the ValidationRule property setting is 2048 characters. ValidationText 屬性設定的最大長度為 255 個字元。The maximum length for the ValidationText property setting is 255 characters.

如果控制項,您可以設定 ValidationRule 屬性為任何有效的運算式。For controls, you can set the ValidationRule property to any valid expression. 為欄位及記錄驗證規則,運算式不能包含使用者定義的函數、 範圍聚合函數或彙總函數、 Eval函數、 CurrentUser方法或表單、 查詢或資料表的參照。For field and record validation rules, the expression can't contain user-defined functions, domain aggregate or aggregate functions, the Eval function, the CurrentUser method, or references to forms, queries, or tables. 此外,欄位驗證規則不能包含其他欄位的參照。In addition, field validation rules can't contain references to other fields. 記錄,運算式可以包含該資料表中的欄位的參照。For records, expressions can include references to fields in that table.

對於資料表欄位及記錄,您也可以設定這些屬性在 Visual Basic 中使用 DAO ValidationRule 屬性。For table fields and records, you can also set these properties in Visual Basic by using the DAO ValidationRule property.

Microsoft Access 會自動驗證欄位的資料類型; 所依據的值例如,Access 不允許數字欄位中的文字。Microsoft Access automatically validates values based on a field's data type; for example, Access doesn't allow text in a numeric field. 您可以設定使用 ValidationRule 屬性更具體的規則。You can set rules that are more specific by using the ValidationRule property.

如果您將ValidationRule屬性,但不是ValidationText屬性,Access 會顯示標準錯誤訊息時違反驗證規則。If you set the ValidationRule property but not the ValidationText property, Access displays a standard error message when the validation rule is violated. 如果您將ValidationText屬性的設定,您輸入的文字會顯示為錯誤訊息。If you set the ValidationText property, the text that you enter is displayed as the error message.

例如,當新進員工新增一筆記錄,您可以輸入要求該員工的StartDate欄位的值,該公司的機構及目前的日期之間的ValidationRule屬性。For example, when a record is added for a new employee, you can enter a ValidationRule property requiring that the value in the employee's StartDate field fall between the company's founding date and the current date. 如果輸入的日期不在此範圍,您可以顯示 ValidationText 屬性訊息: 「 開始日期是不正確 」。If the date entered isn't in this range, you can display the ValidationText property message: "Start date is incorrect."

如果您從欄位清單拖曳欄位來建立控制項,該欄位的驗證規則作用中,雖然仍然不會顯示在控制項的ValidationRule屬性方塊中的屬性表上。If you create a control by dragging a field from the field list, the field's validation rule remains in effect, although it isn't displayed in the control's ValidationRule property box on the property sheet. 這是因為控制項繫結至該欄位的欄位的驗證規則繼承而來。This is because a field's validation rule is inherited by a control bound to that field.

控制項、欄位及記錄驗證規則的套用方式如下:Control, field, and record validation rules are applied as follows:

  • 當您編輯該資料且焦點離開欄位或控制項時,會套用您為欄位及控制項的驗證規則。Validation rules that you set for fields and controls are applied when you edit the data and the focus leaves the field or control.

  • 在移至另一筆記錄時會套用記錄的驗證規則。Validation rules for records are applied when you move to another record.

  • 如果為欄位及與該欄位結合的控制項都建立了驗證規則,則在編輯資料及焦點離開控制項時會套用這兩個驗證規則。If you create validation rules for both a field and a control bound to the field, both validation rules are applied when you edit data and the focus leaves the control.

下表包含了 ValidationRuleValidationText 屬性的運算式範例。The following table contains expression examples for the ValidationRule and ValidationText properties.


ValidationRule 屬性ValidationRule property ValidationText 屬性ValidationText property
<> 0 輸入項必須是非零的數值Entry must be a nonzero value.
> 1000 Or Is Null 輸入項必須是空白或大於 1000Entry must be blank or greater than 1000.
Like "A????" 項目必須是 5 個字元,並以字母A開頭。Entry must be 5 characters and begin with the letter A.
>= #1/1/96# And <#1/1/97# 輸入項必須是 1996 年中的日期Entry must be a date in 1996.
DLookup("CustomerID", "Customers", "CustomerID = Forms!Customers!CustomerID") Is Null 輸入項必須是唯一的 CustomerID (在表單層級的驗證之下才允許使用範圍彙總函數)Entry must be a unique CustomerID (domain aggregate functions are allowed only for form-level validation).

如果您建立欄位的驗證規則,Access 通常不允許Null值會儲存在欄位中。If you create a validation rule for a field, Access doesn't normally allow a Null value to be stored in the field. 如果您想要允許Null值,新增"Is Null"的驗證規則,如下所示"<> 8 Or Is Null",並確定 [必要] 屬性設為 [否]。If you want to allow a Null value, add "Is Null" to the validation rule, as in "<> 8 Or Is Null" and make sure that the Required property is set to No.

您無法設定欄位] 或 [外部存取 (例如,dBASE、 Paradox 或 SQL Server) 所建立的資料表的記錄驗證規則。You can't set field or record validation rules for tables created outside Access (for example, dBASE, Paradox, or SQL Server). 對於這類的資料表,只能為控制項建立驗證規則。For these kinds of tables, you can create validation rules for controls only.

範例Example

下列範例會為欄位建立驗證規則,使其僅允許輸入大於 65 以上的數值。The following example creates a validation rule for a field that allows only values over 65 to be entered. 如果輸入小於 65 的數字便會顯示一個訊息。If a number less than 65 is entered, a message is displayed. 屬性會設定使用SetFieldValidation函數。The properties are set by using the SetFieldValidation function.

Dim strTblName As String, strFldName As String 
Dim strValidRule As String 
Dim strValidText As String, intX As Integer 
 
strTblName = "Customers" 
strFldName = "Age" 
strValidRule = ">= 65" 
strValidText = "Enter a number greater than or equal to 65." 
intX = SetFieldValidation(strTblName, strFldName, _ 
 strValidRule, strValidText) 
 
Function SetFieldValidation(strTblName As String, _ 
 strFldName As String, strValidRule As String, _ 
 strValidText As String) As Integer 
 
 Dim dbs As Database, tdf As TableDef, fld As Field 
 
 Set dbs = CurrentDb 
 Set tdf = dbs.TableDefs(strTblName) 
 Set fld = tdf.Fields(strFldName) 
 fld.ValidationRule = strValidRule 
 fld.ValidationText = strValidText 
End Function

下列範例會使用SetTableValidation函數來設定記錄層級驗證,以確保 [結束日期] 欄位中的值是之後StartDate欄位的值。The following example uses the SetTableValidation function to set record-level validation to ensure that the value in the EndDate field comes after the value in the StartDate field.

Dim strTblName As String, strValidRule As String 
Dim strValidText As String 
Dim intX As Integer 
 
strTblName = "Employees" 
strValidRule = "EndDate > StartDate" 
strValidText = "Enter an EndDate that is later than the StartDate." 
intX = SetTableValidation(strTblName, strValidRule, strValidText) 
 
Function SetTableValidation(strTblName As String, _ 
 strValidRule As String, strValidText As String) _ 
 As Integer 
 
 Dim dbs As Database, tdf As TableDef 
 
 Set dbs = CurrentDb 
 Set tdf = dbs.TableDefs(strTblName) 
 tdf.ValidationRule = strValidRule 
 tdf.ValidationText = strValidText 
End Function

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.