Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Change If Statement to a Case Statement

Posted on 2013-01-20
9
Medium Priority
?
358 Views
Last Modified: 2013-02-06
How do I change the following to a  Select case statement?

If ActivityCode = "sig32" And Agency = "HPD" Then
MsgBox "Contact HPD"

Else

End If

Open in new window

0
Comment
Question by:mickeyshelley1
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38799825
May I ask why you want to change this to a Select Case statement when what you have suffices?
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1500 total points
ID: 38799842
This really doesn't fit Select Case ... since you have two different items (Activity Code and Agency) ... and only one condition.

Leave as is ...

mx
0
 
LVL 75
ID: 38799844
All you need is this:

If ActivityCode = "sig32" And Agency = "HPD" Then MsgBox "Contact HPD"
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 38801217
As indicated above, this is not really a candidate for Select Case, which only evaluates one variable at a time.

You could do something like this:

Select Case ActivityCode
    Case "sig32"
        If Agency = "HPD" Then
            MsgBox "Contact HPD"
        Else
            ' do something else
        End If
    Case Else
        ' Whatever
End Select

Open in new window


However, that's no real improvement on what you already have :)
0
 

Author Comment

by:mickeyshelley1
ID: 38801262
This is just a part of the code, the complete code will have multiple if statements and I figured that a case statement would be a little less cluttered and more efficient.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38801290
You could do this:
Select Case TRUE
    Case ActivityCode = "sig32" And Agency = "HPD"
        ....
    Case ...
         ....
End Select
0
 

Author Comment

by:mickeyshelley1
ID: 38801423
Are there any performance considerations in a scenario with multiple ELSEIF  that would make a case statement a better choice; that is the primary reason for my question
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38801451
I don't think there is a performance difference. If there is, it would be negligible.  I understand what you mean about less cluttered. I prefer using Select Case to multiple If ElseIf statements myself.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38856566
<no points wanted>

From what I have read, a "One Variable" Case Select will always run faster than the corresponding If-Then-Else

But like IrogSinta, my guess is that the real world performance difference will not be of any real consequence.

I'm a bit old school, but I will admit that the Case Select can look neater.

But I tend to go with If/then/else because it is easier to understand in "Human" terms.
For example, the human request:
"If the Price is 2, then make the price box red"
...converts almost verbatim to:
If Price=2 then
    price.Backcolor=VBred
end if
;-)

But again, most current developer will use Select Case when the need is clear...
Again, I am a bit old school.

;-)

Jeff
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

772 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