Question

I have a query I have been trying to refine to allow for user research to compare year over year by month information. The query is working fine with the exception of a number grouping issue I have not figured out how to solve. The query prompts the user to input start year and end year, then start month and end month. Everything runs fine for months 2-9, but when 1, 10, 11, or 12 are used all fields with a month number starting in 1 appear. so I request this year and last year months 1 & 2 I get 1(ty) 1(ly) 2(ty) 2(ly) 10(ty) 10(ly) 11(ty) 11(ly) 12(ty) 12(ly). How do only return the specific numeric range I requested?

SELECT Month([EventLog]![Date]) & "/" & Year([EventLog]![Date]) AS [Date]
    ,EventLog.Type
    ,Count(EventLog.UserID) AS [Attendance Events]
    ,Year([EventLog]![Date]) AS [Year]
    ,Month([EventLog]![Date]) AS [Month]
FROM EventLog
WHERE (((EventLog.LogType) Like "Eve*"))
GROUP BY Month([EventLog]![Date]) & "/" & Year([EventLog]![Date])
    ,EventLog.Type
    ,Year([EventLog]![Date])
    ,Month([EventLog]![Date])
HAVING (((EventLog.Type) Like "Att*") 
    AND ((Year([EventLog]![Date])) Between [Enter Start Year:] And [Enter End Year:]) 
    AND ((Month([EventLog]![Date])) Between [Enter Start Month:] And [Enter End Month:]))
ORDER BY Month([EventLog]![Date]);
Was it helpful?

Solution

The prompt values are strings, but are being compared to numeric values. Try:

AND ((Year([EventLog]![Date])) Between CInt([Enter Start Year:] )
                               And CInt([Enter End Year:]) )
AND ((Month([EventLog]![Date])) Between CInt([Enter Start Month:] )
                                And CInt([Enter End Month:])))

OTHER TIPS

You can include a PARAMETERS clause at the start of your query to notify the db engine that your parameters are numbers. Then when you reference the parameters in the query, you don't need to transform them (from text) to numbers because as far as the db engine is concerned, they are numbers already.

The beginning of your SQL would look like this ...

PARAMETERS [Enter Start Year:] Short, [Enter End Year:] Short;
SELECT Month([EventLog]![Date]) ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top