有没有办法在下面的 SQL 代码上设置条件?我只想显示到年底,但现在显示的是开始年份 + 5 年

Is there a way to set a condition on the SQL code below? I only want to show up until the end year but am showing the start year + 5 years right now

大家早上好我有这个 SQL 代码,我在 Access 中使用它在表格上显示每年 table 的总小时数。用户可以选择最多 6 年,这意味着他们也可以简单地选择 1、2、3、4 或 5 年。如果未选择 6,我只想显示到年底的年份,而不是完整的 6。我现在编写 sql 的方式是从开始年份开始加 1,直到到达第 6 年并显示所有年份。我也有一个结束年份的变量,我在想是否有办法输入 IF startyear + number is greater than endyear then end 但不确定如何实现。任何帮助都会非常有帮助

代码:

SELECT 
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY, 
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID, 
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours 
FROM qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear 
WHERE (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value)) 
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value)) 
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value)) 
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+1)) 
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value)) 
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+2)) 
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value)) 
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+3)) 
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value)) 
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+4)) 
Or (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID)=Forms!frm_Home.Form.cboEstimate.Value)) 
And (((qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID)=Forms!frm_Home.Form.cboStartYear.Value+5)) 
ORDER BY [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY, 
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID, 
[qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours; 

尝试这样的事情:

SELECT 
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY, 
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID, 
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours 
FROM 
    qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear 
WHERE 
    qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.EstimateID = Forms!frm_Home!cboEstimate.Value
    AND
    (qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear.YearID Between 
        Forms!frm_Home!cboStartYear.Value And 
        Forms!frm_Home!cboStartYear.Value + Forms!frm_Home!YearsToSelect.Value)  
ORDER BY 
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].EstimateID,
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].FY, 
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].YearID, 
    [qry_LaborLineItems_TotalLaborHoursPerEstimatePerYear].SumOfLaborHours; 

其中 Forms!frm_Home!YearsToSelect.Value 包含用户选择的年数。