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.
What's causing errors is when CInt(Requests.[Approved/Un approved]) 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).
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"
What's causing errors is when CInt(Requests.[Approved/Un
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 & "'" & ")" & _
so if this line was giving you trouble:
"AND Requests.Department = " & "'" & strDepartment & "'" & ")" & _
change it to this:
"AND Nz(Requests.Department, '') = " & "'" & strDepartment & "'" & ")" & _
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].[App roved/Unap proved], " & _
"[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/Un approved]) = [Approved/Unapproved].ID " & _
"AND CInt(Requests.[Request Type]) = [Type of Leave].ID " & _
"ORDER BY RequestID"
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
"[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/Un
"AND CInt(Requests.[Request Type]) = [Type of Leave].ID " & _
"ORDER BY RequestID"
"AND Nz(Requests.RequestTypeID, 0) = RequestTypes.RequestTypeID " & _
"AND Nz(CInt(Requests.[Approved /Unapprove d]), 0) = [Approved/Unapproved].ID " & _
"AND Nz(CInt(Requests.[Request Type]), 0) = [Type of Leave].ID " & _
"AND Nz(CInt(Requests.[Approved
"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"
Try to use the Nz function:
CInt(Nz(Requests.[Approved /Unapprove d],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
CInt(Nz(Requests.[Approved
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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.
isnull(fieldname,' ')