Sunday, July 28, 2019

Excel VBA- Value property

By Ajeet Kumar
In this article, we will learn about the Value property of different controls used on a user form.
Value property of a control is one of the most used property of a control but in a few cases, due to some ignorance or negligence, developer may err in the coding. The purpose of this article is make aware about such cases.
Value is read write property of a control. We can get or let the value of the control.
Examples:
Msgbox TextBox1.Value
TextBox1.Value = "Enter amount."
Since Value is the default property of text box control, we can also write the above code as
Msgbox TextBox1
TextBox1 = "Enter amount."
Value of TextBox
To find the value of a text box, we use Value property.
Also, we can use Text property. Value and Text  properties are almost equivalent to get the value of a text box.
MsgBox TextBox1.Value
MsgBox TextBox1.Text
Difference between Text and Value is explained in another article.
Value of OptionButton
To find the value of a OptionButton, we use Value property but we must take some precaution before using it.
Option button has TripleState property. If this property is true, option button can return true, false or null value. When a option button is clicked or chosen, it will return true value. When it is de selected, it'll return false value but when it's grayed out, it'll return null value.
We cannot use following statement if the TripleState of option button is true.
Example: if optionbutton1.Value=null then ... End If
Reason: in case of TripleState=true, the l.h.s of the expression will return null value, if the optionbutton1 is grayed out. Thus expression null =null becomes unknown. So, it'll throw error.
Value of ToggleButton
Value is the default property of toggle button. The value of toggle button depends upon its TripleState property. TripleState is boolean property. If it's true, the Value property will return true, false or null, if it's false, Value property will return true or false.
To find the value of a ToggleButton, we use Value property but we must take some precaution before using it.
Toggle button has TripleState property. If this property is true, option button can return true, false or null value. When a toggle button is clicked or chosen, it will return true value. When it is de selected, it'll return false value but when it's grayed out, it'll return null value.
We cannot use following statement if the TripleState of toggle button is true.
Example: if ToggleButton1.Value=null then ...
Reason: in case of triplestate=true, the l.h.s of the expression will return null value, if the ToggleButton1 is grayed out. Thus expression null =null becomes unknown. So, it'll throw error.
Value of CheckBox
To find the value of a CheckBox, we use Value property but we take some precaution before using it. CheckBox has TripleState property. If this property is true, CheckBox can return true, false or null value. When a CheckBox is clicked or chosen, it will return true value. When it is de selected, it'll return false value but when it's grayed out, it'll return null value.
We cannot use following statement if the triplestate of CheckBox is true.
Example: if CheckBox1.Value=null then
Reason: in case of TripleState=true, the l.h.s of the expression will return null value, if the CheckBox1 is grayed out. Thus expression null =null becomes unknown. So, it'll throw error.
To find the value of a text box, we use Value property.
Value of ListBox
To find the value of a list box, we use Value property but we must take some precaution before using it. A list box allows user to select one or more items from the list. If MultiSelect property of list box is set to allow multiple selection, we cannot use Value property of list box to find the value of the list box. In this case, Value property will return null value.
In case of multiple selection, we should use Selected property which determines which items of the list are selected in the list box.
Value of ComboBox
To find the value of a combobox, we use Value property. There is no question of selection of multiple items from a combo box unlike list box.
Return Value of Label
To find the value of a Label, we use Value property.

No comments:

Post a Comment

Hot Topics