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

A date Check/Comparison

DJRhino1175
221 128KB
I have a code that automatically advances the date 1 day using code, but its not working 100% the way I want it to.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Shift_AfterUpdate()
  2.  
  3.     If Shift = 3 Then
  4.  
  5.         If LineDate = Date And Hour(Now) Then
  6.             LineDate = DateAdd("d", 1, Date)
  7.         Else
  8.             LineDate = Date
  9.         End If
  10.     End If
  11.  
  12. End Sub
I only need it to advance if the date is anything before midnight of the previous day. Example - Our third shift starts at 10:30 pm, I have an auto date entry, but it puts the date as Date(), but for our 3rd shift it is technically the next day. So when they select their shift (3) from the drop down, I would like the date to advance 1 day. I think I'm close, I think the (Now) part needs to change.

Thanks for all the help in advance.
Aug 3 '18 #1
11 1616
Seth Schrock
2,965 Expert 2GB
I'm a little confused. First you say you want to advance the date if it is anything before midnight of the PREVIOUS day. Then your example describes the CURRENT day. Please clarify as that will change the calculation.
Aug 3 '18 #2
DJRhino1175
221 128KB
Yes my sample code is in correct, as I'm not sure what to put instead of (Now). My 3rd shift starts at 10:30pm, so when they start to enter data the date will be for example 8/3/2018, but the date of their shift should be 8/04/2018. So when the select or enter a 3 in the shift box I need it to advance one day. Currently no matter what time you input the 3 into shift it advances. This is the behavior I need to stop. Hope that cleared things up.
Aug 3 '18 #3
twinnyfo
3,653 Expert Mod 2GB
DJ,

Will the 3rd Shift employees ALWAYS be entering data prior to Midnight? If so, then I think your solution is as simple as this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Shift_AfterUpdate()
  2.     If Shift = 3 Then
  3.         LineDate = DateAdd("d", 1, Date)
  4.     Else
  5.         LineDate = Date
  6.     End If
  7. End Sub
Hoep this hepps!
Aug 3 '18 #4
DJRhino1175
221 128KB
twinnyfo,

No there will be times when it will be after, usually mondays as those are start up nights.
Aug 3 '18 #5
twinnyfo
3,653 Expert Mod 2GB
So, 3rd Shift will always be checking in after 10:30 PM or early in the morning? Then just set your paramaters thusly.

If they are checking in and the Hour() is after 22 then add a Day. If the Hour is Less than 22, then don't add a Day (this, then, would apply to all employees).
Aug 3 '18 #6
DJRhino1175
221 128KB
Ok, thank you. Here is what I put in:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Shift_AfterUpdate()
  2.  
  3.     If Shift = 3 Then
  4.  
  5.         If LineDate = Date And Hour(22) Then
  6.             LineDate = DateAdd("d", 1, Date)
  7.         Else
  8.             LineDate = Date
  9.         End If
  10.     End If
  11.  
  12. End Sub
I'm unable to test it at the moment as our IT depart doesn't allow us to change the time and date on our PC's.
Aug 3 '18 #7
twinnyfo
3,653 Expert Mod 2GB
You need a comparison operator:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Shift_AfterUpdate()
  2.     If Shift = 3 Then
  3.         If Hour(Now)>=22 Then
  4.             LineDate = DateAdd("d", 1, Date)
  5.         Else
  6.             LineDate = Date
  7.         End If
  8.     End If
  9. End Sub
Hope this hepps!
Aug 3 '18 #8
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

This is so unclear. Not one of your finer questions (You've done better). Why is the time of day at the point when the code's run important for anything?

Certainly Hour() needs a Date/Time parameter, but very rarely does it make sense to tie it to the current time.

How about you stop and explain all the details that need to be taken into consideration for this to work so we can consider the actual problem. I know when things get complicated the first instinct is to treat it like a difficult problem and just blurt out the question quickly. Try to take a step back. Be a little more confident in yourself and us here. Stop and think about what are all the bits of information and how they fit together in the logic.

Often once you've done that you'll have no need to post. When you do though, you can expect a single reply that's bang on. It's so much easier to work with a question that makes solid logical sense.
Aug 3 '18 #9
zmbd
5,501 Expert Mod 4TB
I have two shift in our lab and a similar situation- just in reverse as the second shift spans midnight. We need all of the work from second shift after midnight to use prior date until end of shift.

So in OP's example his shift spans Midnight so all of the dates for work generated on 3rd shift such that:
2018-01-01:22h30 thru 2018-02-02:06h30
With the date 2018-02-02

Issue with his code is that at 2018-02-02:00h00 it's advancing the date to 2018-02-03 when OP still requires the date to be 2018-02-02.

As for tying it to the current time - that is entirely dependent on workflow. I pull the Hour(Now()) because I have to know when the sample was logged into the database as this determines which day's paperwork it is recorded (and for the cost-accountants, but that's a whole other level of voodoo!)

I think twinnyfo has the solution - looks similar to what I have at work... I'm home so I can't verify that.
Aug 3 '18 #10
NeoPa
32,556 Expert Mod 16PB
Now() is only relevant in any way if you want to make when you logged the item a determining factor. As a general rule that's a very bad idea, though there are scenarios where it can be practical and required.

I just wanted to make sure you aren't shooting yourself in the foot by ensuring it will only ever work correctly if the item is logged at the time it becomes ready. Even if the period where it could be logged were a whole day it still leaves you in a position where retrostective entry of the data would be unreliable - essentially wrong.

As long as that problem is understood and accepted then you should be fine.
Aug 4 '18 #11
zmbd
5,501 Expert Mod 4TB
NeoPa - as usual, very good point with the Now() - for my situation, the business rules dictate when the entry is made to the database for OP's that may be entirely different, instead, requiring the date and time the item was completed.
Aug 4 '18 #12

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

Similar topics

2
by: Jon | last post by:
Hi, I am trying to pick up records from a date range without much luck. For my example below I am trying to pick up all records made in the last month. However none are being found (my code is...
1
by: colinhumber | last post by:
I have a datetime variable coming from my ASP.NET application that has a time portion. I give my users the option to perform an equals, greater than, less than, or between comparison. The trouble...
4
by: Matteo | last post by:
Hy everybody. I'm not a html writer, but a sysadmin who's trying to help a user able to compile an online form with IE but not with Mozilla (Moz1.6, Ns7.1, Firefox 0.8+) due to a javascript date...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
2
by: Suresh | last post by:
Hi All, I have varchar column which stores data in 8 char format 'yyyymmdd'. i want to transfer data in date columns but before that i want to check data is proper date or not. like in sql...
2
by: Nazeer | last post by:
Hi I am working in sharepoint portal sever 2007. I am filter data in data view based on some data time column (example startdate). Data view filter rows if i gave equl condition like . If i...
0
by: robtyketto | last post by:
Greetings, I'm using MDMZinc to insert/update records into an MSAccess database, many headaches have been caused by dates. Apparently the best method to avoid complication is to record date...
2
by: barmatt80 | last post by:
I have a webservice that accepts a date which ultimately submits to a sql server 2008 database with a data type of date (allows null values), however the infopath form that submits that date...
1
by: mikeyh | last post by:
Im trying to develop a SQL driven report in Quality Centre using parameters. Ive set up two parameters in the 'query parameters' part of the report which are passed into the SQL, each parameter...
7
by: shydan | last post by:
Hi, i wrote a program in access and i would like to implement a yearly license renewal... i would like to ask if there is any function that can make the program check the online date and compare it...
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
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...
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
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,...
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
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.