Access error 2185 after calling SetFocus and attempting to use the Text property of a Combobox

G

Guest

Hi All,

I am attempting to obtain the Text value of a combobox control in the
footer of an Access form.

As Access does not allow you to obtain these values without first
having "focus", my code looks like this:

Private Sub btnSetValue_Click()
Dim s As String
cboQuarter.SetFocus
s = cboQuarter.Text
MsgBox s
End Sub

The problem is that error "2185 - You can't reference a property or
method for a control unless the control has focus" occurs every time on
the line: "s = cboQuarter.Text"

I am doing this exact same thing in a differant form with no problem.

Any ideas?

Thanks

David
 
A

Allen Browne

David, can you give more details about the case where this error is thrown?

One known case is where the form has no records to display, and no new
records can be added. In this case, the entire detail section of the form
goes blank. You can still see the controls in the Form Header and Form
Footer section, but Access is unable to figure out what to do with them in
this case. More info an example:
Incorrect display of data
at:
http://allenbrowne.com/bug-06.html

If that is the issue, and the only reason the form cannot show the new
record is that you set its AllowAdditions property to No, you can work
around the problem by setting AllowAdditions to Yes and adding this line to
the form's BeforeInsert event procedure:
Cancel = True
That blocks the new record, but averts the display problems.
 
F

fredg

Hi All,

I am attempting to obtain the Text value of a combobox control in the
footer of an Access form.

As Access does not allow you to obtain these values without first
having "focus", my code looks like this:

Private Sub btnSetValue_Click()
Dim s As String
cboQuarter.SetFocus
s = cboQuarter.Text
MsgBox s
End Sub

The problem is that error "2185 - You can't reference a property or
method for a control unless the control has focus" occurs every time on
the line: "s = cboQuarter.Text"

I am doing this exact same thing in a differant form with no problem.

Any ideas?

Thanks

David

From Accdess help:

"While the control has the focus, the Text property contains the text
data currently in the control; the Value property contains the last
saved data for the control. When you move the focus to another
control, the control's data is updated, and the Value property is set
to this new value."

So, once you have left the combo box control it's the control's Value
you would want to read, not it's Text property.
Because the Value property is a control's default, you do not need to
expressly write it.

Private Sub btnSetValue_Click()
Dim s As String
s = cboQuarter
MsgBox s
End Sub
 
G

Guest

Hi Allen,

Thank you so much for your prompt response!

You are spot on.

The allow additions property on the form was set to No and the record
set was empty due to a filter being applied while this problem was
occuring (Its amazing that you figured this out?!).

I have made the adjustments as you suggested and everything is working
fine.

Your help was appreciated.

David
 

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