Results 1 to 5 of 5
  1. #1
    shod90 is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Jan 2016
    Posts
    134

    Date Difference between consecutive rows

    Dear Gents,
    Um creating attendance form that calculate the difference between the check in and check out , But i didn't know it's hard to find the difference between consecutive rows

    below is a sample from my table data that i need to calculate the difference between them
    Click image for larger version. 

Name:	Attendance.PNG 
Views:	18 
Size:	4.9 KB 
ID:	27747


    what um expceted to see this result
    05-03-2017 08:15 h:m
    06-03-2017 08:50 h:m

    And so on ....

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    there is no query to do this. 1 row cannot see another row.
    you need vb code. Date and time should have been in 1 single field.
    something like:

    Code:
    Public Sub CalcElapse()
    Dim rst   'As Recordset
    Dim sSql As String
    Dim bPrev As Boolean, bIn As Boolean
    
    sSql = "select * from table"
    Set rst = CurrentDb.OpenRecordset(sSql)
    bPrev = False
    
    With rst
        While Not .EOF
            vDat = .Fields("Date")
            vTim = .Fields("time")
            vio = .Fields("checktype")
            bIn = (vio = "I")
            
            Select Case True
               Case bIn And bPrev
                  'error  2 INs
                  GoTo skipIt
               
               Case bIn And Not bPrev
                  vStart = vDat & " " & vTim
               
               Case Not bIn And bPrev
                  vEnd = vDat & " " & vTim
               
               Case Not bIn And Not bPrev
                  'error  2 OUTs
                  GoTo skipIt
            End Select
             
            vElaps = DateDiff("n", vStart, vEnd)
                     
            If vEnd Then Debug.Print vStart, vEnd, vElaps
            vPrev = vIn
            
    skipIt:
           .MoveNext
        Wend
    End With
    
    Set rst = Nothing
    End Sub

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,439
    you can use a query,


    SELECT I.Date, format(O.Time-I.Time,"hh:nn") as Diff
    FROM myTable I INNER JOIN myTable 0 ON I.Date=O.Date
    WHERE I.CheckType="I" AND O.CheckType="O"

    Any dates without both In and Out will be excluded, so if you have someone who checked in yesterday and checked out today would not be reported - you will need a modification to the above where, as Ranman suggested, it would be a lot easier if the date and time fields are combined (you can still display the same field twice, once formatted as a date and once as time). Also the hh:nn format only works for periods of less than 24 hours, otherwise you need to use a UDF to convert to a display you require.

    Similarly, if you have multiple ins and outs for the same day, you will get multiple returns.

    Note that date and time are both reserved words, so using them may generate unexpected results. Strongly recommend you change them to something more meaningful

  4. #4
    shod90 is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Jan 2016
    Posts
    134
    Quote Originally Posted by ranman256 View Post
    there is no query to do this. 1 row cannot see another row.
    you need vb code. Date and time should have been in 1 single field.
    something like:

    Code:
    Public Sub CalcElapse()
    Dim rst   'As Recordset
    Dim sSql As String
    Dim bPrev As Boolean, bIn As Boolean
    
    sSql = "select * from table"
    Set rst = CurrentDb.OpenRecordset(sSql)
    bPrev = False
    
    With rst
        While Not .EOF
            vDat = .Fields("Date")
            vTim = .Fields("time")
            vio = .Fields("checktype")
            bIn = (vio = "I")
            
            Select Case True
               Case bIn And bPrev
                  'error  2 INs
                  GoTo skipIt
               
               Case bIn And Not bPrev
                  vStart = vDat & " " & vTim
               
               Case Not bIn And bPrev
                  vEnd = vDat & " " & vTim
               
               Case Not bIn And Not bPrev
                  'error  2 OUTs
                  GoTo skipIt
            End Select
             
            vElaps = DateDiff("n", vStart, vEnd)
                     
            If vEnd Then Debug.Print vStart, vEnd, vElaps
            vPrev = vIn
            
    skipIt:
           .MoveNext
        Wend
    End With
    
    Set rst = Nothing
    End Sub
    Actually i appreciate your efforts , But don't know where to put this code and when ? .. I know that i should create a module but then ?

    Quote Originally Posted by Ajax View Post
    you can use a query,


    SELECT I.Date, format(O.Time-I.Time,"hh:nn") as Diff
    FROM myTable I INNER JOIN myTable 0 ON I.Date=O.Date
    WHERE I.CheckType="I" AND O.CheckType="O"

    Any dates without both In and Out will be excluded, so if you have someone who checked in yesterday and checked out today would not be reported - you will need a modification to the above where, as Ranman suggested, it would be a lot easier if the date and time fields are combined (you can still display the same field twice, once formatted as a date and once as time). Also the hh:nn format only works for periods of less than 24 hours, otherwise you need to use a UDF to convert to a display you require.

    Similarly, if you have multiple ins and outs for the same day, you will get multiple returns.

    Note that date and time are both reserved words, so using them may generate unexpected results. Strongly recommend you change them to something more meaningful
    Appreciate your reply,Actually the source field already is combined date and time in same field,I separated it with format function only..Still i don't know what to do actually

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,439
    already is combined date and time in same field
    for a single field use

    SELECT datevalue(I.Date) as myDate, format(O.Date-I.Date,"hh:nn") as Diff
    FROM myTable I INNER JOIN myTable 0 ON datevalue(I.Date)=datevalue(O.Date)
    WHERE I.CheckType="I" AND O.CheckType="O"

    1. in the above code, change myTable to the name of your table, Date to the name of your combined date/time field
    2. open a new query
    3. go to the sql window
    4. copy and paste the revised code to replace anything that is currently there
    5. run the query to check it works
    6. go to design view to see the query builder view of the query

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

Similar Threads

  1. Replies: 2
    Last Post: 07-19-2015, 03:51 PM
  2. Subtrating Time in Consecutive Query Rows
    By andrebmsilva in forum Queries
    Replies: 1
    Last Post: 02-21-2013, 04:50 PM
  3. Replies: 2
    Last Post: 01-23-2013, 11:07 AM
  4. Replies: 10
    Last Post: 12-27-2011, 01:20 PM
  5. Time difference between rows
    By GeirA in forum Access
    Replies: 4
    Last Post: 11-24-2011, 02:05 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