Why am I Getting Error 2185?

D

Dan

Why am getting "Error 2185: You can't reference a property or method for
a control unless the control has the focus."? My code which is saving
changes simply has this reference, rsRatings("Rating") = txtRating.Text.

Dan
 
D

Dan Artuso

Hi,
Do not use the Text property (the conrol has to have the focus).
Use the Value property which happens to be the default so...
rsRatings("Rating") = txtRating

HTH
Dan Artuso, MVP
 
D

Dirk Goldgar

Dan said:
Why am getting "Error 2185: You can't reference a property or method
for a control unless the control has the focus."? My code which is
saving changes simply has this reference, rsRatings("Rating") =
txtRating.Text.

Presumably the control named txtRating doesn't have the focus. Unlike a
VB text box, where the Text property is where the data in the control is
stored and is available at all times, an Access text box keeps its data
in the Value property. Although it has a Text property, that has only
specialized uses and is only available when the control has the focus.
By contrast, the Value property is available at all times and is the
property you should use for most purposes. Because the Value property
is the default property of an Access text box, you can write

rsRatings("Rating") = txtRating.Value

or

rsRatings("Rating") = txtRating

with equal validity. I prefer to prefix control names with the "Me"
qualifier, just to ensure no confusion can occur:

rsRatings("Rating") = Me.txtRating

However, that's not generally required.
 
D

david epsom dot com dot au

By contrast, the Value property is available at all times

Except when the control is bound to a missing field. The value
property then just disappears. Perhaps the value property of
the control object is replaced with the value property of the
field object when you have a bound control?

(david)
 
D

Dirk Goldgar

david epsom dot com dot au said:
Except when the control is bound to a missing field. The value
property then just disappears. Perhaps the value property of
the control object is replaced with the value property of the
field object when you have a bound control?

Maybe I should have said, "at all non-pathological times". But I'm not
sure exactly what you mean by "the value property then just disappears."
The message, if you try to interrogate the Value property of a
"misbound" control, is something about referring to a field, control or
property that the database can't find. That makes sense, since a
reference to the value of a bound control would have to be translated
into a reference the value of the field to which it's bound. But I
don't know how this is handled under the covers. I get the impression
that the properties of the control and the field are somehow merged,.but
I don't know if this is just an illusion.
 
D

david epsom dot com dot au

If you iterate through the properties collection, the
value property is gone. I guess that's why Access can't
find it. It confused me a little bit when I went looking
for it, because I had pretty much assumed that the value
property would be available at all times: then when i
found a control without a value property i thought maybe
there was something special about the control. I guess
i would have predicted an 'error' result rather than
a 'missing' result.

(david)
 
A

Allen Browne

David, that's an interesting observation.

If a bound form has a field named Field1 in its RecordSource, and the field
is NOT represented by a control on the form, the reference:
Me.Field1
is an object of type AccessField. This type seems to have no properties
*except* Value.

Hypothesis: The Value property of a control bound to a field (not an
expression) is suppressed and replaced by this Value, which is not available
if the control is misbound.
 
D

Dirk Goldgar

david epsom dot com dot au said:
If you iterate through the properties collection, the
value property is gone. I guess that's why Access can't
find it. It confused me a little bit when I went looking
for it, because I had pretty much assumed that the value
property would be available at all times: then when i
found a control without a value property i thought maybe
there was something special about the control. I guess
i would have predicted an 'error' result rather than
a 'missing' result.

But the Value property doesn't appear in the control's Properties
collection anyway, regardless of whether it is bound or unbound. At
least, this code ...

Dim p As DAO.Property
Dim ctl As Access.Control

Set ctl = Me.Controls("ControlName")

For Each p In ctl.Properties
Debug.Print p.Name
Next p

.... never prints "Value" in the list of properties, regardless of
whether the control is bound, unbound, or misbound.
 
D

david epsom dot com dot au

But the Value property doesn't appear in the control's Properties

hmmm. I guess the properties collection was not the right place to look.
what can i say? i was confused by it's absence.

(david)
 
D

Dirk Goldgar

david epsom dot com dot au said:
hmmm. I guess the properties collection was not the right place to
look. what can i say? i was confused by it's absence.

I was surprised by its absence, when I went to look.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top