Results 1 to 15 of 15

Thread: if .. else doubt

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    53
    why is this work

    Code:
           If !a <> "" Then
               tempname = !a
            Else
                tempname = !b
            End If

    but not this???
    Code:
           If !a = "" Then
               tempname = !b
            Else
                tempname = !a
            End If
    --this return NULL error

    Just a bit programming concept... thanks for explaining...
    I hate programming!

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I can't make any of the two codes to work. I get an Unqualified Reference ...' error at the exclamation mark.

    What are you trying to test, anyway? And is it in VB?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    The exclamation mark is usually used as the logical NOT operator, and would be used for boolean values ; not strings.

    The logical not operator in VB is 'Not'.
    Eg.

    Code:
        Dim a As String
        If (Not (a = "")) Then
            MsgBox "A is not empty"
        End If
    You would appear to also be using the not operator with strings, which you obviously cant do.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    53

    sorry...

    I should be more specific...


    --this code is part of my vb code

    --the ! used here is the variable in form, in my case, it's a Textbox's variable


    I was trying to check if "a" is an empty string,
    if yes, I'll use text in "b" as my data
    if no, I'll use text in "a"


    Thanks

    I hate programming!

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    Dim TempData as string
    
    If IsNull(text1.text) then
       TempData = b
    Else
       TempData = a
    End If
    Take a look at the ISNULL() function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Also, I think VB treats "" and Null differently.

    (I also think that I posted this very reply just a short while back and it just disappeared.)

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    53

    Smile halo...

    Thanks everyone..


    I just came back from good friday.. today shold be easter holiday but Singapore only declare good friday as holiday... sad sad have to work..

    I'll try every suggestion..

    again.. Thanks......!!!!!
    I hate programming!

  8. #8
    Junior Member
    Join Date
    Apr 2001
    Location
    San Juan, PR
    Posts
    24

    Cool

    Simple....
    you can't assign a Null value to a variable....
    in the first one you can't never assign a null value, 'cause !a got a value.
    in the second one Null is not equal to "". so it goes to the else part. Being a variable trying to assign a NULL value, BOOM! error.

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    53
    finally I underestand..

    but...

    Do you mean that this code
    Code:
    If !a <> "" Then
               tempname = !a
            Else
                tempname = !b
            End If
    will always go to the ELSE part??

    Thanks
    I hate programming!

  10. #10
    TheSarlacc
    Guest
    you can't assign a Null value to a variable....
    - Rix

    yes u can!

    var1 = vbNullChar

  11. #11
    TheSarlacc
    Guest

    try this...

    to avoid maximum confusion, test if !a = vbnullchar, and if it is, then assign it ""

    then use it in the test case u posted!

  12. #12
    Junior Member
    Join Date
    Apr 2001
    Location
    San Juan, PR
    Posts
    24
    remember that you can have !a with values of any string e.g. "Elvira", zero-lenght string, e.g. "" or a NULL value. You have to consider the fact that any of the 3 can change your operation.
    If you test if !a="", then you know !a = "", but if !a="" it's false, it can be 'cause !a = NULL or !a = "Elvira"

    TheSarlacc....
    with function you can make wonders....

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    53
    I actually use
    Code:
    if IsNull(!a) then
    ...
    is this the only way to check if !a is null?


    Other suggestions like

    !a = "" and !a = vbNullChar

    is to assign null to !a, correct?




    Thanks!
    I hate programming!

  14. #14
    Junior Member
    Join Date
    Apr 2001
    Location
    San Juan, PR
    Posts
    24
    Don't assign a Null value.
    If you want something like it, assign a "" (zero-lenght string ), in the future, you'll see why.
    and yes ISNULL() is your only bet. (assuming that is not a zero-lenght string or a normal string, either case you can test that !a is neither, so, by elimination, it got to be NULL.)

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    53
    Thanks..

    I'll keep everything in mind...

    I hate programming!

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