473,608 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoid IIf(), Switch(), and Choose() in VBA code

ADezii
8,834 Recognized Expert Expert
The tendency of VBA code to evaluate all expressions, whether or not they need to be evaluated from a logical standpoint, makes the use of the IIf(), Switch(), and Choose() Functions inefficient and downright dangerous. For these reasons, they should 'never' be used in VBA code. A couple of examples using IIf() will clearly illustrate this point which applies equally well to Choose() and Switch().

Example 1:
Expand|Select|Wrap|Line Numbers
  1. varValue = IIf(BooleanExpression, Function1(), Function2())
Both Functions will be called possibly leading to undesireable side effects, and a definate slowdown of you program execution. The If...Then...Els e construct, although longer, would be more efficient since only 1 Function will be called. Again, the same applies to both Choose() and Switch(). Here is the more efficient method:
Expand|Select|Wrap|Line Numbers
  1. 'only 1 Function is called depending on the outcome of BooleanExpression
  2. If BooleanExpression Then
  3.    varValue = Function1()
  4. Else
  5.    varValue = Function2()
  6. End If
Example 2:
Expand|Select|Wrap|Line Numbers
  1. dblNew = IIf(intY = 0, 0, intX / intY)
The above line of code would at first appear relatively harmless since it apparently covers the case where intY = 0, but this is far from True. Remember, the 2nd expression will also be evaluated and it will subsequently generate a runtime Error (Division by 0).

NOTE: I saved the best for last! These Functions when used in Queries, Forms, or Reports do not exhibit the same behavior and work exactly as you would expect. It is only in VBA code, in a Module, where you need to avoid IIf(), Switch(), and Choose().
Mar 3 '07 #1
14 25031
NeoPa
32,566 Recognized Expert Moderator MVP
This is an important point to make ADezii.
However, I think to avoid these quite useful functions altogether simply because they can, in certain limited circumstances, noticeably degrade the performance of a project, is possibly overkill.
These limitations are certainly worth bearing in mind, especially if the various options execute large amounts of, or particularly slow, code.

On your last point, this is very interesting and I'd be very interested in a link to allow me to investigate further. I was unaware of any such difference in the SQL versions of these functions.
Mar 4 '07 #2
ADezii
8,834 Recognized Expert Expert
This is an important point to make ADezii.
However, I think to avoid these quite useful functions altogether simply because they can, in certain limited circumstances, noticeably degrade the performance of a project, is possibly overkill.
These limitations are certainly worth bearing in mind, especially if the various options execute large amounts of, or particularly slow, code.

On your last point, this is very interesting and I'd be very interested in a link to allow me to investigate further. I was unaware of any such difference in the SQL versions of these functions.
I must confess that I was a little hesitant about this particular Tip since I know that you are quite fond of these 3 Functions. Please don't take it personally, I just find the details relating to them very interesting.

NOTE: The reference source for this particular Tip is The Access 2002 Desktop Developer's Handbook by Paul Litwin, Ken Getz, and Mike Gunderloy. The specific location is Chapter 15 - Application Optimization, Test 17: Watch Out for IIf - It Doesn't Short-Circuit.
Mar 4 '07 #3
NeoPa
32,566 Recognized Expert Moderator MVP
Personally, I think you were right to make the point my friend.
The emphasis may have been over-strong, but considering the target audience, it's an important point that people should, at least, be aware of.
There are certainly situations where I wouldn't dream of using them (Actually, though I was aware of the IIf() problem I didn't realise it covered Choose() & Switch() too).
People with very large databases who are looking to use the Domain Aggregate functions within one of these functions should be particularly cautious. I only use them with either very small datasets or when it is impossible to get the same functionality using a sub-query in SQL (which is very rare).

It's a shame you found it in a book as I wanted to read through it myself (easily - you know, on the web).
Mar 5 '07 #4
Rabbit
12,516 Recognized Expert Moderator MVP
Tip #3 should be: How to use the help files.
Mar 7 '07 #5
NeoPa
32,566 Recognized Expert Moderator MVP
Yeah, but they change so drastically for each version that would be a tome in itself :D
Mar 7 '07 #6
Rabbit
12,516 Recognized Expert Moderator MVP
Yeah but some things are common like if you highlight a function and press F1 it will bring up the help file for it. Or if you don't know what a property does you can focus on the property and press F1 to bring up that help file. And how these things bring up different help files depending on if you're in Access or the Visual Basic Editor. And how they should look look at the See Also list for related topics.
Mar 8 '07 #7
NeoPa
32,566 Recognized Expert Moderator MVP
Very fair point.
I was hoping to find time to do one on debugging at some stage. Please feel free to take this one on if you feel inclined. I agree that it may well be very helpful and save many explanations - if we could say RTFM (Or the help file equivalent) while pointing them at such a helpful (excuse the pun) Tutorial.
Mar 9 '07 #8
Denburt
1,356 Recognized Expert Top Contributor
NEO I thought I read about this in the helpfile so I was in my VBA just now an typed in IIF, Switch, and Choose highlighted it hit F1 and each one was in there the point ADezii is making is right near the bottom. :)
Mar 22 '07 #9
Denburt
1,356 Recognized Expert Top Contributor
OK I looked it up just for fun it is on the web on the Microsoft Site.

Developer Handbook Chapter 15
Mar 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1263
by: jabailo | last post by:
For web services, is there a way to have VS.NET '03 automatically 'switch to code view' when clicking on a asmx file in the solution.
2
1592
by: John Black | last post by:
Hi, I wonder if there is any good way in debugging the code not steping into STL code, it is easy to do in normal statement, just click "step over", but when there are some STL type variable on the function argument list, I do not find a way to skip stepping into STL. Any suggestion?
14
2567
by: Howard Kaikow | last post by:
Can I use a bookmark to selectively choose the code to be run? For example. Suppose we have the following in an HTML file: <html>
13
4520
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only known at run time. Therefore a lot of template based trick isn't too useful. Considering datafile float x(3) 3.5, 2.5, 8.9 double y(3) 2.7, -2.3, 1.2 int z(3) 5, 2, 3
12
2905
by: Steve | last post by:
Can the Choose function be used to set the criteria for a field in a query to either "Is Null" or "Is Not Null" based on the value of an option Group on a form? Such as: Choose(Forms!MyForm!MyOptionGroup,Is Null,Is Not Null) Where MyOptionGroup can have the value of 1 or 2. Thanks!
4
1765
by: jhoiland | last post by:
Hello, I am writing some pricing optimizations for an online store. What I am trying to do is adjust the sale price based upon a combination of 3 factors...cost, weight, min advertised price (MAP). I know I could code this in VB/ASP for web...not sure how to do it in an access query. Looking around here it looks as if I might be able to do it through a complex nesting of IIF statements too, but those would look messy. Here is the...
0
8498
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8478
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8152
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6817
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.