Results 1 to 10 of 10

Thread: refresher on VB6 Null handling

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    refresher on VB6 Null handling

    Hello -
    I am tasked with updating a legacy VB6 process that writes out a text file from a SQL Server table. This program uses equal / not equal operators to test for Null, which I always thought was a no-no. (I always used the IsNull function.)

    They do things like:
    If rsTable!SomeField = Null Then ...
    and
    If rsTable!SomeField <> Null Then ...

    The "=" seems to be OK, but the "<>" seems to be causing a problem (i.e., the output is always empty for the field being tested with "<> Null").

    Before I do the surgery, I'd like a second opinion, or confirmation on this: "FieldName <> Null" will always return False, whether or not the database field is actually null?

    Thanks, guys ...
    "It's cold gin time again ..."

    Check out my website here.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: refresher on VB6 Null handling

    I prefer this

    Code:
    If len(trim(rsTable!SomeField)) = 0
    or

    Code:
    If len(trim(rsTable!SomeField)) <> 0
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: refresher on VB6 Null handling

    Use IsNull(). Try this little experiment & you might be amazed
    Code:
    Dim MyVar As Variant
    MyVar = Null
    If MyVar = Null Then
        MsgBox "MyVar is Null? True"
    Else
        MsgBox "MyVar is Null? False... Why?"
    End If
    Here's MSDN's description of IsNull and above example

    Edited: You can use the trick koolsid provided, but it doesn't tell you if the field is null or not, only that it doesn't have any data. It could, for example, contain a null string. Depends on what exactly you want to test for.
    Last edited by LaVolpe; Sep 13th, 2010 at 11:56 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: refresher on VB6 Null handling

    Thanks guys. The bottom line is that the equal (=) and not equal (<>) operators should not be used to test if a field is null. The IsNull function really must be used. The folks I inherited this from were obviously not aware.
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: refresher on VB6 Null handling

    The fastest possible way to check if a string is null is to do as followed:

    Code:
    If LenB(String_Here) Then Msgbox "Not Null!"
    If LenB(String_Here) = 0 Then Msgbox "Is Null!"
    Note that while not null it is not necessary to add the <> 0 but it is while checking if null. If Not LenB() would not work.

    LenB() is the same as Len() except LenB does not divide the response by two, making it slightly faster.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: refresher on VB6 Null handling

    vpben did you look at LaVolpe's response.... the use of a Len function (even LenB) does not differenciate between and empty string ('') and a NULL value
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: refresher on VB6 Null handling

    The information posted by vbpen is indeed incorrect. LenB only returns the byte length of a String variable. It doesn't make a difference between a null string and an empty string.

    The easiest way to check whether a string is null is to use StrPtr – this returns the pointer to the string. If StrPtr = 0 Then string is null.

    Just for a note:
    • Empty string: MyString = ""
    • Null string: MyString = vbNullString


    You can check the difference yourself by using StrPtr(MyString). Empty string is an allocated string of zero length (ie. the pointer is non-zero, because there is an allocated BSTR structure). Null string is just a string variable with no data (ie. the pointer is zero because there is no data to point to).


    Another note: IsNull does not work with strings. It only works with the Variant datatype.
    Last edited by Merri; Sep 13th, 2010 at 01:10 PM.

  8. #8
    Lively Member
    Join Date
    Aug 2010
    Posts
    74

    Re: refresher on VB6 Null handling

    Yep sorry I did not read the question correctly I guess.

    I thought they were just asking to check if the string contained data, not if it was assigned data.

    I tried IsNull(var) and it failed to return true without and while setting the variable..?

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: refresher on VB6 Null handling

    IsNull works only if it is set as Null. Basically only useful with databases where a field returned can be null. Technically Null is just a datatype of it's own, for example you can check VarType of a Variant variable to see if it is a Null or not.

    Dim MyVariant
    MyVariant = Null
    If IsNull(MyVariant) Then MsgBox "Null"

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

    Re: refresher on VB6 Null handling

    There there are Empty and Error subtypes as well.


    As usual, the manual spells all this out. For example If...Then...Else Statement says:
    Condition

    A numeric expression or string expression that evaluates to True or False. If condition is Null, condition is treated as False.
    Then Comparison Operators shows that all of the simple comparison operators (<, <=, etc.) return a Null value if one of the operands is Null.

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