Results 1 to 14 of 14

Thread: "Invalid use of Null"

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    malaysia
    Posts
    29

    "Invalid use of Null"

    hi, there
    i hav a form with 9 text boxes(link to Access) and i use control array. after i compile, i got msg "Invalid use of Null". and 3rd line(in BOLD)is highlighted. so where i did wrong?

    VB Code:
    1. Private Sub GiveData()
    2. For i = 0 To 9
    3. [B]txtfields(i) = Rs(i)[/B]
    4. Next
    5. End Sub

    i attach the code as well. pls let me know where i did wrong.
    thx in advanced.
    Attached Files Attached Files

  2. #2
    what does RS(i) return

  3. #3
    Member
    Join Date
    May 2000
    Posts
    63
    This should solve your problem:

    txtfields(i) = Rs(i) & ""

  4. #4
    Hyperactive Member Jlarini's Avatar
    Join Date
    Jan 2002
    Location
    São Paulo, Brazil
    Posts
    263
    bears,


    I think nareth and jmatello read, but don't take it...


    You sad "I have 9 textboxes and your for... next does 10... You may having problems with the 10th, cos it does not exist...

    João Luiz
    nothing is impossible, it's sometimes very hard to do!

    If your thread is solved... Please edit it and add [Resolved] or [Solved] on it!

    If you like Marine aquarium, feel free to PM me.

    Sorry my bad English

    God bless Parksie!

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541
    Originally posted by Jlarini
    bears,


    I think nareth and jmatello read, but don't take it...


    You sad "I have 9 textboxes and your for... next does 10... You may having problems with the 10th, cos it does not exist...

    João Luiz
    ***? Must be using Pentium math.... since the for loop is 0 TO 9 it'll never be 10, so that's isn't it. Besides, that would have generated an "Index out of bounds array" error.

    jmatello is the closest with: txtfields(i) = Rs(i) & ""

    It's actually preferrable to use vbNullString rather than "" (even though they are the same, I've seen it not work one way but work the other... go figure).

    Here's the reason why the error happens. Strings cannot be NULL, for what ever reason MS decided long ago that you could not stuff a NULL value into a string of any kind. What's happening is that one of your fields is returning a NULL value and VB can't put it into the text box, since the text property of the textbox is of type string. By appending vbNullString to the end of it, it will be converted to an empty string and allow it to be put into the text box.

    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??? *

  6. #6
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    I havent tested this in VB but I know it works in access

    there is a function NZ() (Im not sure if VB has it)

    txtfields(i) = nz(Rs(i),"")

    otherwise

    txtfields(i) = IIF(isnull(Rs(i)),"",Rs(i))

    and BTW.. techgnome...he is right.. the for next loop will loop 10 times


    0 to 9...if he only has 9 textboxes then he is missing one
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541
    Originally posted by [LGS]Static
    I havent tested this in VB but I know it works in access

    there is a function NZ() (Im not sure if VB has it)

    txtfields(i) = nz(Rs(i),"")

    otherwise

    txtfields(i) = IIF(isnull(Rs(i)),"",Rs(i))

    and BTW.. techgnome...he is right.. the for next loop will loop 10 times


    0 to 9...if he only has 9 textboxes then he is missing one
    1) IIF will still return an Invalid use of NULL error - it evaluate both values before determining which one to use (which defies logic if you ask me).

    2) Yes, the loop will run 10 times, 0 to 9, and yes that will cause an error (if there are indeed only 9 textboxes) on the last loop, or when it is 9, but not 10 like he had suggested. - but if that was the case, it would be an array out of bounds error, not an NULL error.

    3) I never claimed that the loop wouldn't run 10 times, I questioned the value of the loop counter. And now that re-read it (for the I don't know how many times) I see how it could be interpreted either way. There is a reason I am a programmer and not an English teacher or a Communications professor.



    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
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    c'mon TG its as clear as a foggy day in london! geez..how could you possibly misread it? lol (It took me a number of readthroughs as well)


    Does Nz() work in VB?
    and ...

    is RS(i) a recordset? array? etc?

    the Null value should not be able to get in it if its an array...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044
    Somebody mentioned the IsNull function, which works nicely, but can be a real pain in the ass.

    In situations where I run into this, I found it easier to write a short little wrapper function that takes the field, and returns a string. The string is empty if Null, or the string if not null.

    Appending on the character is another approach, I don't know which would work better. Probably just a matter of preference.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    I'm guessing RS is a recordset and RS(i) is the default property of the field value for each column...

    We always append "" to a RS - in all code - it's one of our ten-commandments here.

    So as said by several others:

    VB Code:
    1. RS(I) & ""

    should take care of the problem.

    TG - BTW, we've never had a problem with this method over the vbNullString you suggest...

  11. #11
    Hyperactive Member Jlarini's Avatar
    Join Date
    Jan 2002
    Location
    São Paulo, Brazil
    Posts
    263
    techgnome,

    You're right... and wrong (a bit)...

    It's true it will generate a "Index out of bounds array" error, cos the range of textboxes (0...8), ok... My mistake...

    But what if he read the "10th" field (9 in recordset) (I know that a recordset starts at 0! ) and the doesn't want this value but he "calls" it and it was null ?!?!?! and other ones weren't ?

    This is what I read... (my mistake, ok!)
    I didn't realize the textbox index...

    Take a look about that. (I know it's not the actual case, but...)

    João Luiz
    nothing is impossible, it's sometimes very hard to do!

    If your thread is solved... Please edit it and add [Resolved] or [Solved] on it!

    If you like Marine aquarium, feel free to PM me.

    Sorry my bad English

    God bless Parksie!

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541
    Originally posted by Jlarini
    But what if he read the "10th" field (9 in recordset) (I know that a recordset starts at 0! ) and the doesn't want this value but he "calls" it and it was null ?!?!?! and other ones weren't ?
    Then you get the "Invalid Use of NULL" error, which is what he is getting:

    Originally posted by bears
    after i compile, i got msg "Invalid use of Null". and 3rd line(in BOLD)is highlighted.
    The third line is:
    VB Code:
    1. txtfields(i) = Rs(i)
    One of the fields has a NULL value. Plain and simple.

    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??? *

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    malaysia
    Posts
    29
    hi, there...
    thx for all, your help.
    i've found the error. thx!
    Last edited by bears; Jun 17th, 2004 at 02:09 AM.

  14. #14
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    What about CStr(var)?

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