Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-08
12
Medium Priority
?
457 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 12

Expert Comment

by:Nathan Riley
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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 12

Expert Comment

by:Nathan Riley
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 12

Accepted Solution

by:
Nathan Riley earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

598 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