Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default verify state of option button

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default verify state of option button

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
"stewart" wrote:

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default verify state of option button

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







"stewart" wrote in message
oups.com...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default verify state of option button

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,

"stewart" wrote in message
oups.com...
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help on how to grey out one option button in one group box based on the selection of another option button in another group box George Excel Programming 12 March 11th 07 02:08 PM
Triple state command button John Davies Excel Programming 1 September 8th 06 10:00 AM
Mouse-Over State on Graphic Button TheVisionThing Excel Programming 6 November 18th 05 05:17 AM
triple state option buttons Ken Macksey Excel Programming 0 April 13th 05 06:23 PM
state of option button crapit Excel Programming 12 June 3rd 04 10:15 AM


All times are GMT +1. The time now is 03:43 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"