Results 1 to 9 of 9
  1. #1
    rotorque is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2009
    Location
    UK
    Posts
    5

    Counting records over a 24hr period

    I have been struggling with an issue in access. I need to count activity across a 24 hr period from 8:00 am in the morning to 07:59 the following morning. This database contains activity for each day for a whole year.

    Below I have listed an example giving a number for each row date, time and activity fields/cells. Each data item is held in a separate field as follows:

    No. Date – Time – Activity

    1 - 01/02/2011 – 07:00 – Carry out activity…
    2 - 01/02/2011 – 07:22 – Clear down last…
    3 - 01/02/2011 – 07:40 – Meet with…
    4 - 01/02/2011 – 08:24 – Clear down last…
    5 - 01/02/2011 – 10:40 – Meet with…
    6 - 01/02/2011 – 16:40 – Plan activity 23232…
    7 - 01/02/2011 – 23:40 – Copy out resource checklist…
    8 - 02/02/2011 – 01:40 – Deliver checklist…
    9 - 02/02/2011 – 03:01 – Clear meeting room…
    10 - 02/02/2011 – 08:01 – Clear meeting room…


    11 - 02/02/2011 – 08:30 – Clear down last…


    As you can see that rows:
    1 to 3 would be counted as 3 activities
    4 to 9 would be counted together as 5 activities
    10 to 11 would be counted as 2 activities in the next 24hr period

    Can anyone put me on the right path please?

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I would create a query that brings in all of the fields of your table, then add a calculated field that calculates the starting date of the period. Something like this:

    IIF(mytime<#08:00#,dateadd("d",-1,mydate) , mydate) as periodstart

    I used mytime and mydate in place of your time and date field names respectively since time and date are reserved words in Access and it is generally not recommended to use reserved words as table or field names.

    Save the query above. Create your counting query based on this new query and group by the periodstart field.

    I believe SQL always interprets dates in the mm/dd/yyyy format, so I don't know how that will impact the query.

  3. #3
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209

    Counting records over a 24hr period

    I have copied the sample data that you have given into an Excel Sheet and transferred to Access as a table. Added a new field grp (Numeric)to the table. Created an Index with Date and Time Fields.

    Written a VBA Routine to scan through the Date & Time fields and gives a group sequence Number in the grp field for records that falls within the time range 08:00 to 07:59 (on the same day or next day).

    The VBA Routine is given below:

    Code:
    Public Sub Grouping()
    Dim db As Database, rst As Recordset
    Dim counter As Integer, T1 As Date, T2 As Date
    Set db = CurrentDb
    Set rst = db.OpenRecordset("Sheet11", dbOpenTable)
    rst.Index = "DateTime"
    
    counter = 0
    T1 = rst![xDate] + rst![Time]
    If rst![Time] < TimeValue("08:00") Then
        T2 = rst![xDate] + TimeValue("08:00")
    Else
        T2 = rst![xDate] + TimeValue("08:00") + 1
    End If
    
    Do While Not rst.EOF
        counter = counter + 1
    
        Do While T1 < T2 And Not rst.EOF
           rst.Edit
           rst![Grp] = counter
           rst.Update
           rst.MoveNext
           If Not rst.EOF Then
              T1 = rst![xDate] + rst![Time]
           End If
        Loop
        If Not rst.EOF Then
           If rst![Time] < TimeValue("08:00") Then
              T2 = rst![xDate] + TimeValue("08:00")
           Else
              T2 = rst![xDate] + TimeValue("08:00") + 1
           End If
        End If
    Loop
    rst.Close
    
    Set rst = Nothing
    Set db = Nothing
    
    End Sub
    Once all the records have the grouping sequence Numbers (the Table Datasheet View image after run of the program is attached) you can create a Total Query GROUP on the group-sequence number and take Count of each group. Sample SQL is given below:

    Code:
    SELECT Sheet11.grp, Count(Sheet11.grp) AS CountOfgrp
    FROM Sheet11
    GROUP BY Sheet11.grp;
    The Query output image is attached.

    If the data file have large amount of records it may take few minutes to complete the process.

    The program is tested only with the sample data that you have provided. If the code needs further refinement you may do so.

  4. #4
    rotorque is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2009
    Location
    UK
    Posts
    5

    Look for a specific item or items over a 13hr period

    My goodness thank you both very much for your help. Both solutions look great. I will try them both so that I can learn.

    One more question occurs to me because I know I will be asked!:

    How difficult would it be to change the code/query to look for a specific item/s over a 13hr period?

    Many thanks

  5. #5
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    How difficult would it be to change the code/query to look for a specific item/s over a 13hr period?
    If you are looking for a specific record or records and not counting, the query would be different. Could you provide an example?

  6. #6
    rotorque is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2009
    Location
    UK
    Posts
    5
    An example would be that I would want to count the time (group the records and count them as well) of all the activites between (a) 09:00 and 22:00 on the same date as well as say (b) 22:00 and 09:00 across midnight. using the example above:

    So (a) would be
    5 - 01/02/2011 – 10:40 – Meet with…
    6 - 01/02/2011 – 16:40 – Plan activity 23232…


    So (b) would be
    1 - 01/02/2011 – 07:00 – Carry out activity…
    2 - 01/02/2011 – 07:22 – Clear down last…

    3 - 01/02/2011 – 07:40 – Meet with…
    4 - 01/02/2011 – 08:24 – Clear down last…

    Total 4 and

    7 - 01/02/2011 – 23:40 – Copy out resource checklist…
    8 - 02/02/2011 – 01:40 – Deliver checklist…

    9 - 02/02/2011 – 03:01 – Clear meeting room…
    10 - 02/02/2011 – 08:01 – Clear meeting room…
    11 - 02/02/2011 – 08:30 – Clear down last…

    Total 5

    Does this make sense?

  7. #7
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I think something along these lines would work. You would construct your counting query based on this query and group by the PeriodStartDateTime field. You will have to adjust the field & table names to match your own


    SELECT table2.mydate, table2.mytime, table2.myactivity, iif(mytime>=#09:00:00# and mytime<=#22:00:00#, Cdate(clng(mydate)+cdate(#09:00:00#)), IIF(mytime>#22:00:00#,cdate(clng(mydate)+cdate(#22 :00:00#)),cdate(clng(dateadd("d",-1,mydate))+cdate(#22:00:00#)))) as PeriodStartDateTime
    FROM table2;

  8. #8
    rotorque is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2009
    Location
    UK
    Posts
    5
    Thank you for responding so quickly, I haven't had a chance to look at this until today. I will take you advice and try it out this week.
    Many thanks again

  9. #9
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You're welcome, and please let us know if you run into any issues.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Counting Only Certain Records
    By jtphenom in forum Queries
    Replies: 9
    Last Post: 03-29-2011, 01:25 AM
  2. Counting Records
    By WhiteNite1971 in forum Access
    Replies: 1
    Last Post: 01-22-2011, 06:36 AM
  3. Counting Active Records Between 2 Dates
    By catat in forum Queries
    Replies: 5
    Last Post: 07-28-2010, 10:55 AM
  4. Counting returned records in a query
    By johncob in forum Queries
    Replies: 0
    Last Post: 02-11-2009, 05:30 PM
  5. Counting records based on specified criteria
    By GrnISSO in forum Queries
    Replies: 2
    Last Post: 10-04-2007, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums