CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    137

    If statement not providing correct result

    I have got myself into trouble, but don't understandd what could be the problem.

    I have an if statement that does not follow the condition test?

    In my recordset I have a field that contains either the word "Withdrawn" or is null.
    I assigned this to a string before applying the if statement.

    The code is :

    If rs.Fields("withdrawnprize").Value <> "" Then
    wstatus = rs.Fields("withdrawnprize").Value & " "
    MsgBox " rs withdrawnprize is not <> null "
    Else
    wstatus = ""
    End If

    If wstatus = "Withdrawn" Then
    MsgBox " withdrawnprize record found **** "
    chkExpired.Value = 1
    Else
    chkExpired.Value = 0
    MsgBox " what is going on here wstatus " & wstatus
    End If

    The msgboxs show the recordset column and the string to contain the word Withdrawn , the If statement always branches to the else code?

    Can anyone see how I have miss coded this IF logic?
    TT

  2. #2
    Join Date
    Feb 2002
    Location
    Australia
    Posts
    125
    NULL and EMPTY are not same. Try the following code

    If isnull(rs.Fields("withdrawnprize").value) Then
    msgbox "Value is null
    else
    if rs.Fields("withdrawnprize").value = "" then
    msgbox "Value is empty"
    else
    ' value found
    ' other code
    endif
    endif

  3. #3
    Join Date
    Jun 2001
    Location
    Mi
    Posts
    1,249
    NULL and EMPTY are not same ...
    Too true Ahmed. You have sinned to the other side tho ... A way to handle both issues at the same time is:

    If rs.Fields("withdrawnprize").value & "" <> "" Then
    ...

    Remember IsNull will fail on an empty string ...

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Moreover null propagate

    Moreover, null propagate:
    if X <>"a" then

    if x value is null, you never enter the if, as null propagate and
    expression is never evaluate as different

    If x="a" then
    if x is null, you always enter the if for the same reason
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    137
    Thanks to all the responses.

    I have used ahmed's approach.

    The field that I am testing against was introduced into my SQL server database after I started testing, therefore I had a combination of values (null, spaces, and the word withdrawn ).

    I believe when I go into production I will only ever have nulls or "withdrawn"

    Interestingly, when my if statement tests for a space the condition failed , but when I testsed for 10 spaces ( the size of the column ) it worked???

    Cimperiali, I don't understand the null propagate concept?
    As a guess, are you saying that the null value is interpretted as being equal to the test value. Therefore the result will always be as if the test value and the condition value are the same??
    Hard to explain, hard to understand?
    TT

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Propagation of null

    Rule is:
    "An expression containing null is always evaluated as null"
    Thus:
    if Null<>1
    becomes
    if Null<>Null
    and

    If Null=1
    becomes
    if Null=Null
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    137
    Cimperiali,

    so if a variable/object can contain null I need to do the IsNull test to avoid comparing it against a value, which will be interpretted as null value.
    (IF condition will always substitute a null test against the variable/object if it contains a null value?)


    IF variable <> 1 .........
    IF object = 1 ............

    I think I understand?

    Thanks
    TT

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Yes, use isNull

    Yes, when dealing with Null, you should use IsNull
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Feb 2003
    Posts
    78

    Re: If statement not providing correct result

    Originally posted by T2T

    The msgboxs show the recordset column and the string to contain the word Withdrawn , the If statement always branches to the else code?

    Can anyone see how I have miss coded this IF logic?

    Back to your original problem, if the message box shows that the field has "withdrawn" in it then it's possible that there is a leading or trailing space on it or that the case doesn't match.

    You can test this way

    If trim(UCASE(wstatus)) = "WITHDRAWN" then
    do what you're going to do
    End if

  10. #10
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Best bet would be to clean your data up first.

    Update them to 'WITHDRAWN' like this...

    Code:
    UPDATE <tablename> SET WStatus = 'WITHDRAWN' WHERE WStatus like '%withdr%'
    OR ISNULL(WStatus)
    OR WStatus like '% %'
    Of course, before running this, make sure you back up your database....and check that the last OR will only update the records that you want (in other words, make sure there are no other records that contain at least one space)
    Be nice to Harley riders...

  11. #11
    Join Date
    Mar 2002
    Location
    Izhevsk, Udmurtia, Russia
    Posts
    930
    To Cimperiali: You are incorrect in conclusion
    Rule is:
    "An expression containing null is always evaluated as null"
    Thus:
    if Null<>1 becomes if Null<>Null
    and
    If Null=1 becomes if Null=Null
    Both "Null<>1" and "Null=1" will be simply "Null" and therefore be converted into boolean "False".

    Because

    MSDN
    The Null value indicates that the Variant contains no valid data. Null is not the same as Empty, which indicates that a variable has not yet been initialized. It is also not the same as a zero-length string (""), which is sometimes referred to as a null string.

    Important Use the IsNull function to determine whether an expression contains a Null value. Expressions that you might expect to evaluate to True under some circumstances, such as If Var = Null and If Var <> Null, are always False. This is because any expression containing a Null is itself Null and, therefore, False.

    HTH
    Last edited by Vi2; March 30th, 2004 at 12:47 AM.
    With best wishes,
    Vita
    -----------------------
    Russian Software Development Network -- http://www.rsdn.ru

  12. #12
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Originally posted by Vi2
    To Cimperiali: You are incorrect in conclusion

    Both "Null<>1" and "Null=1" will be simply "Null" and therefore be converted into boolean "False".
    Yes, now I see.
    Thanks for pointing it out: is nice for me to learn something more every day.

    Cesare
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  13. #13
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    137
    It makes sense what Vi2 is saying.

    VB treats a null as no data to compare situation and returns a false condition.

    Kimmckenzie, I started using the Trim function recently, your example would help in situations where different users enter data inconsistantly, very useful.

    Twodogs, I assign the withdraw value in my code, so it is always consistant. The assignment is triggered by a tick box on the form.
    I will keep a copy of the sql it to looks useful.


    Thanks, I have the result and explanation I was looking for.
    TT

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

Featured