Link to home
Start Free TrialLog in
Avatar of isupsn
isupsn

asked on

DateDiff to calculate time elapsed between current and other records.

Hi

Am using an adodb recordset to pass values from a set of masked edit boxes into a sql table.  The purpose of this exercise is to log tasks and their duration in a time recording program.  One of the edit boxes therefore passes to a 'duration' field.  However if a user does not enter a duration, I would like the program to automatically calculate what the duration is.

I assume i use the DateDiff function, but don't know how to go about picking up values from records other than the current one.

I have tried declaring a 'duration' string in the DateDiff expression and picking up values for it by moving between records, but get 'invalid procedure call or argument' error.

heres the code:

Private Sub cmdUpdate_Click()
    'The code for date and time can be input as date and time and then joined
    'in a string to bring both items together, that string is then added to the
    'recordset as the whole date and time.
    Dim ShortDate As String
    Dim ShortTime As String
    Dim DateTime As String
    Dim DurCalc As String
    Dim NextDur As String


    'in my database table i have fields start_date_time, cost_centre, task_code, duration
   
    DateTime = Format(ShortDate, "dd/mm/yyyy") & " " & Format(ShortTime, "hh:mm:ss")
    rec.Fields(1) = DateTime
    rec.Fields(2) = Format(mebCCentre.Text, ">")
    rec.Fields(3) = Format(mebTask.Text, ">")
    If rec.EOF = True Then
        rec.Fields(4) = Format(mebDur.Text, "0")
    Else
        rec.MoveNext
        NextDur = rec.Fields(1)
        rec.MovePrevious
        rec.Fields(4) = DateDiff("ssss", rec.Fields(1), NextDur)
    End If
       
End Sub
Avatar of rafsalman
rafsalman

Not quite clear on what you are trying to do, but you could consider using the Date function to get the current system date and time....

If however your application is not the app that is performing the tasks then that would be no use.

Otherwise - if the user does not enter a duration where are you getting this field from? I.E. how do you intend calculating a duration when you don't have a finish time or are not given it - picking the data out of the database should be ok once you have the data to calculate it
Avatar of isupsn

ASKER

Hi

thanks for taking a look at this for me.  Sorry i haven't explained it very clearly ... i think half my problem is that i'm not thinking it through properly.

i am working on a time recording program.  i use system date and time as you suggest when user is entering real time records, but the bit i'm stuck on is for batch updating, which is generally historic data.

therefore would ideally like the user just to be able to enter dates, times and task codes and for the field 'duration' to update automatically if there is another record with a date and time against which to calculate a task duration (ie. if its not the last record). it should exist with a value of '0' if it is the last record until another record is entered after it at which time it will have a duration and should automatically be updated.

ASKER CERTIFIED SOLUTION
Avatar of rafsalman
rafsalman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept rafsalman   comment as answer.

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
cerebralPC
EE Cleanup Volunteer