Results 1 to 15 of 15
  1. #1
    Master Klick is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    47

    Toggle button to control criteria


    I have form with a number of of combo boxes that filter a query that populates a list box. It's used to track inventory. I want to add a toggle button that when press will filter my query so that it shows only items with a value > 0 in the quantity field. And when not pressed it won't filter by that field at all.

    I hope this isn't too complicated.

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    If you have the combo boxes already set up to call a single routine that builds the query, then you're just concatenating a single additional test into the SQL. The toggle button's AfterUpdate event would call the same BuildQuery function. Because the unclicked condition doesn't limit the selection criteria of the query, it doesn't put anything into the SQL.
    Code:
     
    If tglValue = True Then 
       strSQL = strSQL & " AND [Value] > 0 "
    End If
    If you're using my code, in which I use a boolean variable to track whether I've already added the WHERE clause to the SQL, then it might look like this -
    Code:
     
    If tglValue = True Then 
       If WhereDone Then 
          strSQL = strSQL & " WHERE [Value] > 0 "
       Else
          strSQL = strSQL & " AND [Value] > 0 "
          WhereDone = True
       End If
    End If

  3. #3
    Master Klick is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    47
    I'm sorry, could you explain that in more detail. I don't have any idea what you're telling me to do.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Dal is suggesting VBA code to build an sql statement.

    How are you using the comboboxes to filter query? Is the query parameterized by referencing the comboboxes? Post the query SQL statement.

    Will every record have a value in Quantity field?

    Try this criteria under the Quantity field:

    >=IIf([Forms]![formname]![togglename],1,0)
    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.

  5. #5
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Sorry, there was another thread where I had posted some code on how to have multiple different controls interacting to build SQL. I'll see if I can find it.

  6. #6
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    I found one of the threads where I posted the code. Post #8 in this thread - https://www.accessforums.net/queries...orm-36100.html

    The basic idea is that the After Update event of each control calls the same BuildQuery routine, which then builds the proper SQL query for the selected states/conditions of all the controls.

    At the beginning I set a Boolean variable WhereDone to False, which indicates that there is no WHERE clause yet. When the first control needs to add a condition, it places the "WHERE (" and sets WhereDone to True, and every control after that will place an "AND" instead. At the end, if WHereDone is False, there is no condition at all, and if it's True, then you close the parenthesis ")" before the semicolon ending the Query ";".

  7. #7
    Master Klick is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    47
    The criteria in the query is: Like [Forms]![InventoryForm]![Name] & "*", where ![Name] is the combo box. The combo boxes also pull their info from that query. So, the combo boxes not only narrow down the list box but they narrow the choices in the other combo boxes as well.

    I just want to add a button to limit it to what I have in stock. But, I need to be able to toggle it on and off.

    So like, button pressed=InvQuery!Quantity > 0, button released, no criteria in quantity.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    You want comboboxes to draw their RowSource from query that uses the comboboxes as criteria? This is circular reference and won't work.

    Did you review my post #4?
    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.

  9. #9
    Master Klick is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    47
    Well, it's working fine right now. It's isn't circular logic because there is no actual overlap. It's linear logic, but the beginning and end are at the same point. And now I'm confusing myself. Never explained without drawing a picture before. Anyway, it works, trust me.

    I just need to make a toggle button to turn the criteria for one field in the query on and off, if possible.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I show criteria using toggle in that earlier post.
    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.

  11. #11
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Ah, yes, June7's post#4 can be ANDed into the base query or the filter and will work no matter which way the toggle is set up.

  12. #12
    Master Klick is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    47
    Yeah, I'm still not getting how it works.

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    If the toggle is down its value is True, if up it is False. So If True, use 1, otherwise parameter is 0. So If the expression returns 1 then all the 0 values are ignored because the criteria is: >=1. If 0 returns then all records are selected because criteria is: >=0 - the same result as if no criteria applied.

    This does assume every record has a value of 0 or greater, no Nulls, no negatives.

    At least it worked that way in my testing.
    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.

  14. #14
    Master Klick is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    47
    Ha ha, it works! That was way way easier than I thought it would be. Thanks guys.

  15. #15
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Yeah, I thought June's code was pretty slick.

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

Similar Threads

  1. Toggle a criteria on and off?
    By overlords in forum Queries
    Replies: 11
    Last Post: 03-31-2013, 03:20 PM
  2. Control Button to toggle edit mode
    By Leonidsg in forum Programming
    Replies: 2
    Last Post: 03-19-2013, 06:53 PM
  3. Toggle Button Help
    By dbalilti in forum Access
    Replies: 1
    Last Post: 07-05-2012, 04:23 AM
  4. Toggle Button Criteria
    By tylerg11 in forum Forms
    Replies: 2
    Last Post: 03-02-2012, 09:28 AM
  5. Toggle Button Options
    By Matthieu in forum Forms
    Replies: 2
    Last Post: 11-23-2009, 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