Results 1 to 8 of 8

Thread: Check for NULL then....

  1. #1

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Check for NULL then....

    I am accessing an Excel sheet via ADO. No problems there.

    What I am doing now is taking the sheet data and inserting it into an SQL Table and I'm having a problem with NULL

    Invalid Use Of NULL

    Code:
    Dim clid as Integer
    clid = IIf(IsNull(rstExcel.Fields("clientid")), 9999, CInt(rstExcel.Fields("clientid")))
    I would think that if I am evaluating NULL then If NULL giving a value, else Converting it to INT I would get what I want. Debug shows ClientID as NULL.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Check for NULL then....

    Don't use IIf() for this operation, use a full If statement.

    IIf() is designed to evaluate all three expressions before returning a value, so it will fail on the Null value (3rd argument here).

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Check for NULL then....

    Yeah, it's a "flaw" of sorts in the VB6 implementation of the IIF() function.... the fact that it is a function means that all three of the parameters get evaluated... you'll need to use a full If statement as dilettante has suggested. PITA I know, but it is what it is.

    -tg
    side note - in .NET this has been fixed with the If() ternary function... which evaluates the condition FIRST... then and only then determines which of the other two expressions to then evaluate.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: Check for NULL then....

    Thanks all,

    Added a Function

    Code:
    Function IfNull(value As Variant, Optional NullValue As Variant = "") As Variant
        If IsNull(value) Then
            IfNull = NullValue
        Else
            IfNull = value
        End If
    End Function
    Then

    Code:
    clid = IfNull(rstExcel.Fields("clientid"), 9999)
    
    'In my SQL String
    CInt(clid)
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Check for NULL then....

    Quote Originally Posted by techgnome View Post
    side note - in .NET this has been fixed with the If() ternary function... which evaluates the condition FIRST...
    Do you happen to know how this is implemented internally? It can't be a regular function, though it makes the language grammar uglier to make it syntactic sugar for a full If block. I suppose it could be implemented by passing closures instead, but that seems over the top in an imperative language. (I only use C#.NET which uses the c ? t : f syntax instead, which is just fine as a feature of the grammar.)
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Check for NULL then....

    I would suspect it is just compiled as an inline construct rather than any sort of function or method call at all.

    I.e. just syntactic sugar hiding an If statement.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Check for NULL then....

    No, I don't know the specifics of how it's implemented under the hood. I think dilettante is right... during compile it gets expanded out... almost like a compiler macro in C. the If terniary function is essentially the VB version of the c ? t : f construct. It isn't any different.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Check for NULL then....

    Well, we seem to agree--we don't know, but it's almost certainly just syntactic sugar. Sounds good .
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width