Results 1 to 2 of 2
  1. #1
    moonman84 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    3

    working out time difference


    Hi there,

    I am gathering data from a alarm monitoring system. It is writing to a Access database. At this moment it is writing to 2 tables, 1 being "faults" the other being "normal". sample of the tables are as follows:

    Table1(faults)
    Name Fault Time
    gas1 1 27/06/2011 15:40:35
    gas1 1 27/06/2011 15:42:35

    Table2(normal)
    Name Fault Time
    gas1 0 27/06/2011 15:41:35
    gas1 0 27/06/2011 15:43:35

    I am trying to show in a report the duration of the Faults. So from time 15:40:35 to 15:41:35. I am not using any ID fields on the tables, the names are the same on both tables but table1 will always have 1 in the fault column and table2 will have 0 in the fault column times will be as and when data is entered.

    I would like some advice weather I should use a query or use VBA and how to go about it...

    Many thanks for your time and help.

  2. #2
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    I have written some VBA Code with some sample data created in Excel and uploaded in Access. The sample code is given below:

    Code:
    Public Function TimeDiff()
    Dim db As Database, rst As Recordset
    Dim xstart As Double, xend As Double, diff As Double
    
    Set db = CurrentDb
    Set rst = db.OpenRecordset("Sheet1", dbOpenTable)
    rst.Index = "idx"
    xstart = rst![faulttime]
    xend = xstart
    rst.MoveNext
    Do While Not rst.EOF
         If (rst![faulttime] - xstart) > TimeValue("1:00:00") Then
             xstart = rst![faulttime]
         Else
            xend = rst![faulttime]
            diff = xend - xstart
            rst.Edit
            rst![diff] = diff
            rst.Update
            xstart = xend
         End If
         rst.MoveNext
    Loop
    rst.Close
    The image of the sample data after running the above code with the result in a new column (diff) is attached for info. An index is created on the FaultTime field to order the data correctly. When the time difference between two dates is more than 1 hour the record is skipped forward assuming that the actual faulttime taken place some other time.

    Since, only few records are tested there may be logical errors when large amount of data are involved. Correct the logic wherever you find it is failing by running the code on original file and manually checking the result.

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

Similar Threads

  1. working out time difference
    By moonman84 in forum Access
    Replies: 2
    Last Post: 06-29-2011, 03:33 AM
  2. Mark the difference
    By zhshqzyc in forum Access
    Replies: 1
    Last Post: 01-28-2011, 08:49 AM
  3. difference between drivers
    By tomc1 in forum Access
    Replies: 0
    Last Post: 08-04-2009, 10:41 AM
  4. Time difference
    By jguidry in forum Programming
    Replies: 1
    Last Post: 11-15-2008, 12:41 PM
  5. Access Runtime 2007 Date Time Picker Vista not working
    By sailinxtc in forum Programming
    Replies: 0
    Last Post: 09-17-2008, 12:56 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