date & Time with two criteria (1 Viewer)

theinviter

Registered User.
Local time
Today, 07:02
Joined
Aug 14, 2014
Messages
240
Hi guys;
i need help, i have a form which have a button to print a paper and when the user click on the button as there are two field one with date and another filed with time, need to verify if the date and time less than now plus 5 hour then will not able to print the form but if above then will be able to print.
Field A = Exp_date
Field B = Time

please find the code i tried below.

Code:
Private Sub Command47_Click()
On Error GoTo errhandler:
    N = 1 / 0    ' cause an error
 If exp_date < (Now() + 0.209) And time < (Now() + 0.209) Then
        ' Display a message
        MsgBox "Date entered not correct", vbExclamation
        ' Cancel the update
        Cancel = True ' -1 or 37 would work just as well
        Else
        DoCmd.RunMacro "LABEL MACRO"
        End If
        
        Exit Sub
errhandler:
    ' error handling code
    Resume Next

End Sub
 
Last edited by a moderator:

June7

AWF VIP
Local time
Today, 06:02
Joined
Mar 9, 2014
Messages
5,463
You tried code and what happened - error message, wrong result, nothing? Have you step debugged?

Why do you have line to cause error?
 

theinviter

Registered User.
Local time
Today, 07:02
Joined
Aug 14, 2014
Messages
240
I tried the code but it does not work and only execute the first line of the code
Display a message
MsgBox "Date entered not correct", vbExclamation
Even if the the date and time entered more than now.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:02
Joined
Oct 29, 2018
Messages
21,449
Hi. Not sure which part you need help with, but I would probably use the DateAdd() function to calculate the current time plus 5 hours.
 

June7

AWF VIP
Local time
Today, 06:02
Joined
Mar 9, 2014
Messages
5,463
Cancel = True is not appropriate for button click event and really serves no purpose. It is used in BeforeUpdate (and some others) event. Did you copy this code from somewhere?

Really should have Option Explicit at top of every code module. If you did, the Cancel reference would trigger compile error because it is not a declared variable.

Time is a reserved word (it is an intrinsic function) and advise not to use reserved words as names for anything.

Consider:
Code:
If Me.exp_date + Me.time < DateAdd("h", 5, Now()) Then
    ' Display a message
    MsgBox "Date entered not correct", vbExclamation
Else
    DoCmd.RunMacro "LABEL MACRO"
End If
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:02
Joined
May 7, 2009
Messages
19,231
Code:
Private Sub Command47_Click()
On Error GoTo errhandler:
    N = 1 / 0    ' cause an error
 If Cdate([exp_date]) + CDate([time]) < DateAdd("h", 5, Now()) Then
        ' Display a message
        MsgBox "Date entered not correct", vbExclamation
        Else
        DoCmd.RunMacro "LABEL MACRO"
        End If
exit_sub:        
        Exit Sub
errhandler:
    ' error handling code
    Resume exit_sub

End Sub
 

Users who are viewing this thread

Top Bottom