Link to home
Start Free TrialLog in
Avatar of CopperNose
CopperNose

asked on

Select Case Statement vs If ElseIf Statement

    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
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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
Avatar of lunchbyte
lunchbyte

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

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
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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CopperNose

ASKER

    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
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.