Results 1 to 9 of 9
  1. #1
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31

    Adding an "All" selection to a ComboBox on Search Form

    I have a search form with unbound combo boxes that uses the combo box values as criteria for a query. The Row Source of cboCategory is based on the query:



    SELECT Categories.ID, Categories.Category FROM Categories ORDER BY Categories.Category;

    When I start the form and I don't select a value for the cboCategory combo box, the query just returns all the values. This is great. The problem is that if I make a selection in the combo box, I have no way of reverting it back to that initial value that would make the query return all categories - if I erase the combo box the query returns nothing. So I have to close the form and reopen it.

    I tried adding an "All" value to the combobox like so:

    SELECT Categories.ID, Categories.Category FROM Categories UNION Select Null as AllChoice , "(All)" as Bogus From Categories
    ORDER BY Categories.Category;

    but it does not seem to work. It just returns nothing instead of all the categories. How can I add a value of "All" to the combo box that will return all of the categories?

  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,521
    What you have should work in conjunction with this:

    http://access.mvps.org/access/queries/qry0001.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    The first SELECT line of UNION sets the field names. Alias field names not needed in subsequent SELECT lines.

    The search is using the ID field?

    If the form RecordSource is a dynamic parameter that references combobox, try criteria under the ID field:

    LIKE "*" & [Forms]![myForm]![myControl] & "*"

    If user selects the Null (All) from the list, the LIKE and wildcard should return all records.

    Will need the Requery method in code.

    Paul, I am confused why your suggested criteria is to test if the control Is Null and not the field. Shouldn't it be:

    [Forms]![myForm]![myControl] Or Is Null

    or with the LIKE and wildcard:

    LIKE "*" & [Forms]![myForm]![myControl] & "*" Or Is Null
    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.

  4. #4
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    I used the LIKE for the criteria under the ID field and it works for Null (All), but now when I select category with ID 2, it also returns other category ID's with 2 in them, like 12, 20, 21, 22, etc.

  5. #5
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Pauls suggestions is correct. If the control is null, it should return all record, there for you check the control to be null.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by June7 View Post
    Paul, I am confused why your suggested criteria is to test if the control Is Null and not the field. Shouldn't it be:

    [Forms]![myForm]![myControl] Or Is Null

    or with the LIKE and wildcard:

    LIKE "*" & [Forms]![myForm]![myControl] & "*" Or Is Null
    It's an Or test. If the control is Null the test evaluates to True and all records are returned. The OP has already pointed out the flaw of using Like.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    True, the LIKE operator and wildcard not really right for number and date fields.

    I never would have figured out Paul's approach. Partly because I avoid dynamic parameters in queries and RecordSource. Learned something new. Thanks!
    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
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Thanks guys, Is Null did the trick. How do I mark a thread solved?

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It's under Thread Tools at the top of the thread.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 12-17-2013, 08:27 AM
  2. Replies: 11
    Last Post: 06-18-2013, 07:48 AM
  3. Replies: 8
    Last Post: 03-05-2013, 01:20 PM
  4. ComboBox "Select" and "DLookUp"
    By witooldas in forum Forms
    Replies: 0
    Last Post: 03-23-2011, 03:31 AM
  5. Replies: 4
    Last Post: 12-03-2010, 04:05 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