Solved

Null values in my table are causing problems in my join query

Posted on 2011-03-08
12
432 Views
Last Modified: 2013-11-27
Hello,
I'm joining multiple tables to create a report.  It works fine unless I run into NULL values in the join statement.  What can I do to fix this?  Below is my query.


 strQuery = "SELECT Requests.*, RequestTypes.RequestTypeID, RequestType, [Approved/Unapproved].ID, [Approved/Unapproved].[Approved/Unapproved], " & _
                    "[Type of Leave].ID , TypeOfLeave " & _
                    "FROM Requests, RequestTypes, [Approved/Unapproved], [Type of Leave] " & _
                    "WHERE ((Requests.StartDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#" & _
                    "OR Requests.EndDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#)" & _
                    "AND Requests.Department = " & "'" & strDepartment & "'" & ")" & _
                    "AND Requests.RequestTypeID = RequestTypes.RequestTypeID " & _
                    "AND CInt(Requests.[Approved/Unapproved]) = [Approved/Unapproved].ID " & _
                    "AND CInt(Requests.[Request Type]) = [Type of Leave].ID " & _
                    "ORDER BY RequestID"

Open in new window


What's causing errors is when CInt(Requests.[Approved/Unapproved])  is NULL (this is a text field in my table).  And when CInt(Requests.[Request Type]) is NULL (this is also a text field in my table).

0
Comment
Question by:AronMcD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 11

Expert Comment

by:N R
ID: 35071651
What fields and I can rewrite it for you, unless you know how do this.

isnull(fieldname,' ')
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35071697
In access, its actually Nz()

so if this line was giving you trouble:

 "AND Requests.Department = " & "'" & strDepartment & "'" & ")" & _

change it to this:

 "AND Nz(Requests.Department, '') = " & "'" & strDepartment & "'" & ")" & _
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35071699
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AronMcD
ID: 35071711
Gallitin,
Thank you for the quick response.  The fields that are causing me problems are in BOLD below.  The Requests.RequestTypeID is a Numeric field in my table.  I'm not sure if that will cause problems using isnull or not.  I'm not so much concerned about that though as that field is rarely left null.  Let me know if you need more.  Thanks.

strQuery = "SELECT Requests.*, RequestTypes.RequestTypeID, RequestType, [Approved/Unapproved].ID, [Approved/Unapproved].[Approved/Unapproved], " & _
                    "[Type of Leave].ID , TypeOfLeave " & _
                    "FROM Requests, RequestTypes, [Approved/Unapproved], [Type of Leave] " & _
                    "WHERE ((Requests.StartDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#" & _
                    "OR Requests.EndDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#)" & _
                    "AND Requests.Department = " & "'" & strDepartment & "'" & ")" & _
                    "AND Requests.RequestTypeID = RequestTypes.RequestTypeID " & _
                    "AND CInt(Requests.[Approved/Unapproved]) = [Approved/Unapproved].ID " & _
                    "AND CInt(Requests.[Request Type]) = [Type of Leave].ID " & _
                    "ORDER BY RequestID"
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35071724
"AND Nz(Requests.RequestTypeID, 0) = RequestTypes.RequestTypeID " & _
                    "AND Nz(CInt(Requests.[Approved/Unapproved]), 0) = [Approved/Unapproved].ID " & _
                    "AND Nz(CInt(Requests.[Request Type]), 0) = [Type of Leave].ID " & _
0
 
LVL 11

Expert Comment

by:N R
ID: 35071737
This is MS SQL
strQuery = "SELECT Requests.*, RequestTypes.RequestTypeID, RequestType, [Approved/Unapproved].ID, [Approved/Unapproved].[Approved/Unapproved], " & _
                    "[Type of Leave].ID , TypeOfLeave " & _
                    "FROM Requests, RequestTypes, [Approved/Unapproved], [Type of Leave] " & _
                    "WHERE ((Requests.StartDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#" & _
                    "OR Requests.EndDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#)" & _
                    "AND Requests.Department = " & "'" & strDepartment & "'" & ")" & _
                    "AND isnull(Requests.RequestTypeID,0) = RequestTypes.RequestTypeID " & _
                    "AND CInt(isnull(Requests.[Approved/Unapproved],0)) = [Approved/Unapproved].ID " & _
                    "AND CInt(isnull(Requests.[Request Type],0)) = [Type of Leave].ID " & _
                    "ORDER BY RequestID"

Open in new window

0
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 35071738
Try to use the Nz function:

CInt(Nz(Requests.[Approved/Unapproved],0))  

CInt(Nz(Requests.[Request Type],0))

Also check out the data type of your fields for it is uncommon to use the Cint to a text field format.

Sincerely,
Ed
0
 
LVL 11

Accepted Solution

by:
N R earned 500 total points
ID: 35071745
and then access if your using it.
strQuery = "SELECT Requests.*, RequestTypes.RequestTypeID, RequestType, [Approved/Unapproved].ID, [Approved/Unapproved].[Approved/Unapproved], " & _
                    "[Type of Leave].ID , TypeOfLeave " & _
                    "FROM Requests, RequestTypes, [Approved/Unapproved], [Type of Leave] " & _
                    "WHERE ((Requests.StartDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#" & _
                    "OR Requests.EndDate Between #" & strStartDate & "#" & " And #" & strEndDate & "#)" & _
                    "AND Requests.Department = " & "'" & strDepartment & "'" & ")" & _
                    "AND nz(Requests.RequestTypeID,0) = RequestTypes.RequestTypeID " & _
                    "AND CInt(nz(Requests.[Approved/Unapproved],0)) = [Approved/Unapproved].ID " & _
                    "AND CInt(nz(Requests.[Request Type],0)) = [Type of Leave].ID " & _
                    "ORDER BY RequestID"

Open in new window

0
 

Author Comment

by:AronMcD
ID: 35071853
I copied and pasted Gallitan's code and while I'm not getting the error anymore, I'm not getting the record that I should be getting.  I'm going to keep testing....I'll get back to you.
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35071862
..did you try mine?
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35072199
i don't know why you completely ignored all my posts, considering i posted a correct solution twice...
0
 

Author Comment

by:AronMcD
ID: 35072200
Gallitin's code worked.  I was entering in the wrong values when testing..it's been a long day.  Anyway thanks to all of you.  I appreciate it.  
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question