Results 1 to 14 of 14
  1. #1
    jlgray0127 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Central Illinois
    Posts
    185

    Multiple Combo Boxes to Filter a Form

    I have a form and am trying to use to combo boxes to filter a form.
    I need the form to filter by one or both combo boxes. If one is blank, then only filter by the other...
    Any assistance is greatly appreciated!

    I am not sure what I am doing incorrectly here.

    I have two combo boxes:
    [SCategory] Number Field
    [GenerateDate] Date and Time Field

    I am receiving a Run time error






    This is the Code I've adhoc'd from AllenBrown's search2000 sample database.


    Private Sub SCategory_AfterUpdate()
    If Not IsNull(Me.SCategory) Then
    strWhere = strWhere & "([SOP_Category] = ' " & Me.SCategory & " ' ) AND "
    Else
    Me.Filter = strWhere
    Me.FilterOn = True
    End If
    If Not IsNull(Me.GenerateDate) Then
    strWhere = strWhere & "([Generated] = & Me.GenerateDate & "
    Else
    Me.Filter = strWhere
    Me.FilterOn = True


    End If


    End Sub

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    where do you declare Dim strWhere as string?

    I would write it as a separate procedure. Something like (aircode)

    Code:
    Private Sub MyFilter()
    
        Dim strFilter As String
    
        If Not IsNull(Me.SCategory) Then
    
            strFilter = strFilter & " And " & "SOP_Category =""" & Me.SCategory & """"
     
        End If
    
        If Not IsNull(Me.GenerateDate) Then
    
            strFilter = strFilter & " And " & "Generated = #" & Me.GenerateDate & "#"
    
        End If
    
        strFilter = Mid(strFilter, 5)
    
        debug.print strFilter
    
    If nz(strFilter,"")<>"" then  
      
         Me.Filter = strFilter
         Me.FilterOn = True
    
    end if
    
    
    End Sub
    Then you can call it in the afterupdate of both combos
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    jlgray0127 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Central Illinois
    Posts
    185
    Thank you. I am not 100% sure I am following this.
    I do not understand the logic behind this step, if you could help me understand what the code is doing and why I would need this, I would really appreciate it!



    I'm not sure how this works.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by jlgray0127 View Post
    Thank you. I am not 100% sure I am following this.
    I do not understand the logic behind this step, if you could help me understand what the code is doing and why I would need this, I would really appreciate it!



    I'm not sure how this works.
    Have you tried stepping through the code at runtime and examining the various values?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    BTW Just wondering if the code might need amending to turn off the filter when both combos are null.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Do you want to allow filter by either or both or none?

    Review http://allenbrowne.com/ser-62.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.

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    BTW Just wondering if the code might need amending to turn off the filter when both combos are null.
    probably but that was just air code to give some guidance.

    Here's a simple example
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  8. #8
    jlgray0127 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Central Illinois
    Posts
    185
    hi June7!
    Yes, that is exactly what I am trying to accomplish. Filter either, both or none.

    Unfortunately, the code I am trying to use provided by Moke is not working. It's capturing the field names, as well as the value. We keep updating the statement but just can not seem to get it to provide the values only. I will take a look at the Allenbrowne link you provided, as well. I was attempting to modify his Search2000 code orignially, to work this. I use his samples alot! Thank you for reaching out!!!

  9. #9
    jlgray0127 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Central Illinois
    Posts
    185
    Quote Originally Posted by moke123 View Post
    probably but that was just air code to give some guidance.

    Here's a simple example
    Thank you, Moke. I downloaded this and tried it, and it is not filtering for me.
    After I update and move to Lname, nothing happens. Then when I select a value in Lname and tab out, nothing is filtering.
    I double checked all my options to ensure it was not something on my end where I was blocking the code from running, and I'm basically running allowing all code, activeX and macros to run right now.

  10. #10
    jlgray0127 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Central Illinois
    Posts
    185
    Quote Originally Posted by June7 View Post
    Do you want to allow filter by either or both or none?

    Review http://allenbrowne.com/ser-62.html
    This is the one I downloaded and attempted to modify to work on my database.
    Thank you for sharing, though!

  11. #11
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    @jlgray0127

    Can you post a copy of your db with a few fictitious records
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  12. #12
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Quote Originally Posted by jlgray0127 View Post
    Thank you, Moke. I downloaded this and tried it, and it is not filtering for me.
    After I update and move to Lname, nothing happens. Then when I select a value in Lname and tab out, nothing is filtering.
    I double checked all my options to ensure it was not something on my end where I was blocking the code from running, and I'm basically running allowing all code, activeX and macros to run right now.
    Did you remember to remove the Mark of the web?
    Right click on the file and check the unblock box?

    Click image for larger version. 

Name:	vba-unblock-file-properties.png 
Views:	13 
Size:	42.1 KB 
ID:	51409
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Moke's DB in post 7 works for me?
    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

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Moke's DB in post 7 works for me?
    Did you have any doubts, GMan?

    Thanks for the confirmation. It always freaks me out a little when someone says my example doesn't work.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 5
    Last Post: 10-16-2019, 06:09 PM
  2. Replies: 1
    Last Post: 10-09-2019, 05:23 PM
  3. Filter with multiple Combo boxes
    By dotcanada in forum Access
    Replies: 3
    Last Post: 09-01-2016, 01:19 PM
  4. Filter a Form with Multiple Combo Boxes
    By Njliven in forum Forms
    Replies: 6
    Last Post: 01-03-2013, 01:25 PM
  5. Filter form from multiple combo boxes
    By Bird_FAT in forum Programming
    Replies: 6
    Last Post: 05-19-2010, 09:32 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