Results 1 to 13 of 13
  1. #1
    kallu is offline Novice
    Windows 8 Access 2013 32bit
    Join Date
    May 2017
    Posts
    10

    Filter as you type in multiple fields

    He there, In access form, I have below code, to filter while typing. Its working well, but if any filed is empty or null, not showing results. example: FirstName is not null, LastName is null, there is no results.

    Can somebady help me to solve this? I am new in VBA, be specific in giving sugestion.

    Code:

    Private Sub FilterFirstName_Change()
    Dim FName
    FName = FilterFirstName.Text
    If Trim(FName & "") = 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
    End Sub

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I would recommend looking at Majp's sample database's here:
    https://www.access-programmers.co.uk...x-form.327863/

    He has created classes for these things that are completely portable.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Here's the basics . . .

    Code:
    Option Compare Database
    Option Explicit
    
    Private blnSpace As Boolean
    
    Private Sub Text3_Change()
    
        Dim strFilter As String
    
        If blnSpace = False Then
        
            strFilter = "FirstName Like ""*" & Me.Text3.Text & "*"" or LastName like ""*" & Me.Text3.Text & "*"""
    
            Me.Text5 = strFilter   ' this line is just for the demo to display the filter string on the form
    
            Me.Filter = strFilter
            Me.Requery
            Refresh
            Me.Text3.SetFocus
            Me.Text3.SelStart = Len(Me.Text3.Text)
            
        End If
        
        Me.FilterOn = True
           
    End Sub
     
    Private Sub Text3_KeyPress(KeyAscii As Integer)
    
        If KeyAscii = 32 Then
            blnSpace = True
        Else
            blnSpace = False
        End If
        
    End Sub
    Here's a sample DB
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    kallu is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    10
    Thanks Moke123

    Working well to filter in one text box. I want to filter as typing from multiple search fields. example: to filter FirstName, than LastName, Phones etc. Find attached sample DB.

    FilterAsYouTypeFromMultipleSearchTextAndFileds.zip

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I think you are going to need a button for that, or something to indicate when to search.

    Otherwise you will search for Kallu Davies, then change the kallu to dimitri, but there is no dimitri davies?

    So enter your criteria, then hit a button to search.
    Test each control to see if a value is there. If so, build the sql string accordingly.
    Last edited by Welshgasman; 08-19-2023 at 08:50 AM.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    kallu is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    10
    Yes, I have code to filter by button. The reason I like the search while typing is that the search is more accurate and less time consuming. For example If you are searching by name and there is no result, search by last name and if there is a result, you can compare with the first name (there are cases when the name is registered incorrectly), to verify if it is the person you were looking for.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Search will not be more accurate?
    How can it?.
    Might be marginally quicker though.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    kallu is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    10
    If the name is entered incorrectly, and you search by first and last name, it will not give a result. Usually users search by first and last name or more fields, and the chance of a result is less due to misspelled entries.

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Personally I would remove the first * from your search string so indexes can be used. Will be much faster for larger datasets. If on the very rare occasions where your users don’t know the first letter of the name, train them to use the *

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    If the name is entered incorrectly, and you search by first and last name, it will not give a result.
    There are ways around spelling errors. There are things like Soundex and Levenschtein Distance that will calculate near matches.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    If the name is entered incorrectly, and you search by first and last name, it will not give a result.
    Shouldn't you program for "No record found" or "Name not found"? This would be a result.

    Have you done any analysis on what search term(s) the user used that gave "no result"? Seems like a key step in providing some corrective action(s) or revised training.

    You could create a small routine to record what the users' search terms were and if no result -record the term(s) for review. If the search was successful, then don't save the term(s).

  12. #12
    kallu is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    10
    I solved this thread, by populating all empty fields with "NULL", and for new records, I put default value to "NULL", to fields which I need to search.
    Thanks to everybody!

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    "Null" is a string?
    Null is Null.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 2
    Last Post: 03-01-2017, 01:48 PM
  2. Replies: 5
    Last Post: 07-26-2016, 09:37 AM
  3. changing multiple fields type to double
    By neefbeef in forum Access
    Replies: 1
    Last Post: 12-07-2014, 03:20 AM
  4. change data type for multiple fields
    By axg275 in forum Access
    Replies: 1
    Last Post: 03-11-2014, 09:31 AM
  5. Datasheet Form Filter By Multiple Fields
    By snoopy2003 in forum Programming
    Replies: 4
    Last Post: 03-14-2012, 03:22 PM

Tags for this Thread

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