UNION SELECT Problem

D

DS

I'm having a problem with this UNION query.
The sort order is saying that it can't sort on the [EmpFrstName] & "" ""
& [EmpLastName], it says that it isn't included. Also can I use Union
All as opposed to just UNION?
Thanks
DS

Me.RecordSource = "SELECT tblChecks.ChkBizDay, [EmpFirstName] & "" "" &
[EmpLastName] AS EMP, " & _
"tblChecks.ChkDate, tblChecks.ChkTime, tblChecks.CheckID, " & _
"tblChecks.ChkAlias, tblChecks.ChkCancelled, tblChecks.ChkKillID,
tblChecks.ChkTotal " & _
"FROM tblChecks LEFT JOIN tblEmployees ON tblChecks.ChkServer =
tblEmployees.EmployeeID " & _
"WHERE (((tblChecks.ChkBizDay) >= [Forms]![frmReportDates]![TxtStart] " & _
"And (tblChecks.ChkBizDay) <= [Forms]![frmReportDates]![TxtEnd]) " & _
"And ((tblChecks.ChkCancelled) = -1) And ((tblChecks.ChkKillID) = 0)) " & _
"UNION " & _
"SELECT tblChecks.ChkBizDay, [EmpFirstName] & "" "" & [EmpLastName] AS
EMP, " & _
"tblChecks.ChkDate, tblChecks.ChkTime, tblChecks.CheckID, " & _
"tblChecks.ChkAlias, tblChecks.ChkCancelled, tblChecks.ChkKillID,
tblChecks.ChkTotal " & _
"FROM tblChecks LEFT JOIN tblEmployees ON tblChecks.ChkServer =
tblEmployees.EmployeeID IN '//Backoffice/Warehouse/History.mdb' " & _
"WHERE (((tblChecks.ChkBizDay) >= [Forms]![frmReportDates]![TxtStart] " & _
"And (tblChecks.ChkBizDay) <= [Forms]![frmReportDates]![TxtEnd]) " & _
"And ((tblChecks.ChkCancelled) = -1) And ((tblChecks.ChkKillID) = 0)) " & _
"ORDER BY [EmpFirstName] & "" "" & [EmpLastName], tblChecks.ChkDate,
tblChecks.ChkTime;"
 
A

Allen Browne

Order by the 2 fields rather than a concatenated field, i.e:
"ORDER BY [EmpFirstName], [EmpLastName], tblChecks.ChkDate, ...
(You probably want to include the 2 names as fields in their own right,
rather than ORDER BY EMP.)

Just using UNION removes duplicates. Use UNION ALL unless you explicitly
want Access to compare and remove the duplicates (which is slower.)
 
G

Guest

One problem may be that you have that combination aliased as EMP. Therefore
the following might work:

ORDER BY EMP, tblChecks.ChkDate, tblChecks.ChkTime;

If not try the following:

ORDER BY 2, 3, 4;

Then there's always the following which should work:

ORDER BY [EmpFirstName], [EmpLastName], tblChecks.ChkDate, tblChecks.ChkTime;

Yes you can use UNION ALL. It is usually faster than a UNION as it doesn't
have to look for duplicates. On the negative side, it will return duplicates,
so make sure that isn't a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

UNION QUERY (Sorting) 2
From External Database 5
Same data from 2 sources 2
Joining 2 Queries 3
UnMatched Query 3
SubQuery Problem 1
ORDER BY Problem 4
OPEN ARGS Trouble 2

Top