Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

difference in if and iff handling a null value

 
   Database Help (Home) -> Visual Basic RSS
Next:  Idempotent ODBMS iterators  
Author Message
temp2

External


Since: Feb 16, 2005
Posts: 2



(Msg. 1) Posted: Wed Feb 16, 2005 6:03 pm
Post subject: difference in if and iff handling a null value
Archived from groups: comp>lang>basic>visual>database (more info?)

I'm having a problem with handling null values in a vb6(sp6) program.
For some reason a if.else.endif statement seems to work fine with
detecting a null value and assigning a default if necessary(1). I
would like to use the iif statement instead(2), but it always throws
the error "Invalid use of Null" for some reason.

Anybody have any ideas why this is occurring?

Thanks in advance.

strIndex = "lngAppliedTo" & CStr(i)

(1)
If IsNull(recReceipt.Fields(strIndex)) Then
lngAppliedTo(i) = 0
Else
lngAppliedTo(i) = CLng(recReceipt.Fields(strIndex))
End If

(2)
lngAppliedTo(i) = IIf(IsNull(recReceipt.Fields(strIndex)), 0,
CLng(recReceipt.Fields(strIndex)))

 >> Stay informed about: difference in if and iff handling a null value 
Back to top
Login to vote
Steve Gerrard1

External


Since: Aug 04, 2004
Posts: 9



(Msg. 2) Posted: Wed Feb 16, 2005 6:51 pm
Post subject: Re: difference in if and iff handling a null value [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

wrote in message

| I'm having a problem with handling null values in a vb6(sp6) program.
| For some reason a if.else.endif statement seems to work fine with
| detecting a null value and assigning a default if necessary(1). I
| would like to use the iif statement instead(2), but it always throws
| the error "Invalid use of Null" for some reason.
|
| Anybody have any ideas why this is occurring?
|
| Thanks in advance.
|

| lngAppliedTo(i) = IIf(IsNull(recReceipt.Fields(strIndex)), 0,
| CLng(recReceipt.Fields(strIndex)))
|

Unfortunately, this is a known characteristic of the IIf function. Here
is the MSDN on it:

<quote>

IIf(expr, truepart, falsepart)

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>

In your case, it will try to evaluate CLng(recReceipt.Fields(strIndex))
even if it is Null, causing the error.

 >> Stay informed about: difference in if and iff handling a null value 
Back to top
Login to vote
temp2

External


Since: Feb 16, 2005
Posts: 2



(Msg. 3) Posted: Wed Feb 16, 2005 11:16 pm
Post subject: Re: difference in if and iff handling a null value [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks! The surprising thing is that when I did a usenet search, a lot
of people were suggesting that they use the IIf function for the
purpose I was trying to use it for. Haha now I know.
 >> Stay informed about: difference in if and iff handling a null value 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
sql query returns in wrong format - Hi, Hope someone can help me with this. I have MS SQL 2000 database where i have Table "PriceList" and there a column "Pricemk" wich is data type "money". All data in this column is in form "10001,35". So why when...

arrays - is there a way to concatenate the contents of an array (looping through the array) and storing each item in a comma delimited string? Thanks!

DataReport PageBreak - Hi everyone, I would like to know how to force pagebreak in a datareport for example after 5 records printed. Thank you all

Possible Use of a Cursor - I have a complex query that I hope I can explain it well enough for everyone to understand. I have a table that contains information for work instructions. Contained in the table are "MACHINE_PROC" these are groupings of machining processes....

Scroll Bars - Is there a way to find the size of scrollbars on a system using Visual Basic 6.0? I know how to look and change them on the display properties, but I don't know how to access that information from within Visual Basic.
   Database Help (Home) -> Visual Basic All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]