Results 1 to 15 of 15
  1. #1
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352

    On Click event filter By checkbox = True

    Need help with code to filter selections. If the check box is checked for certain items i want a button to filter the results to only show these items when the button is clicked.
    So far I have:
    Private Sub Command25_Click()
    If chkNeedPart = -1 Then
    Me.Filter = "NeedPart=" True


    Me.Form.Refresh
    End If
    End Sub

    I tried using RunCmd.ApplyFilter( ,"chkNeedPart=" True, ) didn't work.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You are missing a line:
    Code:
    If chkNeedPart = -1 Then
        Me.Filter = "NeedPart = " True
        Me.FilterOn = True
        Me.Form.Refresh
    End If

  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,930
    Also missing concatenation operator but don't need to concatentate. True is an Access and VBA constant and can be within the quoted string:

    Me.Filter = "NeedPart = True"

    The Refresh line is probably not needed. I don't use it in conjunction with Filter/FilterOn.
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Oops..... I missed the concatenation.

  5. #5
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Perfect. Now the issue is after i filter this, i unchecked a result and hit the button again- it did not re filter. I was presented with the same options.

    Private Sub Command25_Click()
    If Me.chkNeedPart = -1 Then
    Me.Filter = "NeedPart = True"
    End If
    Me.FilterOn = True
    Me.Form.Refresh
    End Sub

    Do I need a line for something like
    ElseIf Me.chkNeedPart = 0 then
    Me.Filter = "NeedPart = False"
    within the if clause?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Eliminate the If Then

    Private Sub Command25_Click()
    Me.Filter = "NeedPart = " & Me.chkNeedPart
    Me.Filter = Me.chkNeedPart
    End Sub
    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
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Did just that... and the filter doesn't work? All results stay

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    Private Sub Command25_Click()
        Me.Filter = "NeedPart = " & Me.chkNeedPart
        Me.FilterOn = Me.chkNeedPart
    End Sub

  9. #9
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Okay this worked well. But I still have the issue of re-filtering my already filtered results. With this code it just sends back all the results, does not re filter them.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Ooops, sorry for typo.

    Filter using the checkbox as parameter should switch between showing records that are True and records that are False. Unless this in an unbound checkbox set for triple state.

    If you want to switch between show all records and showing only the True records, that will need If Then Else.

    If you want to filter for True records and then apply another filter parameter, that is a different issue. http://www.allenbrowne.com/ser-62.html

    What exactly do you want to do?

    I am wondering why there is a checkbox and a button. Is chkNeedPart an unbound control?
    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
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Well I have query results shown on a form (of parts to order). The user can select what parts they need to order with a checkbox. I have a button on the bottom to filter the results to only include what they checked. The most recently posted code worked, however let's say the user accidentally checked some 3rd part they didn't want to order and un-checks it and clicks the button again to filter out that item (assuming they will now see only 2 parts)...currently it does not filter again. It simply shows the same 3 parts- two being checked and one being un-checked. Understand the problem better?

  12. #12
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Quote Originally Posted by June7 View Post
    I am wondering why there is a checkbox and a button. Is chkNeedPart an unbound control?
    No chkNeedPart 's control is the NeedPart field from my table. The button is so I can filter the results to only show those the user has checked.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    So chkNeedPart is checkbox bound to NeedPart field? I was thinking it was an UNBOUND checkbox for user input of filter parameter.

    So why filter the records? This means user cannot select a record they forgot to select.

    What do you want to happen with the selected records? Run code to save their IDs into an OrderDetails table? Then when that's done, need to reset all the parts records back to False?
    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
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    I could make it unbound..the field is only in there for the user to use, so perhaps it would be better if it was unbound for user input of filter param? The selected records are apart of a form that will eventually be emailed as a purchase requisition. Yes, ideally when that is done reset back to false as if nothing had happened for next use.

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I don't think UNBOUND will work for what you want. Every record on form will show the same value in an UNBOUND control that does not have a dynamic expression.

    You want to email a form? A report would be better instrument for output.

    Set the check value for each record. Save the records to OrderDetails. Publish report. Set check value back to False.

    A simple UPDATE SQL action can reset the field: CurrentDb.Execute "UPDATE Parts SET NeedPart=False"

    But really, a subform bound to OrderDetails and combobox to select Part in each record row might be just as fast as scrolling through Parts records and clicking checkbox. Lot less code.
    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.

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

Similar Threads

  1. Replies: 4
    Last Post: 09-12-2014, 08:23 AM
  2. Replies: 7
    Last Post: 05-30-2013, 04:22 PM
  3. Replies: 3
    Last Post: 03-08-2013, 11:34 AM
  4. if checkbox= true subtract 8.75 from A to=B
    By VanillaAwesome in forum Queries
    Replies: 6
    Last Post: 07-28-2012, 12:48 PM
  5. Right Click checkbox to uncheck?
    By JubilantJeff in forum Forms
    Replies: 7
    Last Post: 12-19-2011, 02:12 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