Link to home
Start Free TrialLog in
Avatar of AronMcD
AronMcD

asked on

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

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).

Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

What fields and I can rewrite it for you, unless you know how do this.

isnull(fieldname,' ')
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 & "'" & ")" & _
Avatar of AronMcD
AronMcD

ASKER

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"
"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 " & _
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

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
ASKER CERTIFIED SOLUTION
Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AronMcD

ASKER

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.
..did you try mine?
i don't know why you completely ignored all my posts, considering i posted a correct solution twice...
Avatar of AronMcD

ASKER

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.