Application.OnTime and Procedure that Set it

VenturSum

Board Regular
Joined
May 23, 2010
Messages
137
Guru's

Can Application.OnTime call the procedure that set it?
This would create a loop.

I get an error saying:
Cannot run the macro 'RunCodeAgain()'. The macro may not be available in this workbook or all macros may be disabled.

For Example:
Code:
Public Sub RunCodeAgain()
       
        
        '--- Do Stuff & have an Exit --------------
        
        '--- Rerun this routine in 10 seconds ---
        Application.OnTime EarliestTime:=Now + TimeValue("0:00:10"), _
                           Procedure:="RunCodeAgain()", _
                           Schedule:=True

End

Thanks,
John,
In Annapolis, MD
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I suspect the RunCodeAgain routine is located in a Class module.

Try changing it to this :

Code:
Application.OnTime EarliestTime:=Now + TimeValue("0:00:10"), _
                           Procedure:=[B][COLOR=Red]Me.CodeName & ".RunCodeAgain"[/COLOR][/B], _
                           Schedule:=True
 
Upvote 0
Now I get the error:
Cannot run the macro "C:\Users\...\TestBook.xlsm'!frmLogin.AdviseSymbols'. The Macro may not be available in this workbook or all macros may be disabled.

I think I better check to see if the macro itself can't be recalled.
I am using a vendor's DLL to call a Live Stream of data.

Thanks,

John,
In Annapolis, MD
 
Upvote 0
I've verified that the issue is due to the public routine existing in the code associated with the form. And, that I am apparently not calling the routine properly. I'll try to fix the call to the form's code. Or I may try moving the code to a module.

Any suggestions for calling code associated with a form??
The above example did not work.

John.
 
Upvote 0
I've verified that the issue is due to the public routine existing in the code associated with the form. And, that I am apparently not calling the routine properly. I'll try to fix the call to the form's code. Or I may try moving the code to a module.

Any suggestions for calling code associated with a form??
The above example did not work.

John.

AFAIK,you can't use the timed procedure inside a userform. You must place it in a module. There is an API workaround to get the address of the timer function via the the Form object Virtual table but it is overkill.

A simpler approach ,if you really must have all your code self contained within the userform, is to use a loop as follows :

Code:
Option Explicit

Private bStop As Boolean

Private Const lRunWhen As Single = 10

Private Sub UserForm_Activate()

    Dim T As Single

    bStop = False
    T = Timer
    Do
        If Timer - T >= lRunWhen Then T = Timer: Call RunCodeAgain
        DoEvents
    Loop Until bStop

End Sub

Private Sub UserForm_Terminate()

    bStop = True

End Sub

Private Sub RunCodeAgain()

    MsgBox "Timed routine running."

End Sub

Despite the running loop you can still interact with the form and/or run other code.
 
Upvote 0
Jaafar & Company,
I keep running into the limits of VBA.. in this case multi-tasking/threading.
To receive a live feed from my broker, VBA can't be running. to allow the WithEvents actions can be triggered and run. These events must be associated with an object, in this case a userform, I'm trying to move it down a Class Module.

btw, While in a userform {frmLogin}, I am able to set Application.OnTime to call a procedure outside the userform. But I can't seem to get it to call a proceedure inside the userform:


This does not call the AdviseSymbols proceedure in {frmLogin}:
Code:
  Application.OnTime EarliestTime:=Now + TimeValue("0:00:05"), _
                   Procedure:="[COLOR=DarkRed]frmLogin.AdviseSymbols[/COLOR]", _
                   Schedule:=True
Any thoughts on a work around to call inside the userform. I've tried CallByName().


Thanks,
John
In Annpolis, MD
 
Upvote 0
If the form procedure is public, I would think you can use OnTime to call a stub procedure in a normal module that simply calls the form procedure.
 
Upvote 0
Well that didn't work..

Any suggestions, if it is even possible, to address a procedure in a userform.
This was placed in a normal module:
Code:
   Application.OnTime EarliestTime:=Now + TimeValue(sWaitTime), _
                 Procedure:="[COLOR=DarkRed]frmLogin.TestStringInUserFrm[/COLOR]", _
                  Schedule:=True
I tried:
Code:
   Application.OnTime EarliestTime:=Now + TimeValue(sWaitTime), _
                 Procedure:=CallByName("frmLogin", "TestStringInUserFrm", VbMethod, ""), _
                  Schedule:=True
but the CallByName() was not set properly, and I haven't used it before.

Any help appreciated.

Thanks,
John,
In Annapolis, MD
 
Upvote 0
That's not what I suggested. Your OnTime has to schedule a procedure in a normal module and that procedure can then call the form procedure.
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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