Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select Case Statement vs If ElseIf Statement

Posted on 2006-10-30
7
Medium Priority
?
1,711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 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