Acess2000 VBA: Iif() and type mismatch error

A

Arvi Laanemets

Hi

Why does the expression below return a type mismatch error, when at start
varTTL equals "N/S" or "N/A", and doesn't when it is some numeric string? Or
are in Iif() both expressions processed regardless what does condition
return?

varTTL = IIf(varTTL = "N/S" Or varTTL = "N/A", "Null", CInt(varTTL))
 
B

Brendan Reynolds

Or
are in Iif() both expressions processed regardless what does condition
return?

Yes. From the help file ...

<quote>
IIf always evaluates both truepart and falsepart, even though it returns
only one of them. Because of this, you should watch for undesirable side
effects. For example, if evaluating falsepart results in a division by zero
error, an error occurs even if expr is True.
</quote>

Note that this applies to the use of the IIf function in VBA code - The
behaviour is different when the function is used in queries and expressions
(i.e., when it is evaluated by Jet rather than by VBA).

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
A

Arvi Laanemets

Hi

It means, always both calculations are processed, and after that is decided,
which of them to has to be used! Those guys in Redmont need to read about
optimizing from Oxford Dictionary!
LOL

Lucky me - they have somehow missed to practice the same logic for:
If LogicalExpression Then
...
Else
...
End If
 
A

Andi Mayer

if you want an one_liner then write:

If LogicalExpression Then ... Else ...


If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
Top