Solved

Select Case Statement vs If ElseIf Statement

Posted on 2006-10-30
7
1,690 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now