Results 1 to 10 of 10
  1. #1
    emhill57 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    May 2017
    Posts
    27

    Getting 2185 in VBA Code

    I Googled and found the code below to limit the number of characters that can be entered in an unbound textbox. I call this in the "On Change" event and "On KeyPress" event. In certain instances I get the 2185 error "You can't reference a property or method for a control unless the control has focus." I'm not sure what I'm doing wrong. I do not have a .SetFocus anywhere. Any help?

    ================================================== ======
    Sub LimitKeyPress(ctl As Control, iMaxLen As Integer, KeyAscii As Integer)
    On Error GoTo Err_LimitKeyPress
    ' Purpose: Limit the text in an unbound text box/combo.
    ' Usage: In the control's KeyPress event procedure:
    ' Call LimitKeyPress(Me.MyTextBox, 12, KeyAscii)
    ' Note: Requires LimitChange() in control's Change event also.


    If Len(ctl.Text) - ctl.SelLength >= iMaxLen Then
    If KeyAscii <> vbKeyBack Then
    KeyAscii = 0
    Beep
    End If
    End If


    Exit_LimitKeyPress:
    Exit Sub


    Err_LimitKeyPress:


    'Call LogError(Err.Number, Err.Description, "LimitKeyPress()")
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Exit_LimitKeyPress
    End Sub


    Sub LimitChange(ctl As Control, iMaxLen As Integer)
    On Error GoTo Err_LimitChange
    ' Purpose: Limit the text in an unbound text box/combo.
    ' Usage: In the control's Change event procedure:
    ' Call LimitChange(Me.MyTextBox, 12)
    ' Note: Requires LimitKeyPress() in control's KeyPress event also.


    If Len(ctl.Text) > iMaxLen Then
    MsgBox "Truncated to " & iMaxLen & " characters.", vbExclamation, "Too long"
    ctl.Text = Left(ctl.Text, iMaxLen)
    ctl.SelStart = iMaxLen
    End If


    Exit_LimitChange:
    Exit Sub


    Err_LimitChange:
    'Call LogError(Err.Number, Err.Description, "LimitChange()")
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Exit_LimitChange
    End Sub
    ================================================== ======





  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The .Text property requires focus, so if you passed it a control that didn't have focus you'd get that error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    emhill57 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    May 2017
    Posts
    27
    I have one unbound field in the header of the continuous form. It is used as a simple name filter on a form. It works just fine in that it will stop at the 30 character limit and when the filter button is clicked the form will filter correctly. The issue seems to happen when the filter text does not return any data. In that instance I click back into the textbox to re-enter another filter name and that is when I get the 2185 error. I've tried to SetFocus back to that control after the "Me.FilterOn = True" but it still gets the error. Any suggestions?

    Thanks!!!!

  4. #4
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Does the code stop and highlight a particular line? If not, change your settings in the vb editor to break on unhandled errors (Tools > Options). I suspect that if there is a break, it's not even in the code you posted, but in some other coded event for the control. I presume this
    I click back into the textbox to re-enter another filter name and that is when I get the 2185 error.
    means as soon as you click on it, and before you can type anything.

    Please use code tags for anything more than just a few lines. When code breaks and highlights a line, indicate which line it is by adding a note to your code.
    Thanks.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    emhill57 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    May 2017
    Posts
    27
    Here is the code with code tags and where my error is happening:

    Code:
    Private Sub srch_name_KeyPress(KeyAscii As Integer)
        Call LimitKeyPress(Me.srch_name, 30, KeyAscii)
    End Sub
    
    
    Sub LimitKeyPress(ctl As Control, iMaxLen As Integer, KeyAscii As Integer)
    On Error GoTo Err_LimitKeyPress
        ' Purpose:  Limit the text in an unbound text box/combo.
        ' Usage:    In the control's KeyPress event procedure:
        '             Call LimitKeyPress(Me.MyTextBox, 12, KeyAscii)
        ' Note:     Requires LimitChange() in control's Change event also.
    
    
        If Len(ctl.Text) - ctl.SelLength >= iMaxLen Then  '****Line in error****
    
    
            If KeyAscii <> vbKeyBack Then
                KeyAscii = 0
                Beep
            End If
        End If
    
    
    Exit_LimitKeyPress:
        Exit Sub
    When in debug the line I noted above is the where it stops.

  6. #6
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Looks fine to me (with an oddity or two) so I tested your code. Seems to work if I enter 30 characters, click out of the control and back in to edit using either backspace or delete. I can't replicate your search function, so all I can think of is that you have other code that is diverting focus, or runs after the button click, such as a Lost_Focus or Got_Focus event, in which case you wouldn't be able to use the .Text property as noted.

    So the oddities:
    - once you have left the textbox and click the button I see no reason to use the .Text property. As soon as you leave the control, whatever is in it has been "committed", thus the contents will become the .Value. In that case, why not just use If Len(ctl)
    - I don't see where you pass the SelLength to the sub, nor calculate it in the sub, so it's always going to be zero.

    Depending on your situation, this could be as simple as
    Code:
    Private Sub srch_name_BeforeUpdate(Cancel As Integer)
    If Len(Me.srch_name) > 30 Then
      MsgBox "Too many"
      Cancel = True
    End If
    End Sub
    or just use the control value instead of it's .Text property
    Last edited by Micron; 10-09-2017 at 09:53 AM. Reason: grammar

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Quote Originally Posted by Micron View Post
    once you have left the textbox and click the button I see no reason to use the .Text property. As soon as you leave the control, whatever is in it has been "committed", thus the contents will become the .Value. In that case, why not just use If Len(ctl)
    What button? The code is running as the user types, so the .Text property is necessary. That said, using the before update event would be a way around the problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    emhill57 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    May 2017
    Posts
    27
    That will work just great! Thanks!!!!

  9. #9
    emhill57 is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    May 2017
    Posts
    27
    By the way here is the site for the code I was trying to use to limit the keyed characters. Can you see any flaw why it would not work? Just curious now! :-)

    http://allenbrowne.com/ser-34.html

    Thanks!!!

  10. #10
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Quote Originally Posted by pbaldy View Post
    What button? The code is running as the user types, so the .Text property is necessary. That said, using the before update event would be a way around the problem.
    I took "when I press the filter button " to mean a command button. Poster probably meant ribbon button.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 15
    Last Post: 05-12-2016, 02:27 PM
  2. Replies: 20
    Last Post: 10-13-2015, 09:05 AM
  3. Replies: 3
    Last Post: 10-16-2014, 08:49 AM
  4. Replies: 7
    Last Post: 05-28-2013, 09:11 AM
  5. Replies: 1
    Last Post: 05-04-2013, 12:19 PM

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