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:
Private Sub GiveData()
For i = 0 To 9
[B]txtfields(i) = Rs(i)[/B]
Next
End Sub
i attach the code as well. pls let me know where i did wrong.
thx in advanced.
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.
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.
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.
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!
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:
txtfields(i) = Rs(i)
One of the fields has a NULL value. Plain and simple.