Results 1 to 5 of 5
  1. #1
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591

    Cycling through a collection of controls

    I've done this before, but I can't work out the syntax just now. I want to change the value of every checkbox on my form (40 of them) to True by clicking on a button. The functionality is already there (written by a previous developer) explicitly naming each one and setting the value to True, i.e. "Me.chkOne = True". I'd like to cycle through the with a For Each .... Next statement. Something like this.




    Code:
    Private Sub cmdSelectAllReports_Click()
    Dim chkBox As CheckBox, ctls As Collection For Each chkBox In ctls chkBox.Value = True Next chkBox
    End Sub
    I've tried a lot of variations on this and just dug myself in deeper.

    Hoping you can help.


    Thanks, Paul

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    air code untested
    Code:
    Private Sub cmdSelectAllReports_Click()
    Dim ctl as Control
    For each ctl in me.controls
     If ctl.ControlType = acCheckBox Then '<---spotted by James
        ctl.value = True
     Endif 
    Next ctl
    .....
    Last edited by orange; 08-18-2015 at 02:25 PM.

  3. #3
    JamesDeckert is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Location
    Salina, KS
    Posts
    262
    I believe Orange's code
    If ctl.ControlType = acTextBox Then
    should be
    If ctl.ControlType = acCheckBox Then

  4. #4
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    Thanks, worked perfectly.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Good spotting James!

    It's funny when you reread things after the fact. ( jeez the whole issue was about check boxes)

    Thanks. I have corrected the code in my post.

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

Similar Threads

  1. Replies: 3
    Last Post: 11-16-2012, 10:15 AM
  2. Cycling through values in a list box
    By kmw in forum Programming
    Replies: 4
    Last Post: 08-27-2012, 10:58 AM
  3. Replies: 3
    Last Post: 08-22-2012, 12:49 PM
  4. Cycling through subform Records
    By paddon in forum Programming
    Replies: 3
    Last Post: 05-10-2011, 10:20 AM
  5. Printing reports and cycling through records
    By dabman in forum Programming
    Replies: 4
    Last Post: 12-15-2009, 11:45 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