Check time value

L

Lars Brownies

In VBA I need to check whether the current time is after 8 in the evening
but before 4 in the morning. I looked at the time methods but I can't figure
out how to do this check. Can someone point me in the right direction?
Thanks,
Lars
 
D

Dirk Goldgar

Lars Brownies said:
In VBA I need to check whether the current time is after 8 in the evening
but before 4 in the morning. I looked at the time methods but I can't
figure out how to do this check. Can someone point me in the right
direction?


By "current time", do you mean the time of day as reported by the system
clock? If so, try this:

Dim dtTimeNow As Date

dtTimeNow = Time()

If (dtTimeNow >= #8:00 PM#) Or (dtTimeNow < #4:00 AM#) Then
' The time is between 8 PM and 4 AM
Else
' It isn't.
End If

Notice that I have assumed that "after 8 in the evening" includes the time
of 8:00 PM, but "before 4 in the morning" does *not* include the time of
4:00 AM. That may or may not be what you had in mind. If it's not, you can
modify the comparison operators in the If statement.
 
L

Lars Brownies

Thanks Dirk. That's it!

Lars

Dirk Goldgar said:
By "current time", do you mean the time of day as reported by the system
clock? If so, try this:

Dim dtTimeNow As Date

dtTimeNow = Time()

If (dtTimeNow >= #8:00 PM#) Or (dtTimeNow < #4:00 AM#) Then
' The time is between 8 PM and 4 AM
Else
' It isn't.
End If

Notice that I have assumed that "after 8 in the evening" includes the time
of 8:00 PM, but "before 4 in the morning" does *not* include the time of
4:00 AM. That may or may not be what you had in mind. If it's not, you
can modify the comparison operators in the If statement.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 
J

Jerry Whittle

Debug.Print Now() > (Date() + 0.833333333333333) and Now() < (Date() +
1.166666666666667)

or

Debug.Print Now() > DateAdd("h", 20,Date()) AND Now() < DateAdd("h",
28,Date())
 
Top