Link to home
Start Free TrialLog in
Avatar of Stephen Byrom
Stephen ByromFlag for Ireland

asked on

Time Diff

I have a button with the following code attached,
 
Private Sub btnAdd_Click()
On Error GoTo Err_btnAdd_Click
    txtTimeNow = Now
If txtLastPall < ((DateAdd("n", 0, txtTimeNow)) - 30) Then
    MsgBox "Too soon to have made another Pallet", vbInformation, "Not Possible"
    Exit Sub
End If
    txtLastPall = Time
    txtPallDate = Date
    txtPallTime = Time
    Me.Dirty = False
    DoCmd.GoToRecord , , acNewRec
    PallGraph.Requery
Exit_btnAdd_Click:
    Exit Sub
Err_btnAdd_Click:
    MsgBox Err.Description
    Resume Exit_btnAdd_Click
End Sub

Open in new window

all the text boxes are hidden
txtLastPall is unbound and txtTimeNow is unbound.
In Line 4 I am trying to get the message to display if the button is clicked too soon.
i.e.the button should display the message if less than 30 minutes has elapsed since the last time it was clicked.
I have tried many variations of the DateAdd formula in Line 4 but cannot get it to do what I need.
Please point me in the right direction.

As always, your time is appreciated.
Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stephen Byrom

ASKER

Thanks Cap,
but it allows me to keep clicking the button and adding records.
I need it to exit the sub if less than 30 min has elapsed.
close the form and open, then try the operation again.
take note that this line
       txtLastPall = Now
was also changed
Thanks Cap,
I had missed the other change.
I copied/pasted the entire code and it works fine.
Thanks for your time.
Personally, I would encourage you to use date/time variables rather than text variable to store date and time values.  I would also encourage you to force explicit declaration of all your variables.  You can do this by adding the line:

Option Explicit

right below:

Option Compare Database

at the top of each code module.  You can also do this by setting the Required Variable Declaration checkbox by opening the Options dialog box (open VBA, then click Tools, and Options).

You could also use:

If DateDiff("n", dtLastPall,  dtTimeNow) < 30 Then

instead of

If txtLastPall > DateAdd("n", -30, txtTimeNow) Then
Thanks for the advice Fyed
All the modules in the project are option explicit, someone else told me that some time ago.
I have declared the variables as you suggest, and also changed the line and textbox names..
Thank you very much for the advice.