PC Review


Reply
 
 
Memphis
Guest
Posts: n/a
 
      13th Mar 2010
Hello,
I visited Chip Pearson's webpage regarding the "Scheduling Events With
OnTime ".
I found his solution to work well. http://www.cpearson.com/excel/OnTime.aspx
Now, what I want to do, is not have the process take place in a module, but
instead in a private sub.. For example, I have a master form from which
copies are made, and I don't need the Modules to be copied over to the "child
forms". Since the modules are not beign copied I thought I might be able to
insert this code, with a bit of tweaking, into a command button (cmdBegin).

So this is what I wrote:

Private Sub cmdBegin_Click()
Dim RunWhen As Double
Dim MSG As String
Dim cRunIntervalSeconds As Double
cRunIntervalSeconds = 30 ' Thirty Seconds
Dim cRunWhat As String ' "Note: deviated here from his code purely for
testing purposes"
cRunWhat = "TheSub" ' the name of the procedure to run

GoTo StartTimer
StartTimer:
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat,
Schedule:=True

TheSub:

MSG = MsgBox("Hello World", vbOKCancel)
If MSG = vbOK Then
ActiveWorkbook.Save
GoTo StartTimer ' Reschedule the procedure
Else
Exit Sub
End If

StopTimer:
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat,
Schedule:=False
End Sub

It works, but unfortunately once I click OK, it immediately prompts me to
save again. Unless I click cancel.
Now.. I put the VBOkCancel in the path to allow me to bail out of the enless
"saving loop".

Any thoughts?

Thank you
 
Reply With Quote
 
 
 
 
Wouter HM
Guest
Posts: n/a
 
      16th Mar 2010
Hi Memphis,

It look simple to me.
1:
Put this code in a module.


' -- start of code
Public RunWhen As Double
Public Const cRunIntervalSeconds = 2 ' two minutes
Public Const cRunWhat = "TheSub" ' the name of the procedure to run


Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
Schedule:=True
End Sub


Sub TheSub()
MSG = MsgBox("Hello World", vbOKCancel)
If MSG = vbOK Then
ActiveWorkbook.Save
StartTimer ' Reschedule the procedure
End If
End Sub

Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
Schedule:=False
End Sub

' --- end of code

2: for the commandbutoon:

Private Sub cmdBegin_Click()
StartTimer
End Sub
 
Reply With Quote
 
Memphis
Guest
Posts: n/a
 
      25th Mar 2010
Thank You Wouter.
with your help I stiched this together
I found a small problem with the code you provided, this is where the
problem was, you had "2", and this number indicates the number of seconds and
not minutes, so 120 seconds = 2 minutes. ;-)

Public RunWhen As Double
Public Const cRunIntervalSeconds = 120'
Public Const cRunWhat = "TheSub" ' the name of the procedure to run

Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat,
Schedule:=True
End Sub

Sub TheSub()
ActiveWorkbook.Save
StartTimer ' Reschedule the procedure
End Sub

Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat,
Schedule:=False
End Sub

"Wouter HM" wrote:

> Hi Memphis,
>
> It look simple to me.
> 1:
> Put this code in a module.
>
>
> ' -- start of code
> Public RunWhen As Double
> Public Const cRunIntervalSeconds = 2 ' two minutes
> Public Const cRunWhat = "TheSub" ' the name of the procedure to run
>
>
> Sub StartTimer()
> RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
> Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
> Schedule:=True
> End Sub
>
>
> Sub TheSub()
> MSG = MsgBox("Hello World", vbOKCancel)
> If MSG = vbOK Then
> ActiveWorkbook.Save
> StartTimer ' Reschedule the procedure
> End If
> End Sub
>
> Sub StopTimer()
> On Error Resume Next
> Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
> Schedule:=False
> End Sub
>
> ' --- end of code
>
> 2: for the commandbutoon:
>
> Private Sub cmdBegin_Click()
> StartTimer
> End Sub
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AutoSave Cheese Microsoft Access 2 9th Jan 2008 05:08 PM
Opened Autosave but when closing workbook Autosave closes itself =?Utf-8?B?S2Vu?= Microsoft Excel Worksheet Functions 0 29th Oct 2005 05:11 PM
Removing AUTOSAVE.XLS(AUTOSAVE.XLA) from VBA editor??? Simon Lloyd Microsoft Excel Programming 9 17th Jul 2004 10:24 AM
Autosave Bob Jones Microsoft Excel Setup 3 9th Jul 2004 06:48 PM
autosave fullymooned Microsoft Excel Misc 4 26th Mar 2004 07:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:52 AM.