Solved

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

Posted on 2011-03-08
12
398 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 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

22 Experts available now in Live!

Get 1:1 Help Now