VBA code to call another VBA code if action not taken by user within certain time.

Sahak

Well-known Member
Joined
Nov 10, 2006
Messages
1,012
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Hi All,

I need help with a code which will call another code if action not taken by user within certain time.

The scenario is as follows:
The employee should enter his Employee code in textbox of UserForm. If he pushes button “Clock_In”, VBA code right away executes “Record_Punch” code & clears textbox making ready for next employee, if he starts entering his code & did not push “Clock_In” button (for example changed mind or forgot or…) within certain time (for example 15 seconds), then should be executed another VBA code to clear textbox.

Thank you in advance.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Place in UserForm1 Mod:
Code:
Option Explicit
Dim RunAt
Const TimerSecs As Integer = 15 'seconds of delay before textbox1 clears

Private Sub Clock_In_Click()
  TimerStop
  Record_Punch
  TextBox1 = ""
End Sub

Private Sub TextBox1_Enter()
  TimerStart
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  'this proceedure resets timer to TimerSecs from last key press
  'delete this proceedure to only allow a total of TimerSecs for input
  TimerStop
  TimerStart
End Sub

Private Sub UserForm_Terminate()
  TimerStop
End Sub

Private Sub TimerStart()
  RunAt = Now + TimeSerial(0, 0, TimerSecs)
  Application.OnTime _
    EarliestTime:=RunAt, _
    Procedure:="ClearTextBox1"
End Sub

Private Sub TimerStop()
  On Error Resume Next
    Application.OnTime _
      EarliestTime:=RunAt, _
      Procedure:="ClearTextBox1", _
      Schedule:=False
  On Error GoTo 0
End Sub

Place in Standard Module:
Code:
Private Sub ClearTextBox1()
  UserForm1.TextBox1 = ""
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top