Share via

Runtime error 2185 on SelStart object which previously worked

Anonymous
2013-03-27T01:45:04+00:00

I am encountering a Runtime error 2185 You can't reference a property or method for a control unless the control has focus after typing a filter in a combobox which returns no records that is running the following code:

Private Sub cboFilter_Change()

If Me.cboFilter = "" Then

Me.Form.Filter = ""

Me.FilterOn = False

ElseIf Me.cboFilter.ListIndex <> -1 Then

Me.Form.Filter = "[Lastname] = '" & _

Replace(Me.cboFilter.Text, "'", "''") & "'"

Me.FilterOn = True

Else

Me.Form.Filter = "[Lastname] Like '*" & _

Replace(Me.cboFilter.Text, "'", "''") & "*'"

Me.FilterOn = True

End If

Me.cboFilter.SetFocus

Me.cboFilter.SelStart = Nz(Len(Me.cboFilter))

End Sub

The line with the .Selstart object is the one highlighted on Debug. I am only baffled as I have already implemented this code on multiple occasions on different Access databases before and it is only now that I encountered this error. Before, typing in text that will result in no records will do just that - show no records. Now, it only results in this error.

Perhaps I have seen in this method in this post in the Access blog. And found in the comments below is a problem exactly the same as mine, but the author did not reply. I do not want to make a workaround. I want to understand why this is happening now, but not before.

Please help!

Ome

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-03-28T01:34:02+00:00

Well, after much playing, including recreating the form I found what appears to be the issue.  You query has calculations which is *snatching* the focus to complete.  Once I removed the calculations from the query it behaves as it should... no errors.

Was this answer helpful?

0 comments No comments

24 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-03-27T04:21:19+00:00

    Me.cboFilter.SelStart = Nz(Len(Me.cboFilter.Text))

    I understand what you mean by the .SetFocus being unnecessary. But this used to be my original code. After searching and searching in forums, I decided to omit the .Text property. Neither works.

    Again, I have used the EXACT code before. On almost identical split forms. Only now do I experience this runtime error.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-03-27T03:52:53+00:00

    That line and all others that refer to cboFilter' value need to use the Text property because the code is in the Change event.

    The SetFocus is unecessary beause the focus must be in the cboFilter if the Change event is triggered and the use of the Form property after Me is redundant because Me is the form object.

    Private Sub cboFilter_Change()

    If Me.cboFilter.Text = "" Then

    Me.Filter = ""

    Me.FilterOn = False

    ElseIf Me.cboFilter.ListIndex <> -1 Then

    Me.Filter = "[Lastname] = '" & _

    Replace(Me.cboFilter.Text, "'", "''") & "' "

    Me.FilterOn = True

    Else

    Me.Filter = "[Lastname] Like '*" & _

    Replace(Me.cboFilter.Text, "'", "''") & "*' "

    Me.FilterOn = True

    End If

    Me.cboFilter.SelStart = Nz(Len(Me.cboFilter.Text))

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-03-27T02:36:19+00:00

    Have you always had the line before the .SelStart remmed out?  You need to get the focus back to the control.

    That was a mistake. I didn't mean to make it a remark. But regardless whether its "remmed" or not, it doesn't work.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2013-03-27T02:01:43+00:00

    Have you always had the line before the .SelStart remmed out?  You need to get the focus back to the control.

    Was this answer helpful?

    0 comments No comments