verify state of option button

S

stewart

I have a group of four option buttons on a userform. The user is
required to check select one of them. What can I put in my code to
verify this. What I currently have is below but it does not seem like
it is the most efficient way of completing this task as I have this
occurrence 34 separate times.


sub verify()
If opt1.Value = False And opt2.Value = False And opt3.Value = False
And opt4.Value = False Then
GoTo 2:
ElseIf opt5.Value = False And opt6.Value = False And opt7.Value =
False And opt8.Value = False Then
GoTo 2
end if

'other code below
....
exit sub
2: msgbox "Please verify that you have completed each section"
end sub
 
M

Mike

try this
Sub verify()

Select Case True
Case Me.OptionButton1.Value
'code here
Case Me.OptionButton2.Value
'code here
Case Me.OptionButton3.Value
'code here
Case Me.OptionButton4.Value
'code here
Case Else
'code here
End Select

End Sub
 
S

Susan

there's another way somebody told me, once......
using frames to group each set of option buttons, you only have to check if
the # of frames with a true value are the same as the # of frames on your
userform! if 24 frames = true equals 24 frames total, then you know one
button (and only one) was selected in each frame.
this is the code i came up with from merjet............. it is JUST the
looping thru the frames to see if # matches. this is in my userform code.
=========================
Sub cmdEnter_click()

'make sure an option button is checked in each frame
i = 0

For Each oControl In Me.Controls
If TypeOf oControl Is msforms.OptionButton Then
If oControl Then
i = i + 1
End If
End If
Next
If i >= 12 Then 'no. of frames
'do nothing
Else
MsgBox "Every frame must have a selected option button!" _
& vbCrLf & _
vbCrLf & _
"Please go back and answer all the questions." _
, vbOKOnly + vbExclamation
Exit Sub
End If

=======================
hope it helps!
susan
 
G

George Nicholson

A couple of possible approaches:

If opt1 + opt2 + opt3 + opt4 Then
Msgbox "Something selected"
Else
' All are False (or Null)
Msgbox "Nothing selected"
End If

OR

Select Case True
Case opt1, opt2, opt3, opt4
' One of the above is True
Msgbox "Something selected"
Case Else
' All are False (or Null)
Msgbox "Nothing selected"
End Select

I like the 2nd appoach cause 1) its easier to read IMO, 2) easier to add
additional buttons 3) easier to break a button off to a seperate Case
statement. In my mind, it mimics the functionality of an Option group
(available with Access or vb control sets, etc.) fairly well.

If you are working on a UserForm (which has a Controls collection), you
could also do something like:

Dim i As Integer
Dim iSelected As Integer

For i = 1 To 4
If Controls("Opt" & i) Then
iSelected = i
Exit For
End If
Next i
If iSelected = 0 Then
MsgBox "None selected"
End If

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top