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
LVL 1
Stephen ByromWarehouse/ShippingAsked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
try this  revision
Private Sub btnAdd_Click()
On Error GoTo Err_btnAdd_Click
     txtTimeNow = Now   
If txtLastPall > DateAdd("n", -30, txtTimeNow) Then
    MsgBox "Too soon to have made another Pallet", vbInformation, "Not Possible"
    Exit Sub
End If

    txtLastPall = Now
    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

0
 
Stephen ByromWarehouse/ShippingAuthor Commented:
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.
0
 
Rey Obrero (Capricorn1)Commented:
close the form and open, then try the operation again.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Rey Obrero (Capricorn1)Commented:
take note that this line
       txtLastPall = Now
was also changed
0
 
Stephen ByromWarehouse/ShippingAuthor Commented:
Thanks Cap,
I had missed the other change.
I copied/pasted the entire code and it works fine.
Thanks for your time.
0
 
Dale FyeCommented:
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
0
 
Stephen ByromWarehouse/ShippingAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.