date & Time with two criteria

theinviter

Registered User.
Local time
Yesterday, 18:03
Joined
Aug 14, 2014
Messages
244
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:
You tried code and what happened - error message, wrong result, nothing? Have you step debugged?

Why do you have line to cause error?
 
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.
 
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.
 
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:
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

Back
Top Bottom