Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell if a Checkbox Control is part of an Option Group. 1

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
I can get by using the .Name property of the checkbox for the current project I am working on, but I would like to be able to see how in code to tell whether a Checkbox is part of an option group or not.

My only guess at this point is to see whether a property exists for the Checkbox, as it seems those in an option group have different properties than those not in an option group.

Does anyone have a direct answer or any suggestions?

Thanks!
 
how about
me.checkbox1.parent.name
if it returns the option group name it is part of that group
 
additionally this will return all controls that are part of that option group

Dim ctrl As Control
For Each ctrl In Me.optiongroup.Controls
Debug.Print ctrl.Name
Next

 
So, if it returned an error, then maybe it would not be part of any group?

Basically what I would like to be able to do is this:
Code:
Dim ctl as Control
For Each ctl in Form.Controls
  If TypeOf ctl Is CheckBox Then
    "If ctl is part of an option group (any option group) Then"
      Do Something
    Else
      Do Something Different
    End If
  End If
Next ctl
 
if it is part of a group then its parent would be that group outherwise it would be the from

Dim ctl as Control
For Each ctl in me.Controls
If TypeOf ctl Is CheckBox Then
If ctl.parent.name <> me.name Then 'not part of group
Do Something
Else
Do Something Different
End If
End If
Next ctl
 
Gol4

your solution might not work beacuse a control can have the same name as the form

try something like this

Code:
dim a
err=0
For Each ctl in me.Controls
err=0
  If TypeOf ctl Is CheckBox Then
      a=me.parent.Hwnd
      if a<>0 then 'regulare check box
         dothis
      else
         doshomethingelse
      end if

next
 
pwise, can you elaborate on what you've got there?

I'm not 100% sure that I follow what you're code does...

I may just being dense, but just want to make sure.

Thanks.
 
pwise
You are correct that If the form and a control had the same name it would not work but by following proper naming conventions this will not happen.

but just in case, the below will work

Dim Ctl As Control
For Each Ctl In Me.Controls
If TypeName(Ctl.Parent) = "OptionGroup" Then
do this
else do this
end if
Next Ctl

As hjv161 states not sure hw returning the window handle will come in handy
 
Awesome! That worked perfectly! (using the TypeName property)

Ok, what I was doing on this particular form was just putting a little code behind a button for clearing the form. So, the problem was that in an option group, I could not set the value of each checkbox to False, as that is not valid within an option group. So by recognizing it was in an option group, I could just skip those checkboxes, and rather just change the option group value to the default value for that group.

Here is what I ended up with: (oh, and that saved me about 3 or 4 lines of code in this small example)
Code:
Private Sub cmdClear_Click()

On Error GoTo Err_cmdClear_Click

    Dim ctl As Control
    For Each ctl In Form.Controls
        If TypeOf ctl Is ComboBox Then
            ctl = Null
        ElseIf TypeOf ctl Is OptionGroup Then
            ctl.Value = 1
        ElseIf TypeOf ctl Is CheckBox Then
            If TypeName(ctl.Parent) = "OptionGroup" Then
            Else
                ctl.Value = False
            End If
        End If
    Next ctl
    
Exit_cmdClear_Click:
    Exit Sub

Err_cmdClear_Click:
    MsgBox Err.Description
    Resume Exit_cmdClear_Click
    
End Sub

I did not need a textbox check, as there are none on the particular form. Of course, if there were a way to just check to see if a control is editable (as far as it's values), that'd probably work even better and more universal), but I wouldn't think there would be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top