Solved

Subtracting one time from another time

Posted on 2014-12-17
20
106 Views
Last Modified: 2014-12-18
I have two fields on a form.  One is "StartTime"  The other is "EndTime"  

I am trying to subtract StartTime from EndTime and get the right result.

If StartTime is 10:25 am and StopTime is 11:00 am I want to see 00:35 as the result.

In my result field I have:

=[txtStopTime]-[txtStartTime]

But I'm getting 0;45

????

--Steve
0
Comment
Question by:SteveL13
  • 8
  • 7
  • 3
  • +1
20 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40505662
Try with:

=CVDate([txtStopTime])-CVDate([txtStartTime])

/gustav
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40505663
Use DateDiff() to subtract dates.
0
 

Author Comment

by:SteveL13
ID: 40505668
Hmmm.  I'm getting 0:45 instead of 0:35  (minutes)
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40505673
How about:

=format([txtStopTime]-[txtStartTime], "hh:mm")

But what if one of those values is missing(NULL)?  In that case, this will return a NULL value.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40505677
Will you ever have an instance where the txtStartTime is before midnight and txtStopTime is after midnight (the next morning)?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40505679
CVDate([txtStopTime])-CVDate([txtStartTime]) is the clean method and will return 0:35.
So something else is going on.

/gustav
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 166 total points
ID: 40505685
@Dale,
Minutes is "nn"
so, the expression should be:

=format([txtStopTime]-[txtStartTime], "hh:nn")
0
 

Author Comment

by:SteveL13
ID: 40505702
All suggestions have returned 0:45

??

Bot the start and end time fields are short time format.  Might this be a problem?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40505703
@Pat,

Yes, minutes is generally 'nn', but in this format VBA will interpret it as minutes anyway; try it.  But good point, it should be "hh:nn".
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40505706
Then your times are not what you think they are.

Short Time simply reflects how the data is displayed.  Remove the formatting on that field to see the full data.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:SteveL13
ID: 40505709
They are Date/Time, General Date fields in the table.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40505729
Open your table in design view, change the format of those fields (assuming they are bound controls) to nothing.  Then open the table scroll to the row with the data you are trying to view, take a screen shot, and post it here.
0
 

Author Comment

by:SteveL13
ID: 40505780
Here you go...

Start Stop Screen Shot
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40505901
OK, now try a query.

SELECT [Load Sch Start], [Load Sch Complete]
, Format([Load Sch Complete] - [Load Sch Start], "hh:nn") as LoadTime
FROM yourTable

and display those results.
0
 

Author Comment

by:SteveL13
ID: 40506010
Here it is...

Query Result
So why is the result ok here but not on the form?

??
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40506053
Good question, which I cannot answer, unless the textboxes on the form are not actually bound to the right fields in your table.  Generally, I give the textboxes names that are identical to the name of the field, except with a prefix:

txt_Load_Sch_Start
txt_Load_Sch_Complete

You could simply modify the RecordSource of your form to a query which contains that formatted text.  The control would be blank if either [Load Sch Start] or [Load Sch Complete] was NULL, but would recalculate as each of those values was changed.
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 167 total points
ID: 40506421
> So why is the result ok here but not on the form?

Field names:

    [Load Sch Complete]
    [Load Sch Start]

are different from your original data.

Anyway, as these are bound to fields of datatype Date, you can use the expression

    =[Load Sch Complete]-[Load Sch Start]

and specify the Format property of the textbox as:

    hh:nn

 /gustav
0
 

Author Comment

by:SteveL13
ID: 40507425
Now when I enter hh:nn as the format it automatically changes itself to Short Time.

And the result is still 0:45
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 167 total points
ID: 40507499
So, do as I recommended and change the recordsource of the form to a query which contains the computed column as defined in my previous post.
0
 

Author Closing Comment

by:SteveL13
ID: 40507509
Because of my own inability to provide real field names and also to even use them myself I feel I should grant awards to all who helped.  I am sorry for all the confusion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create Form using Wizard 14 39
MS-Access 2002 error (Win XP on Win7Pro) 19 51
Query design issue 2 24
error 3022 in access vba 3 18
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now