简体   繁体   中英

MS Access Query SQL Union Select returns -1

I have a MS Access Query with a SQL Union Select that returns a -1 on my reports. I assume that the -1 is because the query is returning a null value, but I cannot figure out how to use an IFF() statement to stop showing the -1 in my report. This is the SQL Query:

SELECT [Employees].[Last Name] & ", " & [First Name] AS EmployeeName, Employees.[Active Employee], Employees.[Active Employee], Employees.[Active Employee]
FROM Employees
WHERE (((Employees.[Active Employee])=True))
ORDER BY [Employees].[Last Name] & ", " & [First Name];

UNION SELECT [Employees].[Last Name] & ", " & [First Name] AS EmployeeName, TypeOfAbsence.TypeOfAbsence, [Vacation Calendar].[Date Used], [Vacation Calendar].Time
FROM TypeOfAbsence INNER JOIN (Employees INNER JOIN [Vacation Calendar] ON Employees.ID = [Vacation Calendar].EmployeeID) ON TypeOfAbsence.ID = [Vacation Calendar].TypeID
WHERE ((([Vacation Calendar].[Date Used])<[Forms]![Week Ending Report Form]![End_Date] And ([Vacation Calendar].[Date Used])>[Forms]![Week Ending Report Form]![Start_Date]));

Consider using Null as field placeholder for UNION. This will eliminate the -1 values from the repeated [Active Employee] fields. Remove embedded semi-colon as that will cause UNION to fail. ORDER BY clause is likely useless because sorting should be managed by report and if report Sorting & Grouping is set, the query order will definitely be disregarded. First SELECT line defines field data types and names. Do not need to repeat alias field names in subsequent SELECT lines.

SELECT [Last Name] & ", " & [First Name] AS EmployeeName, Null AS TOA, Null AS DU, Null AS TU
FROM Employees
WHERE (((Employees.[Active Employee])=True))

UNION SELECT [Last Name] & ", " & [First Name], TypeOfAbsence, [Date Used], [Time] 
FROM TypeOfAbsence 
INNER JOIN (Employees INNER JOIN [Vacation Calendar] 
ON Employees.ID = [Vacation Calendar].EmployeeID) 
ON TypeOfAbsence.ID = [Vacation Calendar].TypeID
WHERE ((([Vacation Calendar].[Date Used])<[Forms]![Week Ending Report Form]![End_Date] And ([Vacation Calendar].[Date Used])>[Forms]![Week Ending Report Form]![Start_Date]));

Also consider this filter criteria:
WHERE [Date Used] BETWEEN [Forms]![Week Ending Report Form]![Start_Date] AND [Forms]![Week Ending Report Form]![End_Date]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM