Solved

Select Case Statement vs If ElseIf Statement

Posted on 2006-10-30
7
1,705 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: 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!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

739 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