Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118

    Run Time Error 2185: You Can't Reference A Property for a Control Unless the Control Has the Focus

    Hi, folks. I have a form in which the AllowAdditions property is set to false.

    On same form, I also have 3 unbound text boxes in the header which are used as search boxes- they allow users to filter as they type.



    The problem is, if the user types something into one of the search boxes that returns zero records, it triggers "Run Time Error 2185: You Can't Reference A Property or Method for a Control Unless the Control Has the Focus." This occurs even though the search box control DOES have the focus.

    Here's the code for one of the search boxes, named FilterFirstName:

    Code:
    If Trim(FilterFirstName.Text & "") = vbNullString And IsNull(FilterLastName) And IsNull(FilterPhones) Then
        Me.Filter = ""
        Me.FilterOn = False
        Me.FilterFirstName.SetFocus
    Else
        Me.Filter = "FirstName Like '*" & FilterFirstName.Text & "*'" & _
                    " And LastName Like '*" & FilterLastName & "*'" & _
                    " And Phones Like '*" & FilterPhones & "*'"
        Me.FilterOn = True
        Me.FilterFirstName.SelStart = FilterFirstName.SelLength
    End If
    The line that triggers the error is:

    Code:
    Me.FilterFirstName.SelStart = FilterFirstName.SelLength
    And when the users types another letter in the text box or deletes a letter, the error triggers earlier, at this line:

    Code:
    If Trim(FilterFirstName.Text & "") = vbNullString And IsNull(FilterLastName) And IsNull(FilterPhones) Then
    Any ideas for how to fix this? Thanks so much!

    Matt

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Where is the code, i.e. in what event?

  3. #3
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Ah, good question. It's in the control's On Change event.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,398
    try swapping

    Me.FilterOn = True
    Me.FilterFirstName.SelStart = FilterFirstName.SelLength

    around - I think the filteron will reset focus to the first control on the form per the tab order - as SelStart/SelLength are only available when the control has the focus

  5. #5
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Thanks for the response. Unfortunately, that only corrects the problem for the first letter entered into the box.

    If the 2nd letter or any subsequent letter results in no filter, then the error message still pops up.

    Do you have any other suggestions?

    Thanks again.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You must have some corruption going on.

    I created a table and a form. Used your code in the change event of the unbound first name text box.
    I do not get any error messages.
    Attached Files Attached Files

  7. #7
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Thanks very much, Steve. I opened your file and tried it out. And actually, it does produce the error message! When I type a letter into the FilterFirstName box that produces no results, like x or q, I get the error message. You're not getting the same message?

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You're not getting the same message?
    I was going to say I wasn't getting error messages, but today I am!

    As a test try this:
    Open the "ChangeEvent1" dB.
    Now open the IDE (ctl-G).
    Enter a letter in First name ("z").
    Do you still get an error? (I didn't!)


    Try "ChangeEvent2" (attached).
    Just open the dB and enter"z" into the first name.
    Attached Files Attached Files

  9. #9
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I think I have solved it. In your sample database ssanfu, I didn't get the error message. I changed the form to AllowAdditions = No, entered an "x" in the search, voila, got the error. changed it back to AllowAdditions = Yes, no error message.

    It's because when AllowAdditions = No, and you enter a filter that results in no records, a blank new record is not shown, thus there are no controls that can get the focus.

    MatthewR, you can get around that by putting an error handler in the procedure to trap the error and display a message box with "No records match the filter criteria" or similar.

  10. #10
    EJC99 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    California
    Posts
    8

    AOMEI Backupper

    AOMEI Backupper may be the simplest FREE backup software with system/disk/partition/file backups and system/disk clone. http://www.backup-utility.com I'm going to try it now!

  11. #11
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Sorry, Steve. I tried doing what you told me for ChangeEvent1 and ChangeEvent2, but alas, I'm still getting the error message.

    Why do you suppose I'm getting a different result?

  12. #12
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Thanks very much for the response, John. Yeah, that's a pretty good idea. However, I'd rather not make my users deal with a message box every time they type the wrong thing in the box. They'll be using those search boxes a lot, so all the message boxes could potentially get annoying. Nevertheless, perhaps I'll try that if I have no other solution.

  13. #13
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Why do you suppose I'm getting a different result?
    I am at a loss at this point.....
    Maybe because our confusers are so different in configuration???????

    I am not get any error messages using "ChangeEvent2" dB. Tried opening the dB 5 times and still no error message.

  14. #14
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Matthew -

    You don't need to put a message box in if you don't want to - you can just trap the error and exit. You are right - users should realize that they made a mistake if they don't see any results.

    But there's always one, isn't there, who will tell you "But I know there are matching records...."

  15. #15
    MatthewR is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    118
    Ah, okay. I see why you’re not getting the error message. It’s because you have Allow Additions property set to “Yes.” But what I’m trying to do is get this code to work with the Allow Additions property set to “No.” If you change that property on your file, you’ll see the problem I’m dealing with. Any suggestions on how to fix that?

    As to why we were also getting the error intermittently on your file, even with the AllowAdditions property set to Yes- if you download that file and use it without saving it, you’ll trigger the error. But if you save the file, the error stops.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Runtime error 2110, cannot move focus to control
    By sanderson in forum Programming
    Replies: 7
    Last Post: 11-17-2015, 08:00 AM
  2. Replies: 1
    Last Post: 10-06-2015, 06:50 AM
  3. Tab Control Out of Focus - HELP!
    By derek7467 in forum Forms
    Replies: 2
    Last Post: 02-13-2014, 01:38 PM
  4. Replies: 5
    Last Post: 10-13-2011, 03:36 PM
  5. Replies: 4
    Last Post: 08-02-2010, 06:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums