Run-time error 2185

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I get an error when I tried to assign the value in a textbox to a variable .
the error says "You can't reference a property or method for a control unless
the control has the focus.

my code is:

Private Sub btnReserve_Click()
Dim DL As String
DL = txtDL.Text 'retriveing info from text/combo boxes

I'm using Office Access XP
 
The error is correct, you can't use the Text property unless the control has
the focus. However, you can use the Value property. If the control doesn't
have the focus, the Value and Text property should both contain the same
information if the control is a textbox, for a combo box or listbox there is
another option.

The Value property is the default property, so just removing the ".Text"
should solve your problem for a textbox. For a combo box or listbox, you
need to refer to the Column property if the box is a multi column box. The
Value will come from the Bound Column. If there is only one column, then
this will also be what is displayed in the text portion of the box. If there
is more than one column, frequently the bound column is hidden and is not
the one that is displayed. To get the displayed column, use the Column
property as show below. The index number for the Column property is zero
based, so 0 is the first column, 1 is the second, etc.
DL = txtDL.Text 'retrieving info from text/combo boxes
DL = txtDL.Column(1)

You can run into another problem as well if using a Listbox. If you set the
listbox to be a multiselect listbox, you can't just use the Value property.
For that you have to loop through the ItemsSelected collection property of
the listbox to get the index of the selected rows then use the
ItemData(index) property to get the value of each of the selected rows.
 
Back
Top