Solved

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

Posted on 2011-03-08
12
385 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
  • 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now