Author Topic: Trigger VBA sub at certain time of day  (Read 1639 times)

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Trigger VBA sub at certain time of day
« on: March 06, 2017, 10:58:18 PM »
Hi,

I have a VBA sub which, when a button is clicked, gets a value from a text file and writes it to the PLC. This text file is updated at 5:30am every day, so what I would like is for this VBA sub to run automatically every day at 6am, so that my value is automatically updated every day, whether or not I click the button.

Is there any way of triggering a VBA sub based on time of day?

I have an always-updating, hidden form that sits out of sight and handles all of my background tasks like this, so there's no issues with which form is open at 6am, and the code itself is working - all I need is a way of triggering it.

I could put a data subscriber on the hidden form and monitor the PLC's RTC, and watch for it being "6", but I'm hoping there's a cleaner way...

Thanks!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Trigger VBA sub at certain time of day
« Reply #1 on: March 06, 2017, 11:03:26 PM »
- Add a timer to the form
- Set Interval to 31000
- Set Enabled to True
- Double click the timer to get back to the code
- Enter code similar to this:

Code: [Select]
        If Now.Hour = 6 And Now.Minute = 0 Then
                 '* call you code here
        End If

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Trigger VBA sub at certain time of day
« Reply #2 on: March 06, 2017, 11:29:08 PM »
Thanks, I'll give that a try.

I assume that the timer is a self-resetting timer, and the interval (31000) is 31 seconds?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Trigger VBA sub at certain time of day
« Reply #3 on: March 07, 2017, 12:31:01 AM »
Yes, it will continue to fire every 31 seconds.

ASF

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Trigger VBA sub at certain time of day
« Reply #4 on: March 15, 2017, 01:09:47 AM »
Thanks Archie, that works.