Results 1 to 10 of 10
  1. #1
    cbmorehouse is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Posts
    5

    Select all checkboxes and apply current filter

    I found some code to use so that I can press a button to select or deselect checkboxes next to the records within my form. The problem is that when I filter the form and then toggle the button to select all the records it will select every record in that table instead of just the ones i'm filtering on the screen. What addition do I need to put into my code that tells it to only check/uncheck the records that are currently being filtered? Here's the code below


    Private Sub tglYesNo_Click()
    Select Case tglYesNo


    Case True


    DoCmd.SetWarnings False
    DoCmd.RunSQL "UPDATE tblItemDetail SET tblItemDetail.Selection = On"
    DoCmd.SetWarnings True
    Me.Requery
    Me.tglYesNo.Caption = "Un-Tick All"

    Case False
    DoCmd.SetWarnings False
    DoCmd.RunSQL "UPDATE tblItemDetail SET tblItemDetail.Selection = Off"
    DoCmd.SetWarnings True
    Me.Requery
    Me.tglYesNo.Caption = "Tick All"


    End Select


    End Sub

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    You need to loop through the form's recordsetclone and update the checkboxes:
    Code:
    DIm intJ as integer, iNumberOfSelectedRecords as Integer
    
    DoCmd.SetWarnings False
    With Forms!YOUR_FORM.Form.RecordsetClone
            .MoveLast
            .MoveFirst
             iNumberOfSelectedRecords = .RecordCount
    
    
    
    
        DoCmd.SetWarnings False
    
    
        For intJ = 1 To iNumberOfSelectedRecords 
            .Edit
            !Selected = True
            .Update
                .MoveNext
        Next intJ
        .Close
    End With
    DoCmd.SetWarnings True
    Forms!YOUR_FORM.Form.Refresh
    End Sub
    Cheers,
    Vlad

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    The problem is that when I filter the form and then toggle the button to select all the records it will select every record in that table instead of just the ones i'm filtering on the screen
    you need to apply a criteria to your sql based on the filter of your form. Not tested but along the lines of

    DoCmd.RunSQL "UPDATE tblItemDetail SET tblItemDetail.Selection = On WHERE " & me.filter

  4. #4
    cbmorehouse is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Posts
    5
    Quote Originally Posted by Ajax View Post
    you need to apply a criteria to your sql based on the filter of your form. Not tested but along the lines of

    DoCmd.RunSQL "UPDATE tblItemDetail SET tblItemDetail.Selection = On WHERE " & me.filter

    I tried that and it didn't work.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    OK - 'didn't work' doesn't tell me anything and I don't have time for twenty questions so suggest go with Gicu's suggestion.

    good luck with your project

  6. #6
    cbmorehouse is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Posts
    5
    Quote Originally Posted by Ajax View Post
    OK - 'didn't work' doesn't tell me anything and I don't have time for twenty questions so suggest go with Gicu's suggestion.

    good luck with your project
    Sorry, I didn't see Gicu's comment above. No reason for you to be a complete asshole though. I followed what you said and that didn't work. I don't know how else to say that it didn't work.

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    No reason for you to be a complete asshole though.
    This is a perfect example of how to get on everyone's ignore list.

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    davegri, would you please elaborate? Why "didn't work" is considered a "I have to work harder on this shi...y forum to get some positive response out of it". If you don't have time to fully reply just say so.
    Anyway, the solution I posted was my way of dealing with numerous ways of changing the recordsource of a (sub)form. It could be actual record source: query with parameters coming from a form, filters set by VBA, right clicking and going Equalto... I think the "what you see is what you got got: approach of looping through the visible records of the form is the way to go in this case,

    Sorry if I offended anyone...
    Vlad

  9. #9
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Vlad, my man, you've stepped in front of the bullet by mistake!
    My comment was directed to the OP.

  10. #10
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I've been going back and forth on whether or not to post a reply......

    I'm in agreement with davegri on this.

    @Gicu
    Quote Originally Posted by Gicu View Post
    <snip> "I have to work harder on this shi...y forum ......
    I'm not offended, but I am curious as to why you are still on the forum, if that is the way you truly feel about the forum....



    Quote Originally Posted by cbmorehouse View Post
    I tried that and it didn't work.
    @cbmorehouse,
    maybe you are new to forums and don't understand that a reply like "it didn't work." is not informative. There should be info on what happened, didn't happen, error messages, results, etc. We don't know your skill level with VBA or Access in general. Details from you are important to be able to try and give a valid response.


    Quote Originally Posted by cbmorehouse View Post
    No reason for you to be a complete asshole though.
    However, bad behavior is NEVER acceptable.
    "complete asshole"...really???? You (with 5 posts), insulting the person (with 5,800+ posts) trying to help you is astonishing.
    Ever heard the saying "Never bite the foot that stomps your grapes"?

    Against my better judgment, I looked at your code and made a couple of changes.
    Code:
    Private Sub tglYesNo_Click()
        Dim sSQL As String
        Dim MeFilter As String
    
        'is there a filter applied?
        If Len(Me.Filter) > 0 Then
            MeFilter = "  WHERE " & Me.Filter
        Else
            MeFilter = vbNullString
        End If
    
        sSQL = "UPDATE tblItemDetail SET tblItemDetail.YourFieldName = " & Me.tglYesNo & MeFilter
        'Debug.Print sSQL
        CurrentDb.Execute sSQL, dbFailOnError
        Me.Requery
        If Me.tglYesNo Then
            Me.tglYesNo.Caption = "Un-Tick All"
        Else
            Me.tglYesNo.Caption = "Tick All"
        End If
    End Sub
    Another option:
    Code:
    Private Sub tglYesNo_Click()
        Dim sSQL As String
        Dim MeFilter As String
    
        
        MeFilter = Me.Filter
    
        Select Case tglYesNo
            Case True
                sSQL = "UPDATE tblItemDetail SET tblItemDetail.YourFieldName = TRUE WHERE " & MeFilter
                'Debug.Print sSQL
                CurrentDb.Execute sSQL, dbFailOnError
                Me.tglYesNo.Caption = "Un-Tick All"
            Case False
                sSQL = "UPDATE tblItemDetail SET tblItemDetail.YourFieldName = FALSE WHERE " & MeFilter
                'Debug.Print sSQL
                CurrentDb.Execute sSQL, dbFailOnError
                Me.Requery
                Me.tglYesNo.Caption = "Tick All"
        End Select
    End Sub
    Is "Selection" the name of the field in the table? Don't know because you haven't said anything about the table/fields.....
    Replace YourFieldName with the field name that is used to "select" record(s).

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

Similar Threads

  1. Replies: 4
    Last Post: 01-11-2018, 09:16 PM
  2. Replies: 2
    Last Post: 02-25-2013, 10:47 AM
  3. Replies: 5
    Last Post: 02-07-2013, 12:21 PM
  4. Apply Filter similar to Field Filter
    By DatabaseIntern in forum Forms
    Replies: 1
    Last Post: 06-11-2012, 05:42 PM
  5. How to filter dates using an apply filter code.
    By Jgreenfield in forum Reports
    Replies: 4
    Last Post: 11-15-2011, 01:38 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