Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2008, 03:12 PM
Authorized User
 
Join Date: Jan 2006
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with null value

i'm using a form to create a new record in a table. there are 3 text boxes to enter data into and depending on the info, 1 or 2 of the text boxes may be blank. so i'm using this null value code to enter my data to my table and then i string it into a SQL string:

        If IsNull(Me.TxtPlatNo) Or Me.TxtPlatNo = "" Then
            strPlatNo = ""
        Else
        strPlatNo = Me.TxtPlatNo
        End If

whats happening is, if TxtPlatNo is left empty the field in the table remains empty but when i use a query to search all the records in that field where "not is null" these blank entries are showin up in my query search even though the field is blank. can anyone explain to me why this is happening and most importantly how i can correct this???? i need to retrieve all the records in that field that contain data and exclude the rest of the records that dont.

 
Old October 16th, 2008, 05:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Blank is *NOT* the same as NULL.

A blank string is still a string and is never considered to be a NULL.

You could do
     SELECT ... FROM ... WHERE IIF( field IS NULL, '', field ) <> '' ...
or, equivalently,
     SELECT ... FROM ... WHERE IIF( ISNULL(field), '', field ) <> '' ...
 
Old October 17th, 2008, 09:42 AM
Authorized User
 
Join Date: Jan 2006
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry but i need some clarification here.
1. are you saying i should use this SELECT.... FROM statement in my search query???

2. if the field is blank and that is not considered to be a null value cant i filter whatever value a blank field would be??

i'm trying to use your SELECT .... FROM statement but i cant seem to get it to work. i'm working with a text field and not a number so i'm guessing the <> wouldn't work with text.

can you shed a little more light on this. here's the SQL string i'm using on my form:

strSQL = "INSERT INTO T_Survey(WOID,RecordingNo,PlatNo,Qtr) " & " VALUES ('" & strWOID & "', '" & strRecordingNo & "', '" & strPlatNo & "', '" & strQtr & "')"

 
Old October 17th, 2008, 12:15 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

By inserting empty strings, as in: strPlatNo = "", the field is no longer Null. It is just an empty string.

In your subsequent searches, you can search for Null fields AND empty strings. That is what the old dude is suggesting. That would be AFTER you had already inserted an empty string.

If you want to prevent empty strings from being inserted, then you need to modify your code so that if the field was Null, then you didn't insert an empty string instead. You would have to modify your SQL INSERT string in order to do that. Like:

If all fields had values:
strSQL = "INSERT INTO T_Survey(WOID,RecordingNo,PlatNo,Qtr) " & " VALUES ('" & strWOID & "', '" & strRecordingNo & "', '" & strPlatNo & "', '" & strQtr & "')"

If PlatNo did not have a value:
strSQL = "INSERT INTO T_Survey(WOID,RecordingNo,Qtr) " & " VALUES ('" & strWOID & "', '" & strRecordingNo & "', '" & strQtr & "')"

Is that the sort of thing you want to do dynamically? Or do you just want to change the search parameters to include empty strings AND Nulls?




mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old October 17th, 2008, 03:37 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

To answer you directly:

1. are you saying i should use this SELECT.... FROM statement in my search query???

Yes, of course.

2. if the field is blank and that is not considered to be a null value cant i filter whatever value a blank field would be??

My code will filter on *EITHER* a blank field *OR* a null value. It converts the nulls *TO* blanks. And then looks for fields that are not blank (or not-blank-after-the-null-is-converted-to-blank).

Yes, of course you can do
     <> ''
with text. You could *NOT* do that if the field was indeed numeric!!!
 
Old November 4th, 2008, 11:42 AM
pjm pjm is offline
Authorized User
 
Join Date: Jul 2006
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just to put in my 2 cents:

I find the NZ() function to be an invaluable tool when dealing with nulls in either text or numeric fields.

-Phil-





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Is A Null Different from Another Null ramk_1978 SQL Language 9 February 12th, 2005 03:18 PM
when to use is Null and =null and"null shoakat Classic ASP Databases 3 October 29th, 2004 01:47 AM
Biztalk Stored Procedure problems with NULL value mtlili Biztalk 0 September 21st, 2004 05:32 AM
Problems in ViewStates[]. Null Reference? chenzw BOOK: ASP.NET Website Programming Problem-Design-Solution 0 April 6th, 2004 08:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.