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: - 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: - 'only 1 Function is called depending on the outcome of BooleanExpression
-
If BooleanExpression Then
-
varValue = Function1()
-
Else
-
varValue = Function2()
-
End If
Example 2: - 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(). 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.
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.
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).
Rabbit 12,516
Recognized Expert Moderator MVP
Tip #3 should be: How to use the help files.
NeoPa 32,566
Recognized Expert Moderator MVP
Yeah, but they change so drastically for each version that would be a tome in itself :D
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.
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.
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. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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?
|
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>
|
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
|
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!
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| | |