Results 1 to 12 of 12
  1. #1
    samot79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2017
    Posts
    21

    Filter Form with unbound dropdown

    Hi,

    I am trying to use an unbound Combobox in a form to filter my result that is shown in a form. The form contains data from three different table and I want to filter result on a specific field with a value, e.g only show fruits.

    I can easily to do this with a parameter, I just put [Enter criteria] in design view for the field I want to filter on. But this method is not ideal since the user could easily misspell and has to rerun form if he/she wants to change this value.

    I have now put a Combobox in my form which I populate with data that user may filter on.

    I build an expression in design view for my form on the field I want to filter on, looking like this.

    [Forms]![frmMyData]![MyComboBox]

    I enter event handler in properties for my Combobox and creates a macro after update looking like this.

    Private sub MyComboBox_AfterUpdate()

    DoCmd.OpenQuery "qryMyData"


    DoCmd.OpenForm "frmMyData"

    End Sub

    But nothing happens. Shouldn't query filter on what's in my Combobox and form show data that query generates? With nothing, I mean that query doesn't contain any data.

  2. #2
    samot79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2017
    Posts
    21
    I will try to rephrase my question.

    How can I use value in an unbound Combobox in a form as criteria in a query?

    I want to run the query each time I change the value in Combobox.

  3. #3
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    1 - query used as record source shows ALL records you would like to see
    2 - combobox shows all values which can be used in filtering
    3 - in the AfterUpdate event of the combobox,
    Me.Filter="field_in_record_source=" & Me!comboboxname
    Me.FilterOn=True
    Me.Requery

    Note: the value used in the filter here is shown as a number. If it is a text field on the table, surround it with single quotes; if it is a date field, surround it with #

  4. #4
    samot79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2017
    Posts
    21
    Query actually works now, it shows only data I want. But the form is empty.

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Which line isn't working? Please be more descriptive of your issue.

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Is qryMyData the recordsource for the form?

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Don't need to open the query object. Set the query as the form's RecordSource.

    I never use dynamic parameterized queries. I prefer the VBA method described by aytee111.

    More info in http://www.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.

  8. #8
    samot79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2017
    Posts
    21
    But that is whats weird, I have query as forms RecordSource.

    But if I enter query builder from record source and run it, outcome is no data. Even if I populated my combobox with a proper value. But if I run query when I have my form open, than query have correct data. Just not working with form.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    So the form you want to filter is already open and this form has the combobox on it? Requery the form.

    Private Sub MyComboBox_AfterUpdate()
    Me.Requery
    End Sub

    If you want the form to open with all records then set the query dynamic parameter with LIKE operator and wildcard:

    LIKE [Forms]![frmMyData]![MyComboBox] & "*"
    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.

  10. #10
    samot79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2017
    Posts
    21
    Thanks a million! You have no idea how much I struggled with this today. I can try myself, but would it be possible to just add another combobox like this if I want to filter on one more criteria?

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Criteria applied to another field? Yes. Use the LIKE operator and wildcard. Review the link referenced in post 7.
    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.

  12. #12
    samot79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2017
    Posts
    21
    Thanks again!

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

Similar Threads

  1. Replies: 3
    Last Post: 02-13-2017, 10:29 AM
  2. Apply Filter to Subform on Main Unbound Form
    By StuW in forum Programming
    Replies: 2
    Last Post: 10-20-2016, 10:02 AM
  3. How to filter bound subform from unbound main form?
    By ittechguy in forum Programming
    Replies: 3
    Last Post: 10-25-2015, 09:12 PM
  4. Replies: 3
    Last Post: 05-02-2013, 01:59 PM
  5. Unbound Combo Box to filter form
    By Firefighter22 in forum Forms
    Replies: 4
    Last Post: 08-31-2011, 03:39 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