Table of contents
TOC
折叠目录
展开目录

ComboBox.ValidationText 属性 (访问)ComboBox.ValidationText Property (Access)

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

使用有效性文本属性来指定要输入数据时向用户显示违反记录、 字段或控件的有效性规则设置的消息。读/写,字符串Use the ValidationText property to specify a message to be displayed to the user when data is entered that violates a ValidationRule setting for a record, field, or control. Read/write String.

语法Syntax

表达式有效性文本expression. ValidationText

表达式_表示组合框对象变量。_expression A variable that represents a ComboBox object.

注解Remarks

输入将有效性规则属性设置为的表达式,并将有效性文本属性设置为文本。将有效性规则属性设置为的最大长度为 2048年个字符。将有效性文本属性设置为的最大长度为 255 个字符。Enter an expression for the ValidationRule property setting and text for the ValidationText property setting. The maximum length for the ValidationRule property setting is 2048 characters. The maximum length for the ValidationText property setting is 255 characters.

对于控件,可以为任何有效的表达式中设置有效性规则属性。对于字段和记录的有效性规则,表达式不能包含用户定义的函数、 域聚合的聚合函数, Eval函数或CurrentUser方法或对窗体、 查询或表的引用。此外,字段的有效性规则不能包含对其他域的引用。记录,表达式可以包括该表中的字段的引用。For controls, you can set the ValidationRule property to any valid expression. For field and record validation rules, the expression can't contain user-defined functions, domain aggregate or aggregate functions, the Eval function, or 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有效性规则属性。For table fields and records, you can also set these properties in Visual Basic by using the DAO ValidationRule property.

Microsoft Access 将自动验证值根据字段的数据类型;例如,Microsoft Access 不允许数字字段中的文本。您可以设置通过使用有效性规则属性是更具体的规则。Microsoft Access automatically validates values based on a field's data type; for example, Microsoft Access doesn't allow text in a numeric field. You can set rules that are more specific by using the ValidationRule property.

如果设置了有效性规则属性,但不是有效性文本属性,Microsoft Access 将违反有效性规则时显示标准错误消息。如果设置了有效性文本属性,输入的文本显示为错误消息。If you set the ValidationRule property but not the ValidationText property, Microsoft Access displays a standard error message when the validation rule is violated. If you set the ValidationText property, the text you enter is displayed as the error message.

例如,当添加新雇员记录,您可以输入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. If the date entered isn't in this range, you can display the ValidationText property message: "Start date is incorrect."

如果通过从字段列表中拖动字段创建控件时,该字段的有效性规则仍然有效,尽管它不在属性表中的控件的有效性规则属性框中显示。这是因为绑定到该字段的控件将继承字段的有效性规则。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 in 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 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.

下表包含了有效性规则有效性文本属性的表达式示例。The following table contains expression examples for the ValidationRule and ValidationText properties.

有效性规则属性ValidationRule property有效性文本属性ValidationText property
<> 0<> 0输入项必须是非零值。Entry must be a nonzero value.
> 1000 Or Is Null> 1000 Or Is Null输入项必须为空或大于 1000。Entry must be blank or greater than 1000.
Like "A????"Like "A????"输入项必须是 5 个字符并以字母 A 开头。Entry must be 5 characters and begin with the letter "A".
>= #1/1/96# And <#1/1/97#>= #1/1/96# And <#1/1/97#输入项必须是 1996 年中的日期。Entry must be a date in 1996.
DLookup("CustomerID", "Customers", "CustomerID = Forms!Customers!CustomerID") Is NullDLookup("CustomerID", "Customers", "CustomerID = Forms!Customers!CustomerID") Is Null输入项必须是唯一的“客户ID”(域聚合函数只允许在窗体级的有效性中使用)。Entry must be a unique CustomerID (domain aggregate functions are allowed only for form-level validation).

如果您创建有效性规则的字段,Microsoft Access 通常不允许存储在字段中的Null值。如果您想要允许值,添加"Is Null"有效性规则,如"<> 8 或为空",确保必填属性设置为 no。If you create a validation rule for a field, Microsoft Access doesn't normally allow a Null value to be stored in the field. If you want to allow a Null value, add "Is Null" to the validation rule, as in "<> 8 Or Is Null" and make sure the Required property is set to No.

在非 Microsoft Access(例如,dBASE、Paradox 或 SQL Server)创建的表中,不能为其字段或记录设置有效性规则。对于这些类型的表,只能为控件创建有效性规则。You can't set field or record validation rules for tables created outside Microsoft Access (for example, dBASE, Paradox, or SQL Server). For these kinds of tables, you can create validation rules for controls only.

示例Example

下面的示例为某个字段创建了一个有效性规则,只允许输入大于 65 的数值。如果输入了一个小于 65 的数字,将显示一条消息。这些属性是通过 SetFieldValidation 函数设置的。The following example creates a validation rule for a field that allows only values over 65 to be entered. If a number less than 65 is entered, a message is displayed. 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 函数来设置记录级的有效性规则,以确保“结束日期”字段中的值比“开始日期”字段中的值晚。The next 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

另请参阅See also

概念Concepts

组合框对象ComboBox Object

© 2018 Microsoft