Solved

Change If Statement to a Case Statement

Posted on 2013-01-20
9
295 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
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

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

743 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

14 Experts available now in Live!

Get 1:1 Help Now