Results 1 to 23 of 23

Thread: Checkbox Looping Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Checkbox Looping Help

    Please help with looping

    --------------------------------------------------------------------------

    I don't understand why the following code isn't working

    --------------------------------------------
    For a = 1 To 3

    If Check(a).Value = Checked Then Label1.Caption = "a check box is checked"

    Next a

    End If

    --------------------------------------------

    Any help is appreciated
    Last edited by rovcruiser; Jan 5th, 2005 at 10:20 PM.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Checkbox Looping Help

    VB Code:
    1. If Check(a).Value = vbChecked Then


  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Checkbox Looping Help

    Your code assumes that you have a control array that starts at an index of 1. The normal starting point is 0. Also it's probably better (more usual anyhow) to use vbChecked rather than Checked.

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    [QUOTE=Merri]
    VB Code:
    1. If Check(a).Value = vbChecked Then

    I just tried that and an error comes up saying "Sub or Function not defined" and then it highlights the word "Check"

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Checkbox Looping Help

    Then you don't have a control array named Check. Is it Check1 or have you renamed the controls to something else? Also note what MartinLiss said.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Checkbox Looping Help

    [QUOTE=rovcruiser]
    Quote Originally Posted by Merri
    VB Code:
    1. If Check(a).Value = vbChecked Then

    I just tried that and an error comes up saying "Sub or Function not defined" and then it highlights the word "Check"
    ????

    • Do you have a control array?
    • Is the name of that control array Check?

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    Quote Originally Posted by MartinLiss
    ????

    • Do you have a control array?
    • Is the name of that control array Check?

    I have 3 checkboxes with the following names:

    check1
    check2
    check3

    one label (label1) and one command button

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Checkbox Looping Help

    You don't have a control array (group of controls of the same type sharing one name but having a different indexes) so you'll need to loop through controls collection:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4.     For Each ctl In Controls
    5.         If TypeOf ctl Is CheckBox Then
    6.             Debug.Print ctl.Name & " : " & ctl.Value
    7.         End If
    8.     Next ctl
    9.  
    10. End Sub

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Checkbox Looping Help

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4.     For Each ctl In Controls
    5.         If TypeOf ctl Is CheckBox Then
    6.             If ctl.Value = Checked Then Label1.Caption = "a check box is checked"
    7.         End If
    8.     Next ctl
    9.  
    10. End Sub

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Checkbox Looping Help

    - make a new checkbox
    - rename it chkTest
    - see property Index, make it be 0
    - Edit > Copy
    - Edit > Paste
    - make so that you have three checkboxes
    - the indexes of the checkboxes vary from 0 to 2, so change your code to point from 0 To 2 instead of 1 To 3
    - also make sure you have chkTest(a) instead of Check(a)




    Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Checkbox Looping Help

    Quote Originally Posted by Merri
    ... Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?
    VBF had a problems from (according to my timing) about 7:45 Pm till 8:40+ PM ... I even open a new thread in feedback forum.

    EDIT: the same thing happened with this reply ...

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    Quote Originally Posted by RhinoBull
    You don't have a control array (group of controls of the same type sharing one name but having a different indexes) so you'll need to loop through controls collection:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4.     For Each ctl In Controls
    5.         If TypeOf ctl Is CheckBox Then
    6.             Debug.Print ctl.Name & " : " & ctl.Value
    7.         End If
    8.     Next ctl
    9.  
    10. End Sub

    Hey thanx alot for your help, unfortunately I don't seem to know much about arrays so is there any posibility that you could put the coding together with my above code, sorry but Im still a learner :mike:

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Checkbox Looping Help

    That example is not for an array. It will list the name of your checkboxes.
    To create an array, delete all 3 of them. Create a new one, and name if CHECK. Then copy it, and click PASTE. It will ask you if you want to create a control array. Select yet. This will give you 0 and 1. To get the third one, just paste another onto the form. It will be #2.

    Then, you can check if Check(0).value as you wanted orginally.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    Quote Originally Posted by Merri
    - make a new checkbox
    - rename it chkTest
    - see property Index, make it be 0
    - Edit > Copy
    - Edit > Paste
    - make so that you have three checkboxes
    - the indexes of the checkboxes vary from 0 to 2, so change your code to point from 0 To 2 instead of 1 To 3
    - also make sure you have chkTest(a) instead of Check(a)




    Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?

    YOU SAVED MY DAY, THANX ALOT MERRI

    Thanx to everyone else that helped me out... This Forum Rocks, by the way im new

  16. #16

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    Quote Originally Posted by Merri
    - make a new checkbox
    - rename it chkTest
    - see property Index, make it be 0
    - Edit > Copy
    - Edit > Paste
    - make so that you have three checkboxes
    - the indexes of the checkboxes vary from 0 to 2, so change your code to point from 0 To 2 instead of 1 To 3
    - also make sure you have chkTest(a) instead of Check(a)




    Edit what the... I just posted this, it is the newest post and I'm BEFORE the post that was posted before I posted?

    Hi again,

    Does anyone know how this can be done in VBE (vb excel) as I thought it would be exactly the same as vb6. It just won't let me have 2 check boxes with the same name

  17. #17

  18. #18

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    Quote Originally Posted by MartinLiss
    You don't need a control array. Try post #10 above.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4.     For Each ctl In Controls
    5.         If TypeOf ctl Is CheckBox Then
    6.             Debug.Print ctl.Name & " : " & ctl.Value
    7.         End If
    8.     Next ctl
    9.  
    10. End Sub

    I don't quite understand where to put the code or if I have to modify it and where my if statement goes that if one of the check boxes is checked...


  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Checkbox Looping Help

    Quote Originally Posted by rovcruiser
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4.     For Each ctl In Controls
    5.         If TypeOf ctl Is CheckBox Then
    6.             Debug.Print ctl.Name & " : " & ctl.Value
    7.         End If
    8.     Next ctl
    9.  
    10. End Sub

    I don't quite understand where to put the code or if I have to modify it and where my if statement goes that if one of the check boxes is checked...

    First of all, that's not the code from #10, this is
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4.     For Each ctl In Controls
    5.         If TypeOf ctl Is CheckBox Then
    6.             If ctl.Value = Checked Then Label1.Caption = "a check box is checked"
    7.         End If
    8.     Next ctl
    9.  
    10. End Sub
    I can't tell you where to put the code since I don't know your application.

  20. #20
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Checkbox Looping Help

    cruiser,
    you may want to zip your project and upload it so we can take a look at it.

  21. #21
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Checkbox Looping Help

    That is an Excel project ??? You should've open this thread in VBA Forum .

  22. #22

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    11

    Re: Checkbox Looping Help

    Quote Originally Posted by RhinoBull
    That is an Excel project ??? You should've open this thread in VBA Forum .

    OOoooooopsss ...sorry im new

  23. #23

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width