views:

41

answers:

1

I have a table full of items that each have a unique ItemID. There is another table with information on tests that are done on these items around once a month (some may have multiple tests per month and some may have none some months) and each test entry has a full date mm/dd/yyyy. I would like to display the average of one of the fields over all the data on a monthly basis. So assuming what I'm averaging is the item weight, I want to end up with a table like this:

Date AvgOfWeight  
1/09 24.55  
2/09 28.64  
3/09 22.39

and so on...

I know that I can do a query to do averages and I can group the averages by certain criteria, but is there an easy way to group mm/dd/yyyy dates by month?

A: 

I love this strzero function that can be used here this way:

m_monthYear = strZero(year(date()),4) & "-" & strZero(month(date()),2)

Results look like 2009-09 and can be ordered ascending, as they begin with the year. You'll just have to create this strZero function as follows:

Public function strZero(x_myLong as long, x_length as integer) as string
Dim m_strZero as string

m_strZero = trim(str(x_myLong))
do while len(m_strZero) <= x_length
    m_strZero = "0" & m_strZero
loop
strZero = m_strZero
end function

This was written on the fly. Please check all parameters (if any) for str, add error management, and adapt to your own needs. You might like to pass x_myLong as a variant, in order to return a '0000' string for example. This function has multiple other uses when converting numbers to fixed length strings.

Philippe Grondier
This is basically what i ended up doing. I did it all in SQL though. I grouped by Year: Year(Date) and then by Month: Month(Date) and then for display purposes I made an expression Year_Date: CStr(Year) + "/" + CStr(Month). THanks for the help
Matt