473,799 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unsure why VBA thinks dates entered are greater than Now() - when they are not?

MLH
After entering a date (earlier than today's date) into a textbox
control though, I'm stumped as to why VBA thinks date I enter into the
control is greater than Now - when they are CLEARLY less than Now.
Here's the code I have in the AfterUpdate event. I must be overlooking
the obvious. Ideas?

Private Sub DateSigned_Afte rUpdate()

If Me!DateSigned Now Then
Me!DateSigned = Null
MsgBox "No Way Jose!"
End If

End Sub
Aug 3 '07 #1
14 3498

"MLH" <CR**@NorthStat e.netschreef in bericht news:94******** *************** *********@4ax.c om...
After entering a date (earlier than today's date) into a textbox
control though, I'm stumped as to why VBA thinks date I enter into the
control is greater than Now - when they are CLEARLY less than Now.
Here's the code I have in the AfterUpdate event. I must be overlooking
the obvious. Ideas?

Private Sub DateSigned_Afte rUpdate()

If Me!DateSigned Now Then
Me!DateSigned = Null
MsgBox "No Way Jose!"
End If

End Sub
The obvious:
Check your SystemDate first .... What value does Now() give you ??

BTW: If you are validating input, better use the BeforeUpdate event.

Arno R
Aug 3 '07 #2
On Fri, 03 Aug 2007 11:29:07 -0400, MLH <CR**@NorthStat e.netwrote:

Arno has a good point about BeforeUpdate: set Cancel=True and have the
user try again.
But the real point of Why (Assuming you were not that stupid to reset
your computer clock) is one of type coercion. My guess is that the
whole expression is evaluated as strings rather than as dates. Enter
this in the Immediate Window:
(Note: today is 8/3/2007 in USA format)

?(8/15/2007 Now)
False

?("8/15/2007" Now)
False

?(#8/15/2007# Now)
True

So my guess is your expression is evaluated as a string comparison,
and the ASCII value of "8/1" is lower than that of "8/3".

To compare dates, exclusively use DateDiff. It will cast both
arguments to Date before comparing, similar to my third example.

-Tom.
>After entering a date (earlier than today's date) into a textbox
control though, I'm stumped as to why VBA thinks date I enter into the
control is greater than Now - when they are CLEARLY less than Now.
Here's the code I have in the AfterUpdate event. I must be overlooking
the obvious. Ideas?

Private Sub DateSigned_Afte rUpdate()

If Me!DateSigned Now Then
Me!DateSigned = Null
MsgBox "No Way Jose!"
End If

End Sub
Aug 4 '07 #3
On Aug 4, 5:26 am, Tom van Stiphout <no.spam.tom7.. .@cox.netwrote:
On Fri, 03 Aug 2007 11:29:07 -0400, MLH <C...@NorthStat e.netwrote:

Arno has a good point about BeforeUpdate: set Cancel=True and have the
user try again.
But the real point of Why (Assuming you were not that stupid to reset
your computer clock) is one of type coercion. My guess is that the
whole expression is evaluated as strings rather than as dates. Enter
this in the Immediate Window:
(Note: today is 8/3/2007 in USA format)

?(8/15/2007 Now)
False

?("8/15/2007" Now)
False

?(#8/15/2007# Now)
True

So my guess is your expression is evaluated as a string comparison,
and the ASCII value of "8/1" is lower than that of "8/3".

To compare dates, exclusively use DateDiff. It will cast both
arguments to Date before comparing, similar to my third example.

-Tom.
After entering a date (earlier than today's date) into a textbox
control though, I'm stumped as to why VBA thinks date I enter into the
control is greater than Now - when they are CLEARLY less than Now.
Here's the code I have in the AfterUpdate event. I must be overlooking
the obvious. Ideas?
Private Sub DateSigned_Afte rUpdate()
If Me!DateSigned Now Then
Me!DateSigned = Null
MsgBox "No Way Jose!"
End If
End Sub
You could also use

If CDate(Me!DateSi gned) Date() Then
....

Also check if the date format in Control Panel/Regional Settings
matches date in Access field.

Regards,
Branislav Mihaljev

Aug 4 '07 #4
Dates are a massive pain especially if you are not american!
(Note: No offense meant to americans, it's just that the dates don't
work intuitively for the rest of the world!)

THe problem is that Access only thinks in American format but stores
it anyway you want.

To combat date issues we always use the longer month format in
everything we do in Access. e.g. dd-MMM-yyyy e.g. 5 Mar 2006.

So first things first, figure out if the problem is the date format.
e.g. 5 Mar 2006 is 3/5/2006 in american terms....as they put the month
first.

Regards,
Tom Bizannes
Sydney, Australia
http://www.smartbiz.com.au

Aug 5 '07 #5
"SmartbizAustra lia" <to*@smartbiz.c om.auwrote
THe problem is that Access only thinks in American
format but stores it anyway you want.
Actually, that is not the case... all date/time values in Access are stored
the same way, regardless of the Regional Settings -- that is, a date/time
field or variable is the equivalent of a Double Precision Floating Point
value with the integral (or whole number) portion representing days since
Dec. 30, 1899 and the fractional portion representing time since midnight.

The Regional Settings only affect how date data you enter is interpreted and
how stored date data is displayed.

The problem you encounter is that there are some situations in Access and
the Jet database engine which ignore the Regional Settings.

(YMMV on date data kept by other applications or by Windows itself.)

Larry Linson
Microsoft Access MVP
Aug 6 '07 #6

"SmartbizAustra lia" <to*@smartbiz.c om.auschreef in bericht news:11******** **************@ i13g2000prf.goo glegroups.com.. .
Dates are a massive pain especially if you are not american!
(Note: No offense meant to americans, it's just that the dates don't
work intuitively for the rest of the world!)

THe problem is that Access only thinks in American format but stores
it anyway you want.

To combat date issues we always use the longer month format in
everything we do in Access. e.g. dd-MMM-yyyy e.g. 5 Mar 2006.

So first things first, figure out if the problem is the date format.
e.g. 5 Mar 2006 is 3/5/2006 in american terms....as they put the month
first.

Regards,
Tom Bizannes
Sydney, Australia
http://www.smartbiz.com.au
Would be nice if MLH would let us know what the actual problem was here ....

Arno R
Aug 6 '07 #7
MLH
Arno:
The problem is as I stated in the O.P.

After entering a date (earlier than today's date) into a textbox
control, Sub DateSigned_Afte rUpdate() displays "No Way Jose!".
I did not expect the following Sub to evaluate Me!DateSigned
being greater than Now().

Private Sub DateSigned_Afte rUpdate()
If Me!DateSigned Now Then
Me!DateSigned = Null
MsgBox "No Way Jose!"
End If
End Sub

<snip>
>
Would be nice if MLH would let us know what the actual problem was here ...

Arno R
Aug 8 '07 #8

"MLH" <CR**@NorthStat e.netschreef in bericht news:5u******** *************** *********@4ax.c om...
Arno:
The problem is as I stated in the O.P.

After entering a date (earlier than today's date) into a textbox
control, Sub DateSigned_Afte rUpdate() displays "No Way Jose!".
I did not expect the following Sub to evaluate Me!DateSigned
being greater than Now().

Private Sub DateSigned_Afte rUpdate()
If Me!DateSigned Now Then
Me!DateSigned = Null
MsgBox "No Way Jose!"
End If
End Sub

<snip>
>>
Would be nice if MLH would let us know what the actual problem was here ...

Arno R
So you are telling us that you still have this very same problem ??
I had the impression that your problem was solved and was interested to hear what caused the problem ....
I should have said:
"Would be nice if MLH would let us know what the actual problem has been here ..."

What about all the suggestions ?? Did you work that out ?
What is the outcome of the following in the immediate window?
?Forms!NameOfFo rm!DateSigned
?Now

Arno R

Aug 8 '07 #9
MLH
Seems that Tom van Stiphout hit it on the nose. Must-a-been
some kind-a-string comparison going on, rather than a date-to-date
comparison. I ran his suggested tests in the immediate window.
So I took banem2 up on his suggestion to encapsulate the textbox
reference inside CDate(). Apparently, that had an effect and seems
like the one I want.

Your comments re: Before vs After Update were well received too.
Thx.

After having made the 'fix', I found more places in my code were
greater-than / lesser-than comparisons may be returning questionable
results. So I'll likely take Tom van Stiphout up on his suggestion to
use DateDiff in some of these places. Although, I haven't tested and
am unsure what DateDiff(Forms! MyForm!MyContro l,Now) will do if user
types 8/15/2007 in a textbox without surrounding it with #...#

I guess, in the OP, I was hoping someone might know how I could
inform A97 that a particular textbox on a form contained a date and
that any subsequent use of the value in there would be treated as if
it were a date. I mean, I can use datediff, yeah. But, looking at an
integer difference representing the number of days between two dates
and considering the sign of the value returned is more trouble than
asking "Hey, is Date1 Date2?" Huh? Huh? That's quick & easy
whereas the former is labor-laden.
Aug 8 '07 #10

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

Similar topics

3
3355
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. Here is the formula I am using now to get the number of months: #ofMonths: Month(.-.)-1. It has worked for me until now because it never was greater than a year before. Please help. I am a novice at this.
3
1632
by: Lea | last post by:
Having a problem with a query within a table for appointment schedules. In this table tblMeetingInfo are two fields, StartDate and EndDate. The date information is entered into each of these fields like: 1/12/2005 (This could represent data entered into either field). Most appointments are one day in duration. In this example, StartDate would be equal to Enddate: 1/12/2005 to 1/12/2005 (StartDate to EndDate).
9
1331
by: labelle | last post by:
I have an event listing on my website that is pulling from SQL. Unfortunately, the listing is displaying all events, especially those that are even 2 years old. I want to get rid of them and only display events that are today or in the future. I need help! Dim tmpEvents As Events tmpEvents = New EventsMapper().GetEvents() Dim tmpEvent As New Dim tmpEventListing As String For Each tmpEvent In tmpEvents
4
2382
by: jdph40 | last post by:
I enter data into a database that resides on our network. Other employees can print reports from the database, but they have no way of telling if I've entered all the data. For instance, on Mondays, I have to enter 3 days of data - Friday, Saturday, and Sunday. Last Monday, an employee came to me questioning the results on his report. He had requested data from the weekend: Friday through Sunday. But when he printed the report, I had...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10473
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10219
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.