Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check time of last click event 1

Status
Not open for further replies.

jon92

Technical User
May 30, 2001
37
GB
I have a button on a form - when the user carries out a certain
task they click the button, this logs the time and date and
task type etc.

The following code is attached to the onclick event:
Code:
Me.fldtasktype.Value = "Task description"
Me.fldtaskCode.Value =  "xxx"
Me.fldUsername.Value = Environ("UserName")
Me.fldtaskDate.Value = Now()
DoCmd.GoToRecord , , acNewRec
I have a feeling some of the users may think it's fun
to click the button a few times instead of once !!

Is it possible to add to the above code a routine that
will check the time of the last click event and ignore
the event if it's less than say 30seconds since the last
event?

any help would be appreciated

jon
 
Have a look at this thread:

thread702-887882


Cheers!
 
sorry wrong thread:

This is correct:
thread702-894232
 
Thanks for the reply Mike

The code I have posted works fine.

If I can briefly explain - some of the users (may!) think it amusing to click the button several times instead of the required once. I would like to trap any click events approx 30 seconds after the 1st click and thus ignore the command to create a new record.

cheers
 
I would handle this by creating a textbox on the form (let's call it txtLastClick). Then, in your OnClick code for your button, you can use the datediff function to test to make sure it has been at least 30 seconds since the last click.

It would look something like this (in pseudocode):
Code:
if (30 seconds or more) then
  create record
  timestamp txtLastClick
endif

However, this approach may backfire if you have users entering data quickly. It might be good to make it configurable or provide an override.

Another approach (in pseudocode):
Code:
if (less than 30 seconds) then
  response = Msgbox("You just clicked.  Record another?", vbYesNo)
  if response = vbNo then
    Exit Sub
  endif
endif

create record
timestamp txtLastClick
endif

One final note:
If you use my suggestion, you need to deal with the possibility of txtLastClick being Null (when you first enter the form). I recommend the Nz function.
 
KornGeek
Thanks for the advice but I feel I'm getting out of my depth!
I like the second suggestion using the msgbox. I'm keen to learn more about code, where do you advise me to look for info
on syntax for what you suggest?

cheers
Jon

 
For syntax, I would check the VBA help files.

Just to help you out this time:
Code:
Private Sub btnMyButton_Click
  On Error Goto btnMyButton_Error

  Dim LastClick as Date
  Dim lngSeconds as Long
  Dim response as Integer

  'Convert text box to a date (and accomodate NULL)
  LastClick = CDate(Nz(Me.txtLastClick, #1/1/1980#))

  'Calculate seconds elapsed since last click
  lngSeconds = DateDiff("s", LastClick, Now())

  'Allow user to override time limit
  If lngSeconds < 30 then
    response = Msgbox("Really create another record?", vbYesNo + vbDefaultButton2 + vbQuestion, "MyDBTitle")
    If response = vbNo then
      Exit Sub
    End If
  End If

  'Create record
  Me.fldtasktype.Value = "Task description"
  Me.fldtaskCode.Value =  "xxx"
  Me.fldUsername.Value = Environ("UserName")
  Me.fldtaskDate.Value = Now()
  DoCmd.GoToRecord , , acNewRec
  
  btnMyButton_Exit:
    Exit Sub

  btnMyButton_Error:
    'Handle errors
    Msgbox "Error " & Err.Number & ": " & Err.Description, vbOkOnly, "MyDBTitle"
    Resume btnMyButton_Exit

End Sub

Remember to make txtLastClick invisible. Hope this helps
 
KornGeek

Thankyou again,

I have inserted the code but the textbox txtLastClick is not converting to a date (visible or invisible) when button is clicked

Jon
 
Oops. I left out an important line of code. In the section where you create a record, you need to insert the line:

Me.txtLastClick = Now()

That should fix it.
 
KornGeek

Thanks for your help - have a star
I will definately find some time to read the vba help files!

cheers

jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top