Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinction between using .text and .value in VBA Access

Tags:

vba

ms-access

I am passing the textbox1.text values into a query and sometimes into a string:

Dim combor1 As String
combor1 = comboReason1.Text

How do I know when I should put 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.Value and the .value is null, then I get an error about invalid use of null.

like image 931
Alex Gordon Avatar asked May 16 '10 14:05

Alex Gordon


People also ask

What does .value mean in VBA?

Not all VBA methods can be used with all types of references. For example, the “Value” method tells Excel to store a value in a relevant cell, or an array of values in a relevant range. If you stored a range of values in a variable, you cannot use the value method to attach that array to a single cell.

What is the difference between textbox text and textbox value?

The Text property may be different than the Value property for a text box control. The Text property is the current contents of the control. The Value property is the saved value of the text box control. The Text property is always current while the control has the focus.

What is the difference between text and values?

Text is a string. It represents the actual text the user sees on the screen. Value is an object and can be any value. The reason they are different is because you can use the Combo as a sort've lookup table.

What is the difference between value and Value2 in VBA?

The only difference between this property and the Value property is that the Value2 property doesn't use the Currency and Date data types. You can return values formatted with these data types as floating-point numbers by using the Double data type.


2 Answers

  • ".text" gives you what is displayed on the screen
  • ".value" gives you the underlying value

Both usually give the same result, except when the corresponding control is

  1. a combobox or listbox control
  2. 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

In this situation:

  • id_Person.text displays Tbl_Person.personName
  • id_Person.value displays Tbl_Person.id_Person.

.text property is available only when the corresponding control has the focus.

.text is a string value, therefore it cannot be Null, while .value can be Null

EDIT: .text can only be called when the control has the focus, while .value can be called any time ...

like image 50
Philippe Grondier Avatar answered Oct 22 '22 13:10

Philippe Grondier


You can use the Text property to set or return the text contained in a text box or in the text box portion of a combo box.

To set or return a control's Text property, 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.

You can use the Value property 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.

The Value property 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.

Forms!frmCustomers!txtLastName = "Smith"

Text Property Reference
http://msdn.microsoft.com/en-us/library/aa173453.aspx

Value Property Reference
http://msdn.microsoft.com/en-us/library/aa173476.aspx

like image 28
Robert Harvey Avatar answered Oct 22 '22 14:10

Robert Harvey