Solved

Change If Statement to a Case Statement

Posted on 2013-01-20
9
306 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 - Access MVP) earned 500 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
 
LVL 92

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access query with left expression 5 29
MS Access to SQL Conversion - Bit and Yes/No Datatypes 12 46
Run Time Error 3075 15 44
ms access 2013, running .mdb 2 30
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

911 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

22 Experts available now in Live!

Get 1:1 Help Now