Results 1 to 8 of 8

How do you write a command that applies to the whole form?

  1. #1
    jim35meyers is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    6

    How do you write a command that applies to the whole form?

    I know this is a novice question so if anyone has book recommendations, that'd be great too.



    I have multiple checkboxes on a form and I have been programming them using the "On Click" field in the Event tab for each box. This creates a private sub applicable to just that box and looks like so:

    Private Sub Choice_A_chkbox_Click()
    If Me.Choice_A_chkbox = False Then
    Me.Choice_ALL_chkbox = False
    End If

    The problem is that I have many checkboxes and I have a "check all" option. It works fine in one direction, i.e. if I check that box, all boxes are selected. But how do I go in the opposite direction so that if all boxes are selected and I deselect one box, it will deselect the "check all" box?

    Right now I am having to make an "On Click" event for every single box, but it seems like there would be a faster way where I could just say something like:

    Private Sub Choice_A_chkbox_Click()
    If (Me.Choice_A_chkbox = False OR Me.Choice_B_chkbox = False OR Me.Choice_C_chkbox = False) Then
    Me.Choice_ALL_chkbox = False
    End If

    But that doesn't seem to be working. thank you for your help.

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    2,146
    You could make a new subroutine CheckAll, then in each On Click event you would call that subroutine. Seeing that there is only one line of code required, it seems that you can't get away from what you are doing now.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    4,766
    If the form and check boxes are bound fields, then run an update query.
    check all, set all fields= true.
    uncheck, set all = false.
    Either way , update the form after the query runs,
    me.requery.

    if NOT bound, the you must set each box in code.

  4. #4
    andy49's Avatar
    andy49 is offline Expert
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Posts
    506
    If you have a fixed number of checkboxes (10?) and they were labelled choice_1_chkbox etc

    Looping through

    If me.controls("choice_" & i & "_chkbox) = true then counttrue = counttrue + 1

    If counttrue> 0 then me.Choice_All_chkbox = false

    Or even

    Use for each ctl in me.controls to check each chkbox that way.



    Sent from my iPhone using Tapatalk

  5. #5
    jim35meyers is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    6
    I'll try this, but where do I put it. So far everything is in its own private sub, e.g. "Private Sub Choice_A_chkbox_Click()" which I believe is for specifically when that check box is clicked. Do I put this above all the private subs?

    Thank you

  6. #6
    jim35meyers is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    6
    Quote Originally Posted by andy49 View Post
    If you have a fixed number of checkboxes (10?) and they were labelled choice_1_chkbox etc

    Looping through

    If me.controls("choice_" & i & "_chkbox) = true then counttrue = counttrue + 1

    If counttrue> 0 then me.Choice_All_chkbox = false

    Or even

    Use for each ctl in me.controls to check each chkbox that way.



    Sent from my iPhone using Tapatalk
    I'll try this, but where do I put it. So far everything is in its own private sub, e.g. "Private Sub Choice_A_chkbox_Click()" which I believe is for specifically when that check box is clicked. Do I put this above all the private subs?

    Thank you

  7. #7
    andy49's Avatar
    andy49 is offline Expert
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Posts
    506
    Form After_update event should work I think

    Sent from my iPhone using Tapatalk

  8. #8
    MatthewGrace is offline Advanced Beginner
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    91
    Perhaps this code will help... I have created a subroutine that selects or deselects all checkboxes on your form. Here is the code you can paste into a blank section in your Form's code module and put it to use right now:

    Code:
    Public Sub ToggleCheckboxes(strSelectOrDeselect As String)
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is CheckBox Then
            If strSelectOrDeselect = "Select" Then ctrl.Value = True
            If strSelectOrDeselect = "Deselect" Then ctrl.Value = False
        End If
    Next
    End Sub
    Going forward, all you need to do is write one simple line of code to either select or deselect every checkbox on that form. Below is an example of just that using command buttons. You can easily adapt this logic to your checkbox event handlers.

    Code:
    'Both of these command buttons call on the above Subroutine to select/deselect all checkboxes.
    Private Sub cmdDeselectAll_Click()
    ToggleCheckboxes "Deselect" 'This button deselects everything.
    End Sub
    Private Sub cmdSelectAll_Click()
    ToggleCheckboxes "Select" 'This button selects everything.
    End Sub
    Hope it helps.

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

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2016, 01:22 PM
  2. Replies: 16
    Last Post: 07-15-2014, 07:52 AM
  3. Replies: 1
    Last Post: 07-02-2014, 10:22 AM
  4. two counters in form but only if XXX applies
    By dachap1983 in forum Access
    Replies: 2
    Last Post: 02-22-2014, 12:28 PM
  5. Replies: 3
    Last Post: 10-22-2010, 05:53 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
  •  
Tech Forums: Microsoft Office Forums