Results 1 to 6 of 6
  1. #1
    Steven19 is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2020
    Location
    London
    Posts
    110

    Search Filter

    I have the following code,



    Code:
    Private Sub txtNameFilter_KeyUp(KeyCode As Integer, Shift As Integer)
    On Error GoTo errHandler
    
    Dim filterText As String
    
    'Apply or update filter based on user input
    If Len(txtNameFilter.Text) > 0 Then
     filterText = txtNameFilter.Text
       Me.Form.Filter = "[CompanyName] LIKE '*" & filterText & "*'OR [TradingCompanyName] LIKE '*" & filterText & "*'"
       Me.FilterOn = True
      'Retain filter text in search box after refresh.
       txtNameFilter.Text = filterText
       txtNameFilter.SelStart = Len(txtNameFilter.Text)
    Else
       ' Remove filter.
       Me.Filter = ""
       Me.FilterOn = False
       txtNameFilter.SetFocus
    End If
    
    Exit Sub
    
    errHandler:
    
    MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."
    
    End Sub
    However if I search for: 5 Star because there is a space I get an error, however I have tried:

    Code:
    txtNameFilter.SelStart = Len(Replace(txtNameFilter.Text, " ", "."))
    Instead of

    Code:
    txtNameFilter.SelStart = Len(txtNameFilter.Text)
    However still get Error 2185 You can't reference a property or method for a control unless the control has focus

    I have the unbound control on the form I am searching, its a continuous form which has a Query Control Source.

    Does anyone have any suggestions?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,953
    Does that code work at all?

    Need a space between apostrophe and OR: "*' OR
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,815
    the message means exactly what it says - it just doesn't report what property or method you are trying to reference because it's generic.

    Set focus to the control before trying to use its Text property. The reason you don't have to do this to get a control value is because .Value is the default. Anything else requires that you set focus to the control first, or if you can guarantee that it has the focus (because the event you're coding for makes that a sure thing) then you can also use Screen.ActiveControl.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Steven19 is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2020
    Location
    London
    Posts
    110
    Hi June

    Thanks for the space hint.

    The code searches, filters and returns results, it just doesn't allow for spaces in the user input. If you type a space, then the cursor goes back to the preceding character as though you never entered a space.


    Regards

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,815
    Access will strip leading and trailing spaces from fields if the control is bound. I suspect yours is and that you'll have to un-bind it (remove its recordsource property). If you need to have the control bound to the field, create an unbound textbox (can be hidden when you get it working but only by setting its width to 0), make it equal the text of the bound control and get your text value from this unbound control. You will want to delete the label if you do that. Note that you should remove this control from the tab order as well, otherwise a user could tab into it. Generally speaking, controls that filter or search should not be bound anyway.
    Last edited by Micron; 10-01-2021 at 01:03 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,953
    If you want 'filter as you type' functionality, review http://allenbrowne.com/AppFindAsUType.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Search Filter's
    By Dermir in forum Macros
    Replies: 3
    Last Post: 06-06-2018, 05:44 AM
  2. Search Filter Criteria Help
    By aamer in forum Access
    Replies: 3
    Last Post: 10-31-2014, 06:45 PM
  3. Better search and filter VBA
    By Dazza666 in forum Programming
    Replies: 7
    Last Post: 07-17-2013, 11:33 AM
  4. Another Search Filter Question
    By r0v3rT3N in forum Programming
    Replies: 4
    Last Post: 07-23-2012, 12:20 PM
  5. Filter Form by search
    By DCV0204 in forum Forms
    Replies: 2
    Last Post: 02-09-2012, 10:29 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