Times - add + subtract + the SUM of many

  • Thread starter Thread starter Russ Green
  • Start date Start date
R

Russ Green

I'm trying to perform several manipulations to time values (all will be
stored in a access database). I need to find the elapsed time between a
start time and a finish time and I also need to be able to find the SUM of
multiple time values.

I know how to do this first part of the using the following code but how do
I add multiple time values together when they are in the "15:53:12" format?

____START CODE_____
Dim TimeStart, TimeStop As Date
Dim TimeDiff As TimeSpan

TimeStart = CDate(Me.txtTimeStart.Text)
TimeStop = CDate(Me.txtTimeStop.Text)

TimeDiff = TimeStop.Subtract(TimeStart)
____END CODE_____


Basically, I'm writing a small db app (using an access 2k backend) that will
log time spent on an activity (a log book). Each record will have a Start
Time and a Finish Time and an Elapsed Time (using the method shown above).
What I'm trying to get to is the total time spent across all entries. There
may only be 20 entries in the database but then there may be 100's.

How can I quickly get the SUM of all the times?

TIA

Russ
 
Russ Green said:
How can I quickly get the SUM of all the times?

Simply add the single TimeSpans to a total Timespan:

dim total as timespan

total = total.add(TimeDiff)
 
That's what I'm trying to do. Have a look at where I am now....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'check to see how many records are in the table
Dim maxVal As Integer = CountRecordAll(sAppPAth & "\TIME.mdb",
"tblTIME")
Dim i As Integer
Dim Total As New TimeSpan(0, 0, 0)

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" & sAppPAth & "\TIME.mdb")
conn.Open()
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = New OleDbCommand("SELECT * FROM tblTIME", conn)
Dim ds As DataSet = New DataSet
da.Fill(ds)

Dim dr As DataRow
If Not ds Is Nothing Then
For i = 0 To maxVal - 1
Try
Total = Total.Add(GetTimeSpan(CDate(dr("START")),
CDate(dr("STOP"))))
Catch ex As Exception
MsgBox(ex.Source.ToString + " " + ex.Message.ToString)
End Try
Next
End If

MsgBox("Total is:" & Total.ToString)

'cleanup
dr = Nothing
ds.Dispose()
da.Dispose()
conn.Close()
End Sub

Private Function GetTimeSpan(ByVal TimeStart As Date, ByVal TimeStop As
Date) As TimeSpan
Return TimeStop.Subtract(TimeStart)
End Function


Several problems though.....

This line: -
Total = Total.Add(GetTimeSpan(CDate(dr("START")), CDate(dr("STOP"))))

Generates this exception: -
System.NullReferenceException: Object reference not set to an instance of an
object.

The exception MsgBox does appear the correct number of times and then Total
is displayed as 00:00:00

Russ
 
Russ Green said:
That's what I'm trying to do. Have a look at where I am now....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles Button1.Click
'check to see how many records are in the table
Dim maxVal As Integer = CountRecordAll(sAppPAth &
"\TIME.mdb",
"tblTIME")
Dim i As Integer
Dim Total As New TimeSpan(0, 0, 0)


You can write

Dim Total As TimeSpan

because TimeSpan is a value type. Just like you write "dim i as integer",
not "dim i as new integer"

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" & sAppPAth & "\TIME.mdb")
conn.Open()
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = New OleDbCommand("SELECT * FROM tblTIME", conn)
Dim ds As DataSet = New DataSet
da.Fill(ds)

Dim dr As DataRow
If Not ds Is Nothing Then

ds is never Nothing at this location, but I guess you did this only in this
example.

For i = 0 To maxVal - 1
Try
Total = Total.Add(GetTimeSpan(CDate(dr("START")),
CDate(dr("STOP"))))


I think writing

Total = Total.Add(CDate(dr("STOP")).Subtract(CDate(dr("STOP"))))

is shorter because GetTimeSpan doesn't do anything else.

Why do you use CDate? (Why) Isn't the field a Date/Time field in the
database? Note that Date/Time values stored as Text fields in the database
are "dangerous" because CDate is system/country/region dependent, so the
same database on another PC might cause exceptions.
Several problems though.....

This line: -
Total = Total.Add(GetTimeSpan(CDate(dr("START")), CDate(dr("STOP"))))

Generates this exception: -
System.NullReferenceException: Object reference not set to an instance of an
object.

You never assign anything to 'dr'.
 
Back
Top