在 VBA Access 中使用 .text 和 .value 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2844193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Distinction between using .text and .value in VBA Access
提问by l--''''''---------''''''''''''
I am passing the textbox1.textvalues into a query and sometimes into a string:
我将textbox1.text值传递给查询,有时传递给字符串:
Dim combor1 As String
combor1 = comboReason1.Text
How do I know when I should put combor1 = comboReason1.Value?
我怎么知道什么时候应该放combor1 = comboReason1.Value?
Also, why do I need to set focus for a control to reference its property? That doesn't make sense to me.
另外,为什么我需要为控件设置焦点以引用其属性?这对我来说没有意义。
Also, when I set combor4 = comboReason4.Valueand the .valueis null, then I get an error about invalid use of null.
此外,当我设置combor4 = comboReason4.Value并且.value为空时,我会收到关于无效使用空值的错误。
回答by Philippe Grondier
- ".text" gives you what is displayed on the screen
- ".value" gives you the underlying value
- “.text”为您提供屏幕上显示的内容
- “.value”为您提供潜在价值
Both usually give the same result, except when the corresponding control is
两者通常给出相同的结果,除非相应的控制是
- a combobox or listbox control
- the displayed value differs from the bound column
- 组合框或列表框控件
- 显示值与绑定列不同
Example:
例子:
- id_Person is a combobox control in a form
- the rowsource is "SELECT id_Person, personName FROM Tbl_Person"
- column widths are "0cm;3cm"
- bound column is 1
- id_Person 是表单中的组合框控件
- 行源是“SELECT id_Person, personName FROM Tbl_Person”
- 列宽为“0cm;3cm”
- 绑定列是 1
In this situation:
在这种情况下:
- id_Person.text displays Tbl_Person.personName
- id_Person.value displays Tbl_Person.id_Person.
- id_Person.text 显示 Tbl_Person.personName
- id_Person.value 显示 Tbl_Person.id_Person。
.text property is available only when the corresponding control has the focus.
.text 属性仅在相应控件具有焦点时可用。
.text is a string value, therefore it cannot be Null, while .value can be Null
.text 是一个字符串值,因此它不能为 Null,而 .value 可以为 Null
EDIT: .text can only be called when the control has the focus, while .value can be called any time ...
编辑: .text 只能在控件具有焦点时调用,而 .value 可以随时调用......
回答by Robert Harvey
You can use the Textproperty to set or return the text contained in a text box or in the text box portion of a combo box.
您可以使用该Text属性来设置或返回文本框或组合框的文本框部分中包含的文本。
To set or return a control's Textproperty, the control must have the focus, or an error occurs. To move the focus to a control, you can use the SetFocus method or GoToControl action.
要设置或返回控件的Text属性,控件必须具有焦点,否则会发生错误。要将焦点移到控件上,可以使用 SetFocus 方法或 GoToControl 操作。
You can use the Valueproperty to determine or specify if a control is selected, the selected value or option within the control, the text contained in a text box control, or the value of a custom property.
您可以使用该Value属性来确定或指定是否选择了控件、控件中的选定值或选项、文本框控件中包含的文本或自定义属性的值。
The Valueproperty returns or sets a control's default property, which is the property that is assumed when you don't explicitly specify a property name. In the following example, because the default value of the text box is the value of the Text property, you can refer to its Text property setting without explicitly specifying the name of the property.
该Value属性返回或设置控件的默认属性,即未明确指定属性名称时假定的属性。在下面的示例中,由于文本框的默认值是 Text 属性的值,因此您可以在不明确指定属性名称的情况下引用其 Text 属性设置。
Forms!frmCustomers!txtLastName = "Smith"
Text Property Reference
http://msdn.microsoft.com/en-us/library/aa173453.aspx
文本属性参考
http://msdn.microsoft.com/en-us/library/aa173453.aspx
Value Property Reference
http://msdn.microsoft.com/en-us/library/aa173476.aspx
回答by Res
.textstarts the field validation and causes an error if field validation is hurt. .valuedoesn't start the field validation, you may enter ANY value
.text启动字段验证并在字段验证受损时导致错误。.value不启动字段验证,您可以输入任何值
回答by spinjector
This thread and the answers herein explain the issue well. There are a couple of additional points I'd like to add, which I've found through experimentation:
这个线程和这里的答案很好地解释了这个问题。我想补充几点,这是我通过实验发现的:
The order of precedence of the properties is:
属性的优先顺序是:
.ControlSource.Value.Text
.ControlSource.Value.Text
From what I've been seeing in Access 2007, if .ControlSourceis undefined when the form opens, .Valuewill be Null.
从我在 Access 2007 中看到的情况来看,如果.ControlSource表单打开时未定义,.Value则Null.
If you set the .ControlSourceproperty to =""(an empty string), that will cause the .Valueproperty to default to that instead of Null.
如果您将该.ControlSource属性设置为=""(空字符串),这将导致该.Value属性默认为该属性而不是Null.
You can set the .Valueproperty to ""in the Form_Loadevent. But...I've been seeing some erratic operation there; it seems as if .Valuesometimes changes from ""back to Null, and I haven't yet worked out the circumstances.
您可以在事件中将该.Value属性设置为。但是……我在那里看到了一些不稳定的操作;似乎有时会从back 变为,而我还没有弄清楚情况。""Form_Load.Value""Null
So it seems best to define .ControlSourceto ="", either in Design View or in the Form_Loadevent. But be forewarned, that niblet is tricky because of the embedded double quotes, and it can be tricky to read.
所以最好在设计视图或事件中定义.ControlSourceto 。但要预先警告,由于嵌入了双引号,niblet 很棘手,而且阅读起来可能很棘手。=""Form_Load
Some ways to do it are:
一些方法是:
- myTextbox.ControlSource = "=" & """"" (five double quotes in a row)
- myTextbox.ControlSource = "=" & Chr(34) & Chr(34)
- Etc, etc, there are many ways to do it...
- myTextbox.ControlSource = "=" & """""(连续五个双引号)
- myTextbox.ControlSource = "=" & Chr(34) & Chr(34)
- 等等等等,有很多方法可以做到......
Also, here's an extended tidbit. If you set the .TextFormatproperty to Rich Text, you can format the text in it with bold, italic, colors, etc. But be forewarned (again), beginning with Office 2007, the original Microsoft RTF format was decommissioned in favor of a "mini" version of HTML that only supports a few tags related to formatting fonts and paragraphs.
另外,这是一个扩展的花絮。如果将该.TextFormat属性设置为Rich Text,则可以使用粗体、斜体、颜色等来格式化其中的文本。但是(再次)预先警告,从 Office 2007 开始,原始 Microsoft RTF 格式已退役,取而代之的是“迷你”版本仅支持少数与格式化字体和段落相关的标签的 HTML。
As an example, say you want the textbox to display the little ASCII checkbox character with the word "valid" in italics next to it, and make it all green. You can do it, but it all has to be in HTML, and it's not easy to read:
例如,假设您希望文本框显示旁边带有斜体字“valid”字样的小 ASCII 复选框字符,并将其全部设为绿色。你可以做到,但是这一切都必须在 HTML 中,而且不容易阅读:
myTextbox.TextFormat = acTextFormatHTMLRichText
myTextbox.ControlSource = "=" & Chr(34) & "<font color=#80CA45><font face=Wingdings>" & _
Chr(254) & "</font> <font face=Calibri><i>Valid.</i></font></font>" & Chr(34)
回答by Sofia Khwaja
If the text box is a ReadOnly control, the value property will not be used but if you set the text peoprty, the value will still be used in form data.
如果文本框是只读控件,则不会使用 value 属性,但如果您设置了 text peoprty,该值仍将用于表单数据。

