logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 IIF gotcha

Posted on
2/18/2001
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
All
Product:
5, 6



The "Conditional If" (IIf) function is great.  It allows you to reduce code from 5 lines to one.  For instance


If Condition = 10 Then
     SomeValue = True
Else
     SomeValue = False  
End If

is the same as 

SomeValue =  IIf(Condition = 10, True, False)

The only problem is that IIf doesn't shortcircuit. In other words, both the true and the false parts will execute regardless of the condition.  For instance, execute the following statement:

SomeValue = IIf(Condition = 10, MsgBox("True Condition"), MsgBox("False Condition"))

will show both messages boxes. So if you call subs or functions from IIf - watch out.


Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Marlys. Posted on 11/21/2007 7:53:18 PM
I want to do something like this:

= IIf((Parameters!EndDate.Value)<= DATEADD(DAY,35,(Parameters!BeginDate.Value)), MsgBox("True Condition"), MsgBox("False Condition"))