Search code examples
sqlvbams-access

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


Goodmorning everyone I have this SQL code that I am using in Access to show a table of total hours per year on a form. The user has the OPTION of choosing up to 6 years which means they could also simply choose 1 2 3 4 or 5 years. I only want to show the years up to the end year not the full 6 if 6 aren't selected. The way I have the sql written right now takes the start year and adds 1 to it until it reaches year 6 and displays them all. I have a variable for the end year as well and was thinking if there is a way to put in IF startyear + number is greater than endyear then end but not sure how to achieve that. Any help would be super helpful

code:

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; 

Solution

  • Try something like this:

    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; 
    

    where Forms!frm_Home!YearsToSelect.Value holds the user's choice of count of years.