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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case 4

Status
Not open for further replies.

TRHorn

Technical User
Feb 21, 2010
24
US
I can't really remember how to implement a select case.

Here is the deal. I have a User Form that has some option buttons on it. I want a different thing to happen depending on which one is selected. It seems that select case is the best way to implement this but I don't know how to detect which is selected and send the number to the select case loop.

I hope that was clear.
 
No need for Select/Case for this scenerio. Each option will have its own _Click event you can handle to determine which option was selected.
 

If you have your heart set to Select statement, try this:

3 option buttons on the Form (OptionButton1, OptionButton2, OptionButton3) and a CommandButton1

Code:
Private Sub CommandButton1_Click()

Select Case True
    Case OptionButton1.Value
        MsgBox "One was selected"
    Case OptionButton2.Value
        MsgBox "Two was selected"
    Case OptionButton3.Value
        MsgBox "Three was selected"
End Select

End Sub

Have fun.

---- Andy
 
If this:

"I want a different thing to happen depending on which one is selected."

is the actual case, then Dave is correct. Each control has its own _Click event. Thus you can code individually for each control.

That being said...it is possible you are actually trying to deal with a different situation than the one you posted.

Are you trying to determine which OptionButton is True?

In any case, regarding an individual OptionButton, Select Case is not likely to be applicable as they really only have two states (yes, you can make them triple state...).

True (selected) or False (unselected)

Thus, an IF statement is fine.

Select Case is for multiple value testing of the same value.
Code:
Select Case txtClientName.Text
     Case "Santa Claus"
           ' do this
     Case "Elvis"
           ' do that
     Case "Botticelli"
           ' do something else
     Case Else
           ' do something completely different
End Select

If...Then is for true/false - single - testing of a value.
Code:
IF [i]x condition[/i] = True THEN

So for an individual OptionButton, as it only has two states (True or False), a Select Case is not likely to be the route to take - multiple states are not happening.

Gerry
 
That's an imaginative way of turning Select/Case on its side Andy. Here's some more simple sample code demonstrating how to capture the selected option or determine at a later date which option is selected:
Code:
Private selectedOptionButton As Integer

Private Sub OptionButton1_Click()
    selectedOptionButton = 1
End Sub

Private Sub OptionButton2_Click()
    selectedOptionButton = 2
End Sub

Private Sub OptionButton3_Click()
    selectedOptionButton = 3
End Sub

Private Sub CommandButton1_Click()
    Select Case selectedOptionButton
        Case 1: MsgBox "Option one is selected (This technique utilizes the selectedOptionButton variable)"
        Case 2: MsgBox "Option two is selected (This technique utilizes the selectedOptionButton variable)"
        Case 3: MsgBox "Option three is selected (This technique utilizes the selectedOptionButton variable)"
    End Select
End Sub

Private Sub CommandButton2_Click()
    If (OptionButton1) Then
        MsgBox "Option one is selected (This technique ignores the selectedOptionButton variable)"
    ElseIf (OptionButton2) Then
        MsgBox "Option two is selected (This technique ignores the selectedOptionButton variable)"
    ElseIf (OptionButton3) Then
        MsgBox "Option three is selected (This technique ignores the selectedOptionButton variable)"
    End If
End Sub
 
VERY imaginative use of Select Case Andy. I have never seen it used that way. Thanks, I can think of other examples. Star to you.

Gerry
 



Would you call that...

"Thinking outside of the CASE?"

Wouldn't want you to be one who balks!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip Skip Skip

DaveInIowa, I can see a bit of a problem with:
Code:
Private selectedOptionButton As Integer

Private Sub OptionButton1_Click()
    selectedOptionButton = 1
End Sub

' etc.
' and your commandbutton procedures.


1. If at least ONE of the optionbuttons is not set as True on Initialize, then none of the IF statements:
Code:
Private Sub CommandButton2_Click()
    If (OptionButton[COLOR=red]X[/color red]) Then
will EVER be True. None of the OptionButton are True.

2. Ditto with CommandButton1_Click, if the user never clicks an OptionButton. If they ignore it, skip it, then selectedOptionButton As Integer always = 0 (which is not tested for).

In other words, if the OptionButtons are skipped, both CommandButton1 and CommandButton2 do nothing.

Unless of course you DO make one of the Option Buttons default to True.

This is the same "flaw" in Andy's code. If ALL of the Option Buttons are False (not selected), nothing happens.

Gerry
 

This is my long (over 10 years now and counting) on-going battle with my boss. My point is: ALWAYS have one option button selected as default. Her is: I don’t want to, leave all of them blank.

I still look for any commercial software, Microsoft or any other, with the case of option buttons and none is selected at the start. Help – anyone….?


Have fun.

---- Andy
 
Totally agree with you Andy. I always make one true. It is simple enough. Plus, one of the most common things asked about Option Buttons is "how do I make sure one is selected? How do I make sure that part of the userform is not ignored?"

Simple. Have a default! What results from that is the logic behind what ever you decide is the default. It seems to me a no-brainer. If ONE of the Option Buttons is required.......then one is required. Having none is, in fact, counterproductive.

Gerry
 
Actually, I find that Microsoft is fairly good about this. I have been trying to find an example where this is not the case. Do you know of any?

Gerry
 

I just did a little experiment:

On new, empty Form place several option buttons. None of them are selected because that's the way the come onto the Form in design time. And Run it. One IS selected at run time. The same happens in VB.NET

Interesting.... And - proving my point!

Have fun.

---- Andy
 

Ooops.

I did my little experiment in VB 6.
VBA (in Excel) does not behave this way, at Run time they all are still not selected....) :-(


Have fun.

---- Andy
 
Yup. If they are set as False, at run-time they are false.
On new, empty Form place several option buttons. None of them are selected because that's the way the come onto the Form in design time. And Run it. One IS selected at run time.
Are you saying in VB that if you put, say, three Option Buttons (all False), at run-time one of shows as true (selected)?





Which one??????

Gerry
 
> Help – anyone....?

The Microsft interface style guides recommend tat a default be selected in an option button group. That's the way that the control is supposed to work.

Here's one question that has a number of repercussions: how do I return to the 'nothing selected' state (clearly a valid choice if that is how the form starts off) once I have selected one of the options?
 

Gerry, that's exactly what I am saying - in VB 6 if I put 3 option buttons on the Form and run it, option1 is selected 'by default'. I guess it is first placed on the Form.

Then I did this:
Code:
Private Sub Form_Load()
Option1.Value = False
End Sub
and run it. Guess what? Option2 was selected.

strongm, that was my point with my boss. You select an option and say - Oh no, I want to get back to the original settings. You can not. You can't un-select an option button like you can with check box.

Have fun.

---- Andy
 
>that was my point with my boss


The point goes much deeper than that. A decent user interface is not supposed allow a user to select an invalid options. Contrariwise, it should allow the user to select all valid options

So, with the option button group, you need to ask:

Is having nothing selected an valid choice? If yes, then the user should be able to select it (or reach that state) and, with option buttons, they cannot. If it is NOT a valid option, then it should never be presented to the user.

Either way, not having one option slected as the default is a misuse of the interface.

Someone may argue that having none selected is indeed a valid choice. We've already established that this is an interface no no, but it can be handled by adding one aditional option such as "None of the above" which then allows the paradigm to work (i.e. we can select all valid states)
 



Here, here, strongm!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 

Gosh, it is hard to argue the point if everybody agrees with you. :)

As far as option buttons go, I have another disagreement with my boss: she wants to use option buttons like command buttons, which drives me crazy.
[tt]
-- Offices ------
o Accounting
o Purchasing
o Sales
o Acquisition[/tt]

and when you select the option (none is selected by default, of course) you go straight to the office's menu. So by selecting the option you take the action.

Isn't it the rule of option buttons that you can select one, change your mind, select other option, select again, and then take the action (by clicking a command button or something)?

And just to add an insult to injury, if you work in Sales, only Sales option button is available to you, all other options are disabled (grayed out). So, really you do not have any options to choose from, just one. Is that an option?

But don’t get me wrong – I like my boss and I love to work for her.


Have fun.

---- Andy
 
Andy said:
You select an option and say - Oh no, I want to get back to the original settings. You can not. You can't un-select an option button like you can with check box.
In this case you can replace a set of option buttons with one combo box having (default) first item selected "<please select .......>". This can also save some space on the form.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top