473,396 Members | 2,102 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,396 software developers and data experts.

Date update fuction

I have a form that I would like to calculate the time taken since the last input was made. We have a vessel that fills between either two days or sometimes a week. I have a field to input the time the vessel was full and would like to have the "time taken to fill" field to update. I have very limited experience with access.
Please let me know if you require more info

Regards

Bob
Nov 1 '09 #1

✓ answered by topher23

Okay, your terminology is a bit off, so I'll try to go slow with this.

A form is something that is used to view, change, add and delete records. A record is a row in a table in the database. Records from one or more tables can be put together into a query that holds the data "behind" the form, or a form can just reference a table to get its data. This is called the form's RecordSource, found in the Properties of the form in Design view.

If you're with me so far, we're doing good. Now, when you a say a new form is created every day, I'm going to assume that means a new record in a table or query that acts as a recordsource for the form. If that's not what you mean, let me know immediately - that would mean really big problems.

So, here's what you need to. Open, in design view, the table that holds all of the tank filling information. Add a new field called lngTimeTakenToFill to the table. Make sure this new field is a Long Integer data type, and save it. Making the name start with "lng" is a naming convention that says the field contains a "Long Integer" variable type, which is a Number variable type that allows a very large whole number. Not putting spaces in your field names is a point of protocol that makes programming much easier.

Now, make sure you have a "Primary Key" for your table. This should be an Autonumber field that is the first field in your table. If you don't have one, create one called RecordID and set it to be the Primary Key for the table. If you already have an Autonumbered Primary Key, use the name of that key instead of "RecordID" when you put in the new code (below).

Save and close your table.

Now, open your form in design view. Add the new new lngTimeTakenToFill field to your form. In the new field's properties, go to the data tab and change Locked to Yes and Enabled to No. Now click on your "time tank filled" field on the form. In the Events tab of the Properties dialog, click on "After Update." There should be an ellipsis (...) button at the end of the field. Click that and select "Code Builder."

Now, do you have separate fields for the date and the time? If so, paste this code into your new, empty VBA subroutine.

Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset 
  2.     Set rs = CurrentDb.OpenRecordset("SELECT [Date Field], [Time Tank Filled] FROM tblWhatever WHERE RecordID < " & Me.RecordID & " AND [Time Tank Filled] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot) 
  3.     Me.lngTimeTakenToFill =  DateDiff("h", rs![Date Field], Me![Date Field]) + DateDiff("h", rs![Time Tank Filled], Me![Time Tank Filled]) 
  4.     rs.Close 
  5.     Set rs = Nothing 
  6.  
If your "time tank filled" field holds both the date and the time together (as in the Now function), use this code:


Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset 
  2.     Set rs = CurrentDb.OpenRecordset("SELECT [Time Tank Filled] FROM tblWhatever WHERE RecordID < " & Me.RecordID & " AND [Time Tank Filled] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot) 
  3.     Me.lngTimeTakenToFill =  DateDiff("h", rs![Time Tank Filled], Me![Time Tank Filled]) 
  4.     rs.Close 
  5.     Set rs = Nothing 
  6.  
Remember to rename anything in the code that doesn't match your database, like the table name from tblWhatever to the actual table name and [Time Tank Filled] to the actual field name. Note that, if your field has spaces in it, you have to use the brackets [] when you reference it or your code won't work.

Now, save your form and give it a try. what should happen is, when you make a change to the "time tank filled" field, the "time to fill tank" field will populate itself with the number of hours it took to fill it. If you want minutes, change the DateDiff function in the code from "h" to "n" ("m" is reserved for "months") and try it again.

Now, to populate your old data, you can add this code to the form's "On Current" event using the same "Code Builder" process:

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me!lngTimeTakenToFill) And Not IsNull(Me![Time Tank Filled] Then Time_Tank_Filled_AfterUpdate()
  2.  
As before, if Time_Tank_Filled_AfterUpdate() isn't the name of the subroutine that you just built earlier, change it to the right name. Now, if you pull up an older Tank-filling record, it will see that the lngTimeTakenToFill field is empty and run the code to fill it. Pretty spiffy, huh?

Well, that's probably more guidance than I should've given, but I just couldn't help myself. You definitely need to spend more time with tutorials and the Access help file. Check out Microsoft's website or functionx.com, they are great resources for learning to use Access.

21 1981
missinglinq
3,532 Expert 2GB
I assume you have a start time/date or at least a start date field. This will give you the fill time in hours, assuming you include time in your start and stop fields.

Expand|Select|Wrap|Line Numbers
  1. Private Sub EndDate_AfterUpdate()
  2.  Me.FillTime = DateDiff("h", Me.StartDate, Me.EndDate)
  3. End Sub
  4.  
To get it in minutes you would substitute the n for the h.

To get it in days you would substitute the d for the h.

Note that using the d for day will count any part of a day, even if the fill ended at one minute past midnight on the last day.

To get days and part of days, you could use

Expand|Select|Wrap|Line Numbers
  1. Private Sub EndDate_AfterUpdate()
  2.  Me.FillTime = DateDiff("n", Me.StartDate, Me.EndDate) / 1440
  3. End Sub
  4.  
which calculates the number of minutes and then divides by 1440, the number of minutes in a day.

*********************************

Also note, please. Your duplicate posting of this question/problem has been deleted! Please refrain from this prohibited behavior in the future.

From FAQs

Do Not Double Post Your Questions

Double posting is where you start a thread on a topic and then for some reason start another thread on exactly the same topic in the same forum. Please do not do this because
  • It makes it very hard for people to answer you especially if there are answers happening in all the threads you have started because they have to read 2 or more threads in order to see what has already been said.
  • It swamps the forum with your problem resulting in less attention for the other threads.
If you feel for some reason that you post has been overlooked (for instance it hasn't had any replies) please do not repost the question. Post a message to the thread you started, simply saying "bump" and this will move it back to the top of the thread list for the forum.

Thank you for your attention in this matter

Welcome to Bytes!

Linq ;0)>
Nov 1 '09 #2
I have a field for "time tank full"
This would be the same time as the next new tank started to fill.
A new record is created every day if it is used or not.
I would like the "time taken to fill" feild to update when the tank full time is input if thats possible without having a time new tank started to fill feild?
Hope thats clear to understand?

Thanks for making time to reply

regards
bob
Nov 1 '09 #3
maxamis4
295 Expert 100+
I assume based on your answer that you are not to familiar with access VBA code?

missinglinq has given you the answer to do this calculation. If you do not understand the VBA please let us know so that we can give you better guidance.
Nov 1 '09 #4
topher23
234 Expert 100+
max, the question bob is posting is a bit more complex than that.

bob, let me run this by you to see if I understand it.

You create a record when the tank is full to state that it is full and when. Then, the next time the tank is full, you create a new record stating that it is full, and when. What you are looking to do is get the difference in values between the times on the separate records and display that as a value on the form. Now for the big question - do you plan to store the elapsed time in the database itself or calculate it on the form each time it pulls up?

If you plan to save it, the code is fairly easy.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Time_Tank_Full_AfterUpdate ()
  2. Dim dtLastTime As Date
  3.     dtLastTime = DLast("[Time Tank Full]", "[tblWhatever]", "Not IsNull([Time Tank Full])") 'replace with your actual field and table names
  4.     If Nz(Me.strElapsedTime)="" then me.strElapsedTime = DateDiff("h", Me.[Time Tank Full], dtLastTime) 'again, replace with your actual field names
  5. End Sub
  6.  
Run this code as part of your "Time Tank Full" AfterUpdate event. It's not perfect, but it should give you a good start.

This code will only work properly if you save "Time Tank Full" as a complete Date/Time and not just as a time. Also, you would have to save it as a field, otherwise it would pull up the wrong "Time Tank Full" every time you looked at old data.
Nov 2 '09 #5
topher23
234 Expert 100+
After some more thought, there is a way you could do this using recordsets that might work a bit better.
Expand|Select|Wrap|Line Numbers
  1. Private Time_Tank_Full_AfterUpdate()
  2. Dim rs As Recordset
  3.     Set rs = CurrentDb.OpenRecordset("SELECT [Time Tank Full] FROM tblWhatever WHERE RecordID < " & Me.RecordID & " AND [Time Tank Full] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot)
  4.     Me.strElapsedTime = DateDiff("h", Me![Time Tank Full], rs![Time Tank Full])
  5.     rs.Close
  6.     Set rs = Nothing
  7. End Sub
This assumes that you are using an autonumber field called RecordID to identify your records and that the table is sorted by RecordID, along with the other assumptions in the last post, but it shouldn't be as easy to break as the other code.
Nov 2 '09 #6
Topher23
Thanks for your time looking at this.
My skills on this are very limited.
A new form is saved every day.
I would like to save the time tanken to fill field so it can be viewed when required via a database view function.
Hope thats clear enough to understand?

Thanks again

Bob
Nov 2 '09 #7
topher23
234 Expert 100+
Bob, did you make this application or was it someone else? Because it sounds like you are completely lost now.
Nov 2 '09 #8
All my own efforts im sorry to say.
A new form is created each day <=Now() And >#25/09/2009# in my query.
I have a field for "time tank filled" on the form and would like it to update a "time taken to fill" field on the form.
Im not lost yet but its getting dark

bob
Nov 2 '09 #9
topher23
234 Expert 100+
Okay, your terminology is a bit off, so I'll try to go slow with this.

A form is something that is used to view, change, add and delete records. A record is a row in a table in the database. Records from one or more tables can be put together into a query that holds the data "behind" the form, or a form can just reference a table to get its data. This is called the form's RecordSource, found in the Properties of the form in Design view.

If you're with me so far, we're doing good. Now, when you a say a new form is created every day, I'm going to assume that means a new record in a table or query that acts as a recordsource for the form. If that's not what you mean, let me know immediately - that would mean really big problems.

So, here's what you need to. Open, in design view, the table that holds all of the tank filling information. Add a new field called lngTimeTakenToFill to the table. Make sure this new field is a Long Integer data type, and save it. Making the name start with "lng" is a naming convention that says the field contains a "Long Integer" variable type, which is a Number variable type that allows a very large whole number. Not putting spaces in your field names is a point of protocol that makes programming much easier.

Now, make sure you have a "Primary Key" for your table. This should be an Autonumber field that is the first field in your table. If you don't have one, create one called RecordID and set it to be the Primary Key for the table. If you already have an Autonumbered Primary Key, use the name of that key instead of "RecordID" when you put in the new code (below).

Save and close your table.

Now, open your form in design view. Add the new new lngTimeTakenToFill field to your form. In the new field's properties, go to the data tab and change Locked to Yes and Enabled to No. Now click on your "time tank filled" field on the form. In the Events tab of the Properties dialog, click on "After Update." There should be an ellipsis (...) button at the end of the field. Click that and select "Code Builder."

Now, do you have separate fields for the date and the time? If so, paste this code into your new, empty VBA subroutine.

Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset 
  2.     Set rs = CurrentDb.OpenRecordset("SELECT [Date Field], [Time Tank Filled] FROM tblWhatever WHERE RecordID < " & Me.RecordID & " AND [Time Tank Filled] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot) 
  3.     Me.lngTimeTakenToFill =  DateDiff("h", rs![Date Field], Me![Date Field]) + DateDiff("h", rs![Time Tank Filled], Me![Time Tank Filled]) 
  4.     rs.Close 
  5.     Set rs = Nothing 
  6.  
If your "time tank filled" field holds both the date and the time together (as in the Now function), use this code:


Expand|Select|Wrap|Line Numbers
  1. Dim rs As Recordset 
  2.     Set rs = CurrentDb.OpenRecordset("SELECT [Time Tank Filled] FROM tblWhatever WHERE RecordID < " & Me.RecordID & " AND [Time Tank Filled] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot) 
  3.     Me.lngTimeTakenToFill =  DateDiff("h", rs![Time Tank Filled], Me![Time Tank Filled]) 
  4.     rs.Close 
  5.     Set rs = Nothing 
  6.  
Remember to rename anything in the code that doesn't match your database, like the table name from tblWhatever to the actual table name and [Time Tank Filled] to the actual field name. Note that, if your field has spaces in it, you have to use the brackets [] when you reference it or your code won't work.

Now, save your form and give it a try. what should happen is, when you make a change to the "time tank filled" field, the "time to fill tank" field will populate itself with the number of hours it took to fill it. If you want minutes, change the DateDiff function in the code from "h" to "n" ("m" is reserved for "months") and try it again.

Now, to populate your old data, you can add this code to the form's "On Current" event using the same "Code Builder" process:

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me!lngTimeTakenToFill) And Not IsNull(Me![Time Tank Filled] Then Time_Tank_Filled_AfterUpdate()
  2.  
As before, if Time_Tank_Filled_AfterUpdate() isn't the name of the subroutine that you just built earlier, change it to the right name. Now, if you pull up an older Tank-filling record, it will see that the lngTimeTakenToFill field is empty and run the code to fill it. Pretty spiffy, huh?

Well, that's probably more guidance than I should've given, but I just couldn't help myself. You definitely need to spend more time with tutorials and the Access help file. Check out Microsoft's website or functionx.com, they are great resources for learning to use Access.
Nov 2 '09 #10
WOW
Thanks for your help I will follow this tomorrow.
I am already aware of the basics but thanks for so much detail.
I will let you know how I get on

Regards

bob
Nov 2 '09 #11
I get a compile error method or data member not found showing the & Me.RecordID item as the fault?
Expand|Select|Wrap|Line Numbers
  1. Private Sub Time_Trade_tank_full_AfterUpdate()
  2. Dim rs As Recordset
  3.     Set rs = CurrentDb.OpenRecordset("SELECT [Date], [Time Trade tank full] FROM tankfills WHERE RecordID < " & Me.RecordID & " AND [Time Trade tank full] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot)
  4.     Me.lngTimeTakenToFill = DateDiff("h", rs![date], Me![date]) + DateDiff("h", rs![Time Trade tank full], Me![Time Trade tank full])
  5.     rs.Close
  6.     Set rs = Nothing
Nov 3 '09 #12
topher23
234 Expert 100+
Ooops! Sorry. I forget to mention that.

You will have to put the RecordID field on your form and set its Visible property to False (unless you want it visible, that is).
Nov 3 '09 #13
This is now the new error?
Sorry to keep asking for help if its taking to much of your time I fully understand.

Compile error method or data member not found?
Expand|Select|Wrap|Line Numbers
  1. Private Sub Time_Trade_tank_full_AfterUpdate()
  2. Dim rs As Recordset
  3.     Set rs = CurrentDb.OpenRecordset("SELECT otTankStatus.Date, [Time Trade tank full] FROM tankfills WHERE RecordID < " & Me.RecordID & " AND [Time Trade tank full] Is Not Null ORDER BY RecordID DESC;", dbOpenSnapshot)
  4.     Me.lngTimeTakenToFill = DateDiff("h", rs!otTankStatus.date, Me!otTankStatus.date) + DateDiff("h", rs![Time Trade tank full], Me![Time Trade tank full])
  5.     rs.Close
  6.     Set rs = Nothing
Nov 3 '09 #14
topher23
234 Expert 100+
No worries, it's worth it for the help I've gotten from this forum (invaluable).

First, find out exactly which line the error is occurring in (it should be highlighted when you hit Debug after the error occurs).

If the error is occurring in the line that creates the recordset, make sure all of your fields are correct. It looks like you have the Date referenced from the table otTankStatus, but the FROM statement references tankfills. Is tankfills a query? If it's a table, you need a JOIN between the two tables in the recordset or it won't work. If it's a query, make sure all of the referenced fields exist in the query.

You could try re-creating the recordset in the database as a new query in Query Designer. If it works as a query, save the query and change the recordset to the name of the query. To reference the RecordID on the form in Query Designer (Me.RecordID in the recordset) you'd set the Criteria field to > Forms!(Form Name)!RecordID
Nov 3 '09 #15
NeoPa
32,556 Expert Mod 16PB
@BibBob:
Please remember to use the CODE tags whenever you post any code. It's much easier to follow that way.

Also, consider giving the line number an error occurred on when posting that information. Also much easier to deal with.

Lastly, you may find it helpful (it will help others to help you - you therefore get better help more quickly) to post your meta-data (info about the layout / structure) of the table in the same way as I use in my example. Click on the Reply button and you will have access to all the codes I've used. PK & FK stand for Primary Key & Foreign Key respectively. Never use TABs in this as the layout gets mucked up. Use spaces and all is fine.
Table Name=[tblStudent]
Expand|Select|Wrap|Line Numbers
  1. Field           Type      IndexInfo
  2. StudentID       AutoNumber    PK
  3. Family          String        FK
  4. Name            String
  5. University      String        FK
  6. Mark            Numeric
  7. LastAttendance  Date/Time
@Topher:
Nice going here again. We'll have you as an expert sooner rather than later if I'm any judge :)
Nov 3 '09 #16
topher23
234 Expert 100+
@bibbob66
Don't stress about NeoPa, bob, he's just doing his job as the forum cop. It's best to keep a thread going no matter how confused things seem to be getting.

Date and Time are Access reserved words, meaning that they should never be used as field names. This is where Leszynski-style naming conventions come in handy http://en.wikipedia.org/wiki/Leszyns...ing_convention. I use my own variant of strict Leszynski, with "dt" as my prefix for date/time values, so my tables tend toward field names like dtDate or dtTime at the very least, but usually with more detail, like dtDateEntered or dtTimeCompleted.

Again, spaces in field names are bad, because they have to be encapsulated (wrapped in brackets) in order for Access to be able to evaluate them. If your database isn't too big already, now would be a good time to fix things and start practicing good design skills.

Keep at it. I can smell smoke, but I hope it means something good's cooking and not that your brain is getting totally fried.

Please let us know what errors come up when you get the design worked out.
Nov 3 '09 #17
Not sure how I ve done it but it works great!!
Thanks for your help and seeing it through to the end.
Should help in my performance review
Nov 4 '09 #18
topher23
234 Expert 100+
Glad to hear it works! If you want to get a better understanding, use the help file to study the different functions. One day, grasshopper, you too may be master. :-)
Nov 4 '09 #19
NeoPa
32,556 Expert Mod 16PB
@topher23
That dates you Topher ;)

Signed -Another fan.
Nov 4 '09 #20
maxamis4
295 Expert 100+
bibbob66,

I am glad you found the forum helpful. Sorry I couldn't provide more help. What I will offer now is a link to a great training site for MS Access. I hope that you take the opportunity to review it and learn a little more about access and its power. Good look to you

http://cisnet.baruch.cuny.edu/holowc...accessall.html
Nov 5 '09 #21
NeoPa
32,556 Expert Mod 16PB
That looks like a very useful link Max.

We do quite often get posters who are keen to go off and learn up before asking too many questions. I've made a note of the link ;)
Nov 5 '09 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: jorntk | last post by:
Hi, How do i compare two date in YYYY-MM-DD format to determine which one is earlier? thanks and regards Jorn
3
by: BlackFireNova | last post by:
This concerns an Access 2002 (XP) database. There are two fields, "Notes" (Memo Field) and "Notes Update" on a form (among others) which I am concerned with here. Problem: I need to be able...
5
by: m_t_hill | last post by:
Running MS Access 2000 MS Windows XP Pro This has caused me a lot of hair loss in the last few days so would appreciate any help. I am running code to append/update a local access database...
1
by: Riley DeWiley | last post by:
I have an UPDATE query that is always setting 0 records. When I cut and paste the SQL into Access and use it, it fails in the same way unless I coerce the date fields to be '=now()', in which case...
2
by: Kapti | last post by:
Hi! I save 10 rows per second to a Access DataBase. Data are copied to DataSet pro term, then I call Update fuction of DataAdapter to refresh database. code: DataRow newRow; newRow =...
5
by: YardDancer | last post by:
Hi All, Does anybody have a routine for validating a date or DateTime entry in a textbox. I know i can use one of the DateTime.Parse(textbox.text) methods which would throw a Fromat...
2
by: mandy1 | last post by:
Hi to all i am Mandeep new to all of You. i have a Problem and hope solution from you. i want to return only Month of the Given date. suppose i have a field order_date in the order table. i...
4
by: nagarwal | last post by:
Hi All, I am having prblm in adding the no. of days passed to the current date, in a class fuction. This is an immediate requirment plz help.. I am using java.util.Date object for the current...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.