Results 1 to 8 of 8

Thread: For Each Statement - Need Help! {Resolved}

  1. #1

    Thread Starter
    Addicted Member epod69's Avatar
    Join Date
    Feb 2005
    Location
    Cambridge, WI - USA
    Posts
    239

    Resolved For Each Statement - Need Help! {Resolved}

    Hi everyone, I have a picturebox (called GridFrame) which can have any amount of checkboxes in it. During a Sub I am wanting to check each checkbox in Gridframe and see if they are check, and if so, add text to a cell.

    Here is what I want to do:
    VB Code:
    1. For Each CheckBox In GridFrame
    2.             If CheckBox.Value = True Then
    3.                 CellText = DetailsGrid.CellText((lRow - 2), 5)
    4.                 If CellText = "" Then
    5.                     CellText = CheckBox.Caption
    6.                 Else
    7.                     CellText = CellText & "; " & CheckBox.Caption
    8.                 End If
    9.             End If
    10.         Next CheckBox
    Of course this doesnt work and I get a Variable not defined error which points to CheckBox. So, if you can understand what I am trying to do, then what suggestions might anyone have on what I should do or a correct way of how I should do it using the For Each Statement. Thanx everyone for the help! =)
    Last edited by epod69; May 26th, 2005 at 02:26 PM.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: For Each Statement - Need Help!

    what someone showed me was, to name each checkbox as checkbox*

    ie) checkbox1, checkbox2, checkbox3 etc


    then use this
    VB Code:
    1. For Each ctl In Me.Controls
    2.     If ctl.Name Like "CheckBox*" Then
    3.       If checkbox.checked = true Then
    4.            'do whatever
    5.       Else
    6.            'do something else
    7.       End If
    8.     End If
    9. Next
    Last edited by kfcSmitty; May 26th, 2005 at 01:58 PM.

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: For Each Statement - Need Help!

    just tweaked it a bit.. let me know if it works..
    the gridfram.controls may not work...


    VB Code:
    1. Dim ctl As control
    2. For Each ctl In GridFrame.controls
    3.     If Type of ctl = checkbox Then
    4.         If ctl .Value = True Then
    5.             CellText = DetailsGrid.CellText((lRow - 2), 5)
    6.             If CellText = "" Then
    7.                 CellText = ctl .Caption
    8.             Else
    9.                 CellText = CellText & "; " & ctl .Caption
    10.             End If
    11.         End If
    12.     End If
    13. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Addicted Member epod69's Avatar
    Join Date
    Feb 2005
    Location
    Cambridge, WI - USA
    Posts
    239

    Re: For Each Statement - Need Help!

    Hey, in the line 'If Type of ctl = CheckBox Then' it says: Expected Expression, and points to 'Type'. Also, there is no controls option for GridFrame (the picturebox), but I put it in anyways.....but I really can't continue with the Expected Expression error.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: For Each Statement - Need Help!

    Quote Originally Posted by epod69
    Hey, in the line 'If Type of ctl = CheckBox Then' it says: Expected Expression, and points to 'Type'. Also, there is no controls option for GridFrame (the picturebox), but I put it in anyways.....but I really can't continue with the Expected Expression error.
    Thats because TypeOf is one word.

  6. #6

    Thread Starter
    Addicted Member epod69's Avatar
    Join Date
    Feb 2005
    Location
    Cambridge, WI - USA
    Posts
    239

    Re: For Each Statement - Need Help!

    GridFrame.Controls does not work =( , what am I missing?

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: For Each Statement - Need Help!

    [LGS]Static is on the right track, however a picture box doesn't have a Controls collection and you can't use the equal sign with the TypeOf keyword. You must use the Is keyword instead. So you need to use code simular to this:
    VB Code:
    1. Dim ctl As Control
    2. For Each ctl In Controls
    3.     If TypeOf ctl Is CheckBox Then
    4.         If ctl.Container Is GridFrame Then
    5.             If ctl.Value = vbChecked Then
    6.                 CellText = DetailsGrid.CellText((lRow - 2), 5)
    7.                 If CellText = "" Then
    8.                     CellText = ctl.Caption
    9.                 Else
    10.                     CellText = CellText & "; " & ctl.Caption
    11.                 End If
    12.             End If
    13.         End If
    14.     End If
    15. Next

  8. #8

    Thread Starter
    Addicted Member epod69's Avatar
    Join Date
    Feb 2005
    Location
    Cambridge, WI - USA
    Posts
    239

    Re: For Each Statement - Need Help!

    Thanx a lot, that last one worked good. Thanx everyone for the help!

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