Timer function

Helix used Ask the Experts™
Hi everyone, I have a time fuction which displayed the time on my form, what i want to do is when the time displays 9:00 the caption of a label will change

I have tried any different things to do this but with no such luck, ive tried doing:

If Time = "9:00:00" Then
Msgbox = "bla bla bla"


If Time = Format("hh....etc etc
msgbox = "bla bla bla"

but nothing happens. ive also tried

If lblTime.Caption = "9:00:00" Then
Msgbox = "bla bla bla"

What am i doing wrong?


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Show us the line you use to set the label caption?



This works...

Private Sub Timer1_Timer()
    Dim x As Date
    x = Time
    If FormatDateTime(x, vbShortTime) = "09:00" Then MsgBox "hi helix"
End Sub

You could also do a StrComp
MsgBox StrComp(FormatDateTime(x, vbShortTime), "09:00", vbTextCompare)

Don't know if that helps any....
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

It sounds like you are not running this code in a loop. To do what eric0213 suggested, drop a Timer control on your form, set it to enabled with an interval of 500, double click on it, then paste his code in the function.




thanks eric0213 that was great, thanks for the help :)

soz i havn't replied in a while, school work :(

one last problem remains though, a message box will keep appearing saying "hi helix", is there a way to only come up once?
You could set the timer interval to 60000 -- one minute in milliseconds -- this is probably the easiest and cleanest.

Or you could set a global boolean variable:
  If FormatDateTime(x, vbShortTime) = "09:00" Then
      ' do stuff
      blnAlreadyDone = True
      blnAlreadyDone = False
  End If

You're changing the label on a form, right?  Let's say you change it to "Hey buddy, it's 9:00! Wake up!"  You could do something like

  If FormatDateTime(x, vbShortTime) = "09:00" and Label1.Caption <> "Hey buddy, it's 9:00! Wake up!" Then
      ' do stuff
  End If

Lots of options... good luck


Thanks a lot, that worked perfectly :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial