Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / November 2006

Tip: Looking for answers? Try searching our database.

Code efficiency for if-then vs Select Case

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jael - 16 Nov 2006 17:00 GMT
I have several one line If - then statements (by one line, I mean "If x>1
then y=x+1") which I could recode as a select case structure.  Is there trade
off  point for execution/memory efficiency?

Code segment:
   If EventID = 2 Or EventID = 20 Then Gala2003_2004 (EventID)
   If EventID = 33 Then Gala2005 (EventID)
   If EventID = 56 Then Gala2006 (EventID)
   If EventID = 50 Or (EventID >= 57 And EventID <= 71) Then RHGT (EventID)
   If EventID = 87 Then RHGT (EventID)
   . . .

Thanks,
Jael
Klatuu - 16 Nov 2006 18:51 GMT
A Select Case statement would be better.  The way it is written, each
condition has to be evaluated even if a previous condition tested true.  With
the Select Case statements, once a true condition is found, the other
conditioins are not evaluated.

Also, (and this is a personal opinion), One Line If statements are a bad
habit.  I have seen too many times when a developer could not understand why
their code didn't work.  It is usually because they have put some line of
code in that is affected by the If.  It is not a technical thing, it is a
human thing.  Using a traditional If Then
   Do Something
Else
  End If

Is easier to read.

   Select Case EventID
       Case 2, 20
           Gala2003_2004 (EventID)
       Case 33
           Gala2005 (EventID)
       Case 56
           Gala2006 (EventID)
       Case 50, 57 to 71, 87
           RHGT (EventID)
   End Select

> I have several one line If - then statements (by one line, I mean "If x>1
> then y=x+1") which I could recode as a select case structure.  Is there trade
[quoted text clipped - 10 lines]
> Thanks,
> Jael
Marshall Barton - 16 Nov 2006 19:14 GMT
>I have several one line If - then statements (by one line, I mean "If x>1
>then y=x+1") which I could recode as a select case structure.  Is there trade
[quoted text clipped - 7 lines]
>    If EventID = 87 Then RHGT (EventID)
>    . . .

That approach executes every If condition.  For mutuall
exclusive conditions a Select Case would only evaluate the
conditions until it finds one that is true.  While a human
will never notice the time difference, I strongly prefer the
Select Case because it it much easier to read and you can
specify the conditions much more concisely.

Signature

Marsh
MVP [MS Access]

Jael - 16 Nov 2006 23:54 GMT
My thanks to you and Klatuu.

When I thought about it, it was obivious that the if-then was not as
execution efficient as the case structure.  I use a lot of case structures
and you are quite correct - they are easier to read and easier to control.

Klatuu - I understand your concern re:one line if-then, but will probably
continue to use it until it bites me (standard human response...;=)

Again - thank you,
Jael

> >I have several one line If - then statements (by one line, I mean "If x>1
> >then y=x+1") which I could recode as a select case structure.  Is there trade
[quoted text clipped - 14 lines]
> Select Case because it it much easier to read and you can
> specify the conditions much more concisely.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.