Solved

Select Case Statement vs If ElseIf Statement

Posted on 2006-10-30
7
1,698 Views
Last Modified: 2008-01-09
    Simple form to send chosen report to printer with CRViewer91. Form has a Frame control with 3 Option Buttons, 2 Command Buttons. Following is code for the cmdSelect Report button.
         Private Sub cmdSelectReport_Click()
    'View Report and Print user choice
    Dim Value As Boolean
    Select Case Value
        Case optAssist(0).Value = True
            frmCRViewerAssist.Show
        Case optBoard(1).Value = True
            frmCRViewerMonth.Show
       Case optAnnual(2).Value = True
            frmCRViewerYear.Show
       Case Else
         'If optAssist(0).Value = True Then
         '   frmCRViewerAssist.Show
         'ElseIf optBoard(1).Value = True Then
         '   frmCRViewerMonth.Show
         'ElseIf optAnnual(2).Value = True Then
         '  frmCRViewerYear.Show
        ' ElseIf optGraph(3).Value = True Then
        '    frmCRViewerGraph.Show
        ' Else
            MsgBox "You have not made a selection" & vbCrLf & _
                            "Please select one option or press Cancel", 32, "Make Print Selection"
        'End If
        End Select
 End Sub
         The Select Case code drops to he MsgBox when the click event is fired regardless is a button is checked or not checked.    
      The If ElseIf code that is commented out works like a charm with no problems but I know that the Case Statement will work. I'm missisng something simple in the Select Case statement I think. Any help appreciated
0
Comment
Question by:CopperNose
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17834408
this is because the default value of the variable "Value" is false.


   Private Sub cmdSelectReport_Click()
    'View Report and Print user choice
    Dim Value As Boolean
    Select Case true
        Case optAssist(0).Value = True
            frmCRViewerAssist.Show
        Case optBoard(1).Value = True
            frmCRViewerMonth.Show
       Case optAnnual(2).Value = True
            frmCRViewerYear.Show
       Case Else
         'If optAssist(0).Value = True Then
         '   frmCRViewerAssist.Show
         'ElseIf optBoard(1).Value = True Then
         '   frmCRViewerMonth.Show
         'ElseIf optAnnual(2).Value = True Then
         '  frmCRViewerYear.Show
        ' ElseIf optGraph(3).Value = True Then
        '    frmCRViewerGraph.Show
        ' Else
            MsgBox "You have not made a selection" & vbCrLf & _
                            "Please select one option or press Cancel", 32, "Make Print Selection"
        'End If
        End Select
 End Sub
0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17834601
Why are you doing this     Select Case Value?

value boolean only has two value and that is true and false therefore you do not use case statement for this.

This is what I would have done.
  if Case optAssist(0).Value = True then
            frmCRViewerAssist.Show
  elseif  Case optBoard(1).Value = True then
            frmCRViewerMonth.Show
  elseif  Case optAnnual(2).Value = True then
            frmCRViewerYear.Show
  else
         'If optAssist(0).Value = True Then
         '   frmCRViewerAssist.Show
         'ElseIf optBoard(1).Value = True Then
         '   frmCRViewerMonth.Show
         'ElseIf optAnnual(2).Value = True Then
         '  frmCRViewerYear.Show
        ' ElseIf optGraph(3).Value = True Then
        '    frmCRViewerGraph.Show
        ' Else
            MsgBox "You have not made a selection" & vbCrLf & _
                            "Please select one option or press Cancel", 32, "Make Print Selection"
        'End If
  end if

0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17834608
correction


  if optAssist(0).Value = True then
            frmCRViewerAssist.Show
  elseif  optBoard(1).Value = True then
            frmCRViewerMonth.Show
  elseif  optAnnual(2).Value = True then
            frmCRViewerYear.Show
  else
         'If optAssist(0).Value = True Then
         '   frmCRViewerAssist.Show
         'ElseIf optBoard(1).Value = True Then
         '   frmCRViewerMonth.Show
         'ElseIf optAnnual(2).Value = True Then
         '  frmCRViewerYear.Show
        ' ElseIf optGraph(3).Value = True Then
        '    frmCRViewerGraph.Show
        ' Else
            MsgBox "You have not made a selection" & vbCrLf & _
                            "Please select one option or press Cancel", 32, "Make Print Selection"
        'End If
  end if
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17835162
A Select Case would be useful if you were looking for several different VALUES of the same VARIABLE:

Select Case color
Case "blue"
...
Case "green"
...
End Select


You're looking for the same value in each of several different variables.  A Select Case can't handle that; your original If statement is the "correct" (and best) syntax for it.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 17835200
You can still use the neater Select Case in your case (No puns intended)

    Select Case True
        Case optAssist(0).Value
            frmCRViewerAssist.Show
        Case optBoard(1).Value
            frmCRViewerMonth.Show
       Case optAnnual(2).Value
            frmCRViewerYear.Show
       Case Else
'...
0
 

Author Comment

by:CopperNose
ID: 17836083
    lunchbyte, yes that works. I think that is what I have commented out that I said worked, but thank anyway.
        GrahamSkan, "You Are the Man", your code is neater and straight forward. I should have realized what was wrong, there is only one varaible involved.
         Thanks, Guys for all your replys.    CopperNose
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17836146
I'd strongly encourage the simpler (and more expected) format of

If optAssist(0).Value Then
    frmCRViewerAssist.Show
ElseIf optBoard(1).Value Then
    frmCRViewerMonth.Show
ElseIf optAnnual(2).Value
    frmCRViewerYear.Show
End If


It doesn't take up any more space than a Select Case  (less if you count the "Select Case" statement itself), and it's the more appropriate construct for this type of situation.  Clearly a Select Case can be made to work, but it takes longer to adjust to an unusual use of a construct, and thus longer to understand the code.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question