Results 1 to 17 of 17

Thread: converting NULL values

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117

    converting NULL values

    I am writing a databse program using an Access datbase in VB.

    I am tryin to add the contents of a recordset to a combo box, however some of the values are NULL and so VB returns and error.

    Therefore i am trying to convert the NULL to a blank string "" or 0 using an SQL statement

    Code:
    db.OpenRecordset("SELECT a, IIf(IsNull(a), "", a) " & _
                                        "FROM table")
    This brings up a syntax error.... can NE one shed any light on this...

    Ta

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    instead of doing it in the SQL statement, do it when you fill the combo

    VB Code:
    1. while not rs.eof
    2.    cbo.AddItem rs("field") & ""
    3.    rs.movenext
    4. wend
    -= a peet post =-

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117
    I have now fixed the sql statement but it still brings up an error when it iterates to a record with a blank field it brings and error
    'Invalid use of NULL'

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    again

    cbo.AddItem rs("field") & ""
    -= a peet post =-

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117
    i still get the 'invalid use of null' error!!

    The sql to replace the null with "" or 0 wasnt working NE way as i tested it using a msgbox to display the field... it still displayed blank.

    Is there any other way to overcome this problem??

    The error ocurs when im moving through the recordset. WHen it reaches a record with a blank field, the error occurs.


  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: converting NULL values

    Originally posted by boyracer38
    I am writing a databse program using an Access datbase in VB.

    I am tryin to add the contents of a recordset to a combo box, however some of the values are NULL and so VB returns and error.

    Therefore i am trying to convert the NULL to a blank string "" or 0 using an SQL statement

    VB Code:
    1. db.OpenRecordset("SELECT a, [i]IIf(IsNull(a), "", a)[/i] " & _
    2.                                     "FROM table")

    This brings up a syntax error.... can NE one shed any light on this...

    Ta
    If you are using the bit in italics its because of the function.
    IIF evaluates both outcomes ALWAYS.

    Now, from the looks you aren't following Peets advice, have a go with that. All it means is the sql statement becomes the following :
    SELECT a FROM table
    The as you loop through filling your combo, you replace the null with a "" as applicable.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    Re: converting NULL values

    Originally posted by boyracer38
    I am writing a databse program using an Access datbase in VB.

    I am tryin to add the contents of a recordset to a combo box, however some of the values are NULL and so VB returns and error.

    Therefore i am trying to convert the NULL to a blank string "" or 0 using an SQL statement

    Code:
    db.OpenRecordset("SELECT a, IIf(IsNull(a), "", a) " & _
                                        "FROM table")
    This brings up a syntax error.... can NE one shed any light on this...

    Ta
    Like i see you are selecting the same column twice the first time it is a the second time you didn't gave an alias to the column. This could be the problem.
    try to change it to.
    VB Code:
    1. db.OpenRecordset("SELECT IIf(IsNull(a), '', a) as a " & _
    2.                                     "FROM table")
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117
    In the SQL statemnt i have:
    Code:
    Set rs = db.OpenRecordset("SELECT a, IIf(IsNull(a), "", a) " & _
                                        "FROM table")
    If i ad 'as a' to IIF statement, an error occurs.
    When filling the combo box i have:
    Code:
    rs.movefirst
    While Not rs.EOF
        MsgBox ("a: " & rs.Fields("a"))
           cmb.AddItem rs.Fields("a") & ""
           rs.MoveNext
        Wend
    The IIF stament is still not workin tho bcause i have replaced the "" with text and it dosent show in the message box.
    The relevant items ARE added to the combo box, but when moving through the recordset (rs.movenext), when a null field is encountered, i get the 'invalid use of NULL'.

    I have also tried it without the IIF staement, and just the combo box bit '& "" ' but that dont work either.
    Last edited by boyracer38; Aug 9th, 2002 at 04:18 AM.

  9. #9
    Hyperactive Member buddu's Avatar
    Join Date
    Jul 2001
    Location
    India
    Posts
    446

    try this sql statement

    Code:
    SELECT  CASE WHEN  a is null  THEN  '' else a  END from table
    prasad

  10. #10
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Originally posted by boyracer38
    The IIF stament is still not workin tho bcause i have replaced the "" with text and it dosent show in the message box.
    The relevant items ARE added to the combo box, but when moving through the recordset (rs.movenext), when a null field is encountered, i get the 'invalid use of NULL'.

    I have also tried it without the IIF staement, and just the combo box bit '& "" ' but that dont work either.
    The iif statement is working try to fill your combo with
    cmb.AddItem rs.Fields(1) & ""

    Else if you add as a you have to remove the first a from the sql string
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117
    Originally posted by swatty

    The iif statement is working try to fill your combo with

    cmb.AddItem rs.Fields(1) & ""

    Else if you add as a you have to remove the first a from the sql string
    Where does the (1) come from? is that the index of the field or something like that?

  12. #12
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    The 1 is the number of the field you retrieved starting at 0

    So if you select a, b, c from d then you can access the fields without giving the name of the column (a,b,c)

    here you could use rs.fields(0), rs.fields(1), rs.fields(2)

    Cause you wouldn't give an alias to the second column you cannot access it through its name cause you doesn't have a name.
    Therefore i placed the 1 instead cause you didn't read my post well.
    if you set the sql like ive mentioned
    VB Code:
    1. db.OpenRecordset("SELECT IIf(IsNull(a), '', a) as a " & _
    2.                                     "FROM table")
    You could access the field like rs.fields("a")
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  13. #13
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Why not just eliminate the nulls from your recordset using a where condition in your SQL statement:
    VB Code:
    1. db.OpenRecordset("SELECT  a " & _
    2.                               "FROM table " & _
    3.                               "Where a Is Not Null")
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117
    If i eliminate the nulls, the data will be out of sync with the data in rest of the table..... wont it??

  15. #15
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Sorry to change tack, but you could use the following statement in Access. May not work with a vb front end tho..

    Select NZ([a],"Unknown") As ThisField From Tablename


    As to the 'out of sync' .... you only have one field being returned and therefore no ids or anything to pull any other data. So, no, in that respect.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  16. #16
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    Of if you keep getting invalid use of null.. stop using null. Try switching tracks to if len(field)>0.

  17. #17
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Ummm.....

    "SELECT a, IIf(IsNull(a), "", a) " & _
    "FROM table"

    Doesn't anyone else see anything wrong with this statement?

    If I am reading it correctly, that is going to collapse to:

    SELECT a, IIf(IsNull(a), ", a) FROM table

    Instead of:

    SELECT a, IIf(IsNull(a), "", a) FROM table

    Which is what I think the author intended.

    To make the second occur, use:

    "SELECT a, IIf(IsNull(a), """", a) " & _
    "FROM table"
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

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