|
-
Aug 7th, 2002, 05:24 AM
#1
Thread Starter
Lively Member
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
-
Aug 7th, 2002, 05:47 AM
#2
-= B u g S l a y e r =-
instead of doing it in the SQL statement, do it when you fill the combo
VB Code:
while not rs.eof
cbo.AddItem rs("field") & ""
rs.movenext
wend
-
Aug 7th, 2002, 06:33 AM
#3
Thread Starter
Lively Member
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'
-
Aug 7th, 2002, 09:16 AM
#4
-= B u g S l a y e r =-
again 
cbo.AddItem rs("field") & ""
-
Aug 9th, 2002, 03:14 AM
#5
Thread Starter
Lively Member
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.
-
Aug 9th, 2002, 03:19 AM
#6
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:
db.OpenRecordset("SELECT a, [i]IIf(IsNull(a), "", a)[/i] " & _
"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
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...
-
Aug 9th, 2002, 03:46 AM
#7
Frenzied Member
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:
db.OpenRecordset("SELECT IIf(IsNull(a), '', a) as a " & _
"FROM table")
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Aug 9th, 2002, 04:13 AM
#8
Thread Starter
Lively Member
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.
-
Aug 9th, 2002, 04:23 AM
#9
Hyperactive Member
try this sql statement
Code:
SELECT CASE WHEN a is null THEN '' else a END from table
-
Aug 9th, 2002, 05:06 AM
#10
Frenzied Member
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
-
Aug 9th, 2002, 05:37 AM
#11
Thread Starter
Lively Member
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?
-
Aug 9th, 2002, 06:04 AM
#12
Frenzied Member
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:
db.OpenRecordset("SELECT IIf(IsNull(a), '', a) as a " & _
"FROM table")
You could access the field like rs.fields("a")
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Aug 9th, 2002, 06:33 AM
#13
Fanatic Member
Why not just eliminate the nulls from your recordset using a where condition in your SQL statement:
VB Code:
db.OpenRecordset("SELECT a " & _
"FROM table " & _
"Where a Is Not Null")
Chris
Master Of My Domain
Got A Question? Look Here First
-
Aug 9th, 2002, 08:06 AM
#14
Thread Starter
Lively Member
If i eliminate the nulls, the data will be out of sync with the data in rest of the table..... wont it??
-
Aug 9th, 2002, 09:41 AM
#15
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
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...
-
Aug 9th, 2002, 09:47 AM
#16
Hyperactive Member
Of if you keep getting invalid use of null.. stop using null. Try switching tracks to if len(field)>0.
-
Aug 9th, 2002, 10:57 AM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|