不确定为什么VBA认为输入的日期大于Now() - 当它们不是? [英] Unsure why VBA thinks dates entered are greater than Now() - when they are not?

查看:104
本文介绍了不确定为什么VBA认为输入的日期大于Now() - 当它们不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入一个日期(早于今天的日期)进入文本框

控制之后,我很难过为什么VBA认为日期我进入

控制大于Now - 当它们比现在小得多时。

这是我在AfterUpdate事件中的代码。我必须要忽略

显而易见的。想法?


Private Sub DateSigned_AfterUpdate()


如果我!立即签名然后

我!DateSigned = Null

MsgBoxNo Way Jose!

结束如果


End Sub

解决方案



" MLH" < CR ** @ NorthState.netschreef在bericht新闻:94 ******************************** @ 4ax.com ...


输入日期(早于今天的日期)后进入文本框

控制虽然,我是难以理解为什么VBA认为我进入

控制的日期比现在还要大 - 当它们比现在要小得多时。

这里是我的代码AfterUpdate事件。我必须要忽略

显而易见的。想法?


Private Sub DateSigned_AfterUpdate()


如果我!立即签名然后

我!DateSigned = Null

MsgBoxNo Way Jose!

结束如果


结束Sub



显而易见:

首先检查你的SystemDate ....现在()给你什么价值?


BTW:如果您正在验证输入,请更好地使用BeforeUpdate事件。


Arno R


周五,2007年8月3日11:29 :07 -0400,MLH< CR ** @ NorthState.netwrote:


Arno对于BeforeUpdate有一个好处:设置Cancel = True并且有

用户再试一次。

但是真正的为什么(假设你不是那么愚蠢地重置你的计算机时钟的b $ b)就是类型强制之一。我的猜测是

整个表达式被评估为字符串而不是日期。在立即窗口中输入



(注意:今天是美国格式的8/3/2007)


? (2007年8月15日)

错误


?(8/15/2007现在)




?(#8/15/2007#Now)

True


所以我的猜测是你的表达被评估为字符串比较,

和ASCII值8/1。低于8/3。


要比较日期,请专门使用DateDiff。在比较之前它会将两个

参数投射到Date,类似于我的第三个例子。


-Tom。


>输入一个日期(早于今天的日期)进入文本框
控制之后,我很难过为什么VBA认为我进入
控制的日期比现在更大 - 当它们比现在更小时。
这是我在AfterUpdate事件中的代码。我必须忽视显而易见的事情。想法?

Private Sub DateSigned_AfterUpdate()


如果我!DateSigned Now

Me!DateSigned = Null

MsgBoxNo Way Jose!

结束如果

结束Sub


< blockquote> 8月4日上午5:26,Tom van Stiphout< no.spam.tom7 ... @ cox.netwrote:


星期五,8月3日2007 11:29:07 -0400,MLH< C ... @ NorthState.netwrote:


Arno对于BeforeUpdate有一个很好的观点:设置Cancel = True并拥有

用户再试一次。

但是真正的为什么(假设你不是那么愚蠢地重置你的计算机时钟的b $ b)是类型强制之一。我的猜测是

整个表达式被评估为字符串而不是日期。在立即窗口中输入



(注意:今天是美国格式的8/3/2007)


? (2007年8月15日)

错误


?(8/15/2007现在)




?(#8/15/2007#Now)

True


所以我的猜测是你的表达被评估为字符串比较,

和ASCII值8/1。低于8/3。


要比较日期,请专门使用DateDiff。在比较之前它会将两个

参数投射到Date,类似于我的第三个例子。


-Tom。


输入一个日期(早于今天的日期)到一个文本框

控制之后,我很难过为什么VBA认为我进入

控制大于Now - 当它们比现在小得多时。

这是我在AfterUpdate事件中的代码。我必须要忽略

显而易见的。想法?


Private Sub DateSigned_AfterUpdate()


If Me!DateSigned Now然后

我!DateSigned = Null

MsgBoxNo Way Jose!

结束如果

< blockquote class =post_quotes>
End Sub



你也可以使用


如果CDate(Me!DateSigned)日期()然后

....


同时检查控制面板/区域设置中的日期格式是否

与Access字段中的日期匹配。


问候,

Branislav Mihaljev


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_AfterUpdate()

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

End Sub

解决方案


"MLH" <CR**@NorthState.netschreef in bericht news:94********************************@4ax.com...

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_AfterUpdate()

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


On Fri, 03 Aug 2007 11:29:07 -0400, MLH <CR**@NorthState.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_AfterUpdate()

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

End Sub


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...@NorthState.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_AfterUpdate()

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

End Sub

You could also use

If CDate(Me!DateSigned) Date() Then
....

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

Regards,
Branislav Mihaljev


这篇关于不确定为什么VBA认为输入的日期大于Now() - 当它们不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆