reporting on fortnightly periods

Happy YN

Registered User.
Local time
Today, 01:07
Joined
Jan 27, 2002
Messages
425
Sorry -posted this as a reply to another post by mistake!

I have a table into which I append various offences of pupils with the dates. I want a user to be able to pull out instances of where any pupil scored 3 or more offences in any fortnightly period. (I want them to have the option to decide whether to search on 3 times the same offence or 3 times any offence but I think I can handle that. What I am not sure of is to query the table for the last 3 months and only bring out those pupils who have more than 3 in each fortnight (not any 14 days - rather from mon to mon 2 weeks later)

Any ideas anyone
Thanks
 
This might be a bit simple(I'm tired ahhhh!).

If you run a query once a week on all pupils offending in the previous week and then using a count query tag a new field in their records with todays date if three offences in the previous fourteen days?

Then for your three monthly report filter for tag dates in the preceding 3 months

If you open the db every day, you could hide the update query in the startup routine.
 
Thanks fornatian
i understand what you are saying but the way you described has its drawbacks. the user wants to pull out the info whenever they want. they do not want the pressure of having to run weekly/fortnightly queries even if the tagging gets done automatically. Also supposing the user changes the rules and only wants to see those that have 3 offences in 3 weeks or a month what then?
I had hoped for a solution as aquery which I could let the user modify the criteria via a form
Thanks
 
I believe you have three options, neither of which I am suitability quick at perfecting to present any examples

1. use querydefs to loop your dataset running a dynamic query for each offence date to get a count and tag each record for viewing and clear afterwards or

2. replicate the same in a subquery select statement

3. use a pivot table?(urggh!)
 
Thanks Fornatian
I'd be grateful if anyone could elaborate or suggest anything simpler!
 
The following should solve it for you, if you need any further explanation just let me know. I've assumed the school week is Monday to Friday. The date you enter should be a date after the last Friday you want included in the report, eg. if you run for 17/01/2003 for 3 months then you will get results for the 3 months from 21/10/2002 to 10/01/2003. If you want to have the 3 months running from 28/10/2002 to 17/01/2003 you will have to enter a date from 18th to 23rd.

Function FrstDay(D As Variant, ReqWeekday As Integer) As Date
' Returns the date of the first specified day in a month
' FirstWeekDay 1 - Sunday, 2 - Monday, 3 - Tuesday, etc.

FrstDay = NextDay(EndOfMonthPrev(D), ReqWeekday)

End Function

Function NextDay(D As Variant, DayCode As Integer) As Variant
' Returns the date of the next DayCode (1=Sun ... 7=Sat) after the
' date D.

NextDay = D - WeekDay(D) + DayCode + IIf(WeekDay(D) < DayCode, 0, 7)

End Function

Function EndOfMonthPrev(D As Variant) As Variant
' Returns the date representing the last day of the previous month.
' Arguments:
' D = Date

EndOfMonthPrev = DateSerial(Year(D), Month(D), 0)

End Function

Public Function RunOffencesRep(D As Variant, intMonths As Integer)
' Accepts a date equivalent to latest date of report and finds pupils
' with 3 or more offences in any consecutive 2 week period in the previous
' intMonths months, a pupil may appear more than once but for different
' consecutive fortnights
' D = Date, this date is not included in the report
' intMonths = no of months the report is to run back for

Dim dteMax As Date, dteMin As Date, dteMonday As Date, dteFriday As Date
Dim strSQL As String
Dim db As DAO.Database

dteMax = D
dteMin = dteMax - (intMonths * 30)
dteMonday = FrstDay(dteMin, 2)

' find first Monday equivalent to intMonths months ago
Do While dteMonday < dteMin
dteMonday = dteMonday + 7
Loop

' find Friday of 2 weeks later
dteFriday = dteMonday + 11

' need to clear out records from report table
strSQL = "DELETE * from Off3M"
' do delete using strSQL
Set db = CurrentDb
db.Execute strSQL
rst.Close
db.Close

' continue for each week until end date reached
Do While dteFriday < (dteMax - 1)
' Find pupils who have 3 or more offences for two weeks between
' dteMonday and dteFriday
FindPupils dteMonday, dteFriday
' move on one week to Monday & Friday of next fortnight
dteMonday = dteMonday + 7
dteFriday = dteFriday + 7
Loop

' open report showing details of pupils with 3 or more offences
'DoCmd.OpenReport "3orMore", acViewPreview

End Function

Public Function FindPupils(dteMon As Date, dteFri As Date)
' find pupils with 3 or more offences between the dates received
' and append a record to a table

Dim strSQL As String
Dim db As DAO.Database
Dim rstOff As DAO.Recordset

' assuming table called Offences with PupilID, OffenceDate and OffenceID then use SQL to get pupils
' with 3 or more offences
strSQL = "SELECT Offences.PupilID, Count(Offences.OffenceID) AS Offences FROM Offences WHERE "
strSQL = strSQL & "(((Offences.OffenceDate) Between #" & Format(dteMon, "mm/dd/yyyy") & "# And #" & Format(dteFri, "mm/dd/yyyy") & "#)) "
strSQL = strSQL & "GROUP BY Offences.PupilID HAVING (((Count(Offences.OffenceID))>=3));"

Set db = CurrentDb
' open a recordset with strSql
Set rstOff = db.OpenRecordset(strSQL)

' loop through the recordset and append to a table containing pupils with 3 or more offences
' append PupilID, No of Offences, dteMon and dteFri
Do While Not rstOff.EOF
strSQL = "INSERT INTO Off3M ( PupilID, Offences, StartDate, EndDate ) "
strSQL = strSQL & "VALUES (" & rstOff.PupilID & ", " & rstOff.Offences & ", #" & dteMon & "# , #" & dteFri & "#);"
' do append using strSql
db.Execute strSQL
rstOff.MoveNext
Loop

rstOff.Close
db.Close

End Function
 
Thanks antomack!
I am impressed you've really put yourself out for me and explained clearly how I can loop using your functions which you have worked at!
What I need to work out basing myself on your code is some form which users can adapt the sql to search within their designated periods at their specification i.e. maybe the user would want to know instances of 5 offences in 3 weeks etc.
I don't know why you had to delete offences and could not simply query them and append them as needed keeping the original table intact/
Nevertheless I shall work on your code Thanks once again
Happy YN
 
I have made a few changes to the code in the two main function to allow the user to set no. of weeks and no. of offences. So essentially you will just need a form to allow setting of the date (a calendar control might be best), and 3 combos; one to allow selection of no of months, another to allow selection of no of offences and the third to allow selection of no of weeks. Then just need to call the function behind a button

RunOffencesRep date, months, offences, weeks


I use a delete and an append because I am resetting the report table for the new run and then adding the records for only the current run. Without the delete the table would contain all data from all runs of the report and thus the report would contain data other than from current run. A make-table could be used but make table queries can often cause your database to increase in size unnecessarily.

Public Function RunOffencesRep(D As Variant, intMonths As Integer, intOff As Integer, intWeeks As Integer)
' Accepts a date equivalent to latest date of report and finds pupils
' with intOff or more offences in any consecutive intWeeks week period in the previous
' intMonths months, a pupil may appear more than once but for different
' consecutive intWeeks
' D = Date, this date is not included in the report
' intMonths = no. of months the report is to run back for
' intOff = no. of offences that pupil must have to be included
' intWeeks = no. of consecutive weeks to search within

Dim dteMax As Date, dteMin As Date, dteMonday As Date, dteFriday As Date
Dim strSQL As String
Dim db As DAO.Database

dteMax = D
dteMin = dteMax - (intMonths * 30)
dteMonday = FrstDay(dteMin, 2)

' find first Monday equivalent to intMonths months ago
Do While dteMonday < dteMin
dteMonday = dteMonday + 7
Loop

' find Friday of intWeeks weeks later
dteFriday = dteMonday + 4 + (7 * (intWeeks - 1))

' need to clear out records from report table
strSQL = "DELETE * from Off3M"
' do delete using strSQL
Set db = CurrentDb
db.Execute strSQL
rst.Close
db.Close

' continue for each week until end date reached
Do While dteFriday < (dteMax - 1)
' Find pupils who have intOff or more offences for intWeeks weeks between
' dteMonday and dteFriday
FindPupils dteMonday, dteFriday, intOff
' move on one week to Monday & Friday of next intWeeks
dteMonday = dteMonday + 7
dteFriday = dteFriday + 7
Loop

' open report showing details of pupils with 3 or more offences
'DoCmd.OpenReport "3orMore", acViewPreview

End Function

Public Function FindPupils(dteMon As Date, dteFri As Date, intOff As Integer)
' find pupils with 3 or more offences between the dates received
' and append a record to a table

Dim strSQL As String
Dim db As DAO.Database
Dim rstOff As DAO.Recordset

' assuming table called Offences with PupilID, OffenceDate and OffenceID then use SQL to get pupils
' with intOff or more offences
strSQL = "SELECT Offences.PupilID, Count(Offences.OffenceID) AS Offences FROM Offences WHERE "
strSQL = strSQL & "(((Offences.OffenceDate) Between #" & Format(dteMon, "mm/dd/yyyy") & "# And #" & Format(dteFri, "mm/dd/yyyy") & "#)) "
strSQL = strSQL & "GROUP BY Offences.PupilID HAVING (((Count(Offences.OffenceID))>=" & intOff & "));"

Set db = CurrentDb
' open a recordset with strSql
Set rstOff = db.OpenRecordset(strSQL)

' loop through the recordset and append to a table containing pupils with intOff or more offences
' append PupilID, No of Offences, dteMon and dteFri
Do While Not rstOff.EOF
strSQL = "INSERT INTO Off3M ( PupilID, Offences, StartDate, EndDate ) "
strSQL = strSQL & "VALUES (" & rstOff.PupilID & ", " & rstOff.Offences & ", #" & dteMon & "# , #" & dteFri & "#);"
' do append using strSql
db.Execute strSQL
rstOff.MoveNext
Loop

rstOff.Close
db.Close

End Function
 
I don't know why you are being so kind to me but i see that over 50 views have been recorded so I am sure that many have benefitted from your efforts!
Thanks again I shall certainly try all this1
Happy YN
 

Users who are viewing this thread

Back
Top Bottom