Distinction between using .text and .value in VBA 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.


Solution 1:

  • ".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 ...

Solution 2:

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