Solved

Change If Statement to a Case Statement

Posted on 2013-01-20
9
330 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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

713 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