473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Times - add + subtract + the SUM of many

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

Jul 21 '05 #1
3 1898
"Russ Green" <ru**@NO-SPAMrussgreen.net> schrieb

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)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Jul 21 '05 #2
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
Jul 21 '05 #3
"Russ Green" <ru**@NO-SPAMrussgreen.net> schrieb
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("STO P"))))

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'.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim | last post by:
I need to get the number of seconds when subtracting two date/times. With the code I am using below I subtract the two date/times and end up with a total of days like "1.56". Then when I multiply...
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
5
by: marty | last post by:
As part of an assignment I need to calculate a 24 hour time by subtracting a one time from another e.g. if the user enters 1.00 as the time and 1.30 as the time to subtract the returned time is...
2
by: Asad | last post by:
Hello, I need to know how I can calculate the number of seconds between two time stamps, e.g. if I have the following (using ToLongTimeString) 8:28:21 PM and 9:02:33 PM
3
by: ACaunter | last post by:
Hi all, I needed to know if there was a way to count the hours a user has been logged on for. When they first log in, i record the time and when the logoff i record the time, but now how would i...
6
by: James Goodman | last post by:
I have modified the ASP.NET TimeTracker Starter Kit to include the ability for users to log working hours (Start Time, Finish Time, Lunch Duration). This is then presented to them in a grid & a...
87
by: John Rivers | last post by:
Hello everybody, I just wondered if anybody else has noticed this? It takes around 6 seconds to start debugging a very simple ASPX page with VS.NET whereas VB6 takes under 0.5 seconds, even...
3
by: Russ Green | last post by:
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...
2
by: antonyfran | last post by:
hey can some please tell me how to run this class a specific number of times. maybe by using a for loop?. i am making a game called memory game where you match the cards which are the same. i want...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.