PDA

View Full Version : [SOLVED:] Identifying Check Box If True



GenuineGin
01-13-2015, 07:46 AM
Hello again (sorry for all the posts!)

I am having an issue with checkboxes this time. So far I have managed to get the code to count the number of checkboxes selected like so:

'Find out how many checkboxes have been checked
Dim ctl As Control
Dim j As Long
For Each ctl In Me.frameSurveyed.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
j = j + 1
End If
End If
Next

What I would now like to do is, if only one checkbox is selected in 'frameSurveyed', for the caption of that checkbox to be applied to a label in another form. I can't work out how to incorporate this argument into an if statement (tried 'If j = 1 Then...' but that didn't work, it applies to anything more than one) or how to specifically reference the selected checkbox.

Any help much appreciated!

Bob Phillips
01-13-2015, 08:07 AM
'Find out how many checkboxes have been checked
Dim ctl As Control
Dim ctlName As String
Dim j As Long
For Each ctl In Me.frameSurveyed.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If ctl.Value Then
j = j + 1
ctlName = ctl.Name
End If
End If
Next
If j = 1 Then
otherform.labelename.Caption = ctlName
End If

GenuineGin
01-13-2015, 08:35 AM
Thank you very much!

Is there a way to use the checkboxes caption instead of it's name? It would just look neater if it displayed "Name" instead of "chkNme"...

Bob Phillips
01-13-2015, 08:49 AM
Of course


'Find out how many checkboxes have been checked
Dim ctl As Control
Dim ctlCaption As String
Dim j As Long
For Each ctl In Me.frameSurveyed.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If ctl.Value Then
j = j + 1
ctlCaption= ctl.Caption
End If
End If
Next
If j = 1 Then
otherform.labelename.Caption = ctlCaption
End If

GenuineGin
01-13-2015, 09:58 AM
That simple! Fantastic, thank you.