Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65

    Count Checkboxes

    Hi again all....i thought that while i'm on a roll i might as well throw this one in too



    I have a form where there are several checkboxes (all bound to different fields).....i need a textbox at the end of the form which will count the amount of checkboxes which are checked (regardless of whether true or false)...can that be easily done ?

    Thanks
    Em

  2. #2
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Not tested but something like this:




    Code:
    Dim iCounter as integer
    Dim ctrl as Control
    iCounter=0
    For Each ctrl In form.controls 
        If typename(ctrl) = "CheckBox" Then 
            If ctrl.value = True Then 
                iCounter = iCounter+1 
            End If 
        End If
    Next ctrl
    Me.YourTextBox.SetFocus
    Me.YourTextBox.Value=str(iCounter)

  3. #3
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    Thank you (again).....i've a couple of questions. Where does that code go ? and is the line of code where you have 'YourTextBox' the only place where i substitute a name ?

    Thanks

  4. #4
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Where that code goes it's up to you really. You can have it under some button Click event, on form_current event or make a separate sub for it and call it on every checkbox Change event. It depends if you want it to count ticked checkboxes "live" or after something special happens.
    And yes, you only need to put proper name for YourTextBox.

  5. #5
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    When i open the Form i'm getting a message saying 'Runtime Error 2427. You entered an expression that has no value'

    If ctrl.Value = True Then is highlighted in yellow

  6. #6
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Where did you place the code? It needs to be inside some sub, like:

    Private Sub SomeButton_Click()
    (here goes the code)
    End Sub


    Can you upload your db with that code placed?

  7. #7
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    I added it to the OnCurrent event of the form

    Code:
    Private Sub Form_Current()Dim iCounter As Integer
    Dim ctrl As Control
    iCounter = 0
    For Each ctrl In Form.Controls
        If TypeName(ctrl) = "CheckBox" Then
            If ctrl.Value = True Then
                iCounter = iCounter + 1
            End If
        End If
    Next ctrl
    Me.txtCount.SetFocus
    Me.txtCount.Value = Str(iCounter)
    End Sub]
    I've a feeling i've done something stupid

  8. #8
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Is that exactly how it looks in your VBA editor?
    Code:
    Private Sub Form_Current()Dim iCounter As Integer
    should be two separate lines:
    Code:
    Private Sub Form_Current()
    Dim iCounter As Integer
    Also:
    Code:
    End Sub]
    There's extra bracket at the end of your code.

    Apart from that looks ok. Actually I just tested it and it works as expected.

  9. #9
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    Ah no i think that's just how it copied and pasted onto the webpage. I've attached the error message and the code window below
    Attached Thumbnails Attached Thumbnails VB.JPG   Error.JPG  

  10. #10
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Ok, quick googling returned this:
    When a Form's control is linked to a recordset, and that recordset is empty (e.g. a table with no records in it yet.) then any reference to that control's value will trigger the following error:
    "Run-time error '2427': You entered an expression that has no value."
    My guess is that code you put in tries to count checkboxes bound to recordset and possibly one of those is null (new record maybe?).
    Now, I'm not sure if you want to count those checkboxes as well or only unbound ones. So I see two possible solutions:
    1. to count only unbound checkboxes, change their Tag property to something unique (like "CountMe" or whatever) and then add another If in code to check if ctrl.Tag="CountMe" before checking its value.
    2. to count checkboxes bound to recordset add some more conditions before checking value as well, like:
    Code:
    If Not IsEmpty(ctrl) And Not IsNull(ctrl) And Not hasNoValue(ctrl)
    That should resolve this error I think, can't test it at the moment unfortunately.

  11. #11
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    This is turning out to be trickier than i thought. So would it be better if they were all unbound ?
    I'm not really sure how to integrate the extra code into what i already have (my knowledge of VB is non existent)

  12. #12
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Try this:
    Code:
    Private Sub Form_Current()
    Dim iCounter As Integer
    Dim ctrl As Control
    iCounter = 0
    For Each ctrl In Form.Controls
        If Not IsEmpty(ctrl) And Not IsNull(ctrl) And Not hasNoValue(ctrl) Then
            If TypeName(ctrl) = "CheckBox" Then
                If ctrl.Value = True Then
                    iCounter = iCounter + 1
                End If
            End If
        End If
    Next ctrl
    Me.txtCount.SetFocus
    Me.txtCount.Value = Str(iCounter)
    End Sub
    It will just omit all checkboxes with no value (if that's what you want).

  13. #13
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    I think we're nearly there...i'm getting the error below when i open the Form
    Attached Thumbnails Attached Thumbnails Error.JPG   VB.jpg  

  14. #14
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Remove that part and in that line leave only:
    Code:
    If Not IsEmpty(ctrl) And Not IsNull(ctrl) Then
    I don't know where did I get it from

  15. #15
    Emma35 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Ireland
    Posts
    65
    Ok did that but it's now giving the previous '2427' error with the same line highlighted in yellow

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-15-2014, 01:59 PM
  2. Counting Checkboxes
    By maxx102 in forum Queries
    Replies: 2
    Last Post: 12-19-2012, 08:09 AM
  3. COUNT Group Count Records COUNT FUNCTION
    By PMCOFFEY in forum Access
    Replies: 9
    Last Post: 11-09-2012, 09:40 PM
  4. Checkboxes ?
    By Trojnfn in forum Access
    Replies: 3
    Last Post: 09-30-2011, 01:52 PM
  5. Reports and Checkboxes
    By seah0rse in forum Access
    Replies: 1
    Last Post: 06-07-2011, 03:37 AM

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