BaldyWeb

The Difference Between the Value and Text properties

What is the difference between .Text and .Value?

When trying to retrieve the contents of a Text Box, a common attempt is something like:
Code:
variable = textboxcontrol.Text
This is probably a bad idea. In fact, when you want the data in a control, the vast majority of the time you want the .Value property, not the .Text property.

Let's examine a few of the issues with the above code.

1. If textboxcontrol does not have focus, run-time error 2185 is raised. This can be avoided by explicitly setting the focus before attempting to get the .Text property, but such efforts are not required with the .Value property.

2. You can't be sure if textboxcontrol is Null or not. Because the .Text property is of type String it can not contain Null, even if the control is Null - in contrast, since .Value is of type Variant, you can distinguish between a Null and a ZLS value in the control.

3. The .Text property must be explicitly specified. However, since the .Value property is the default property of a control, it is not required. This is not a positive or negative point, just informational - some people like to explicitly specify .Value even if it is not required.

The main difference between the .Text and the .Value is that the former contains the current control data, and the latter contains the last saved control data.  This means that often the two properties return the same value. In those cases where they do return the same value, .Value is preferable because it does not have the issues listed earlier.

Following this thinking, the only time the .Text property is required is when one needs to deal with what a control currently contains, rather than the saved data, such as examining what the user is typing, keystroke by keystroke.

JasonM

Home