Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time function 1

Status
Not open for further replies.

Terence

Technical User
Feb 22, 2001
30
US
help, i have tried to use the "time" function with no success. i have tried to follow what is suggested in the MSDN libary but still a dead end. Please help with getting the system time. i wrote code as follows, where did i go wrong

dim comptime as string
dim mytime as string

comptime = time
mytime= (" 5:00:00 A.M")

if mytime = comptime then

......
end if

this was the only way i could show this without errors. if i used the # sign i get errors.
 
You need to Dim your variables as Date. Then you can use the # signs.
Dim comptime As Date
Dim mytime As Date

comptime = time
mytime = #5:00:00 AM#

If mytime = comptime Then
.....
End If

You could also just use:
If comptime = #5:00:00 AM# Then Rick Sprague
 
Actually, While I agree w/ rick in that the vars SHOULD be date type, the function would work with just the porper format for mytime (also shown correctly as date/time type in Rick's post - just not mentioned)

mytime = #5:00:00 AM#

Not the subtle differnece a single period can make!

mytime= (" 5:00:00 A.M")



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Michael, I considered the string-based solution, but rejected it because I thought it might not work on machines with different "regional settings" for date/time. If somebody sets up his machine for a 24-hour clock, or with a different time separator, the string-based comparison won't work, but I think the date/time one will.

The only thing I really know about localization is that it's easier to avoid it when possible, rather than adapt to it. Am I worrying too much, do you think? Rick Sprague
 
Rick,

No! I did NOT mean that you were - in ANY WAY - not correct. At most, my post was meant to point out that one of the errors/changes made in your code (and I beleive it is an important one) was the CORRECT format for the time.

Perhaps I was being to subtle in the observation.



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Dim MyStr, MyTime As String
Dim comptime As String

comptime = Time
MyTime = #5:00:00 AM#
MyStr = Format(MyTime, "hh:mm:ss AMPM")
If MyStr = comptime Then


End If Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX.

Download Demo version on my Site:
Promotions before 02/28/2001 (free source codebook),visite my site
 
Thanks for all your help, i am new at this and need all the help i can get.
I got it to work using Ricks format, but now i can't get it to run in the form by itself.
i have it in a "Sub" but not sure how to declare it in the declarations so that the "time"
is always active. If i click the sub or put it in the "form_load" it runs once thats all.
 
Oh, so you're looking for something that will wake up at 5:00 AM, eh?

This is what you were trying to do, then:
Code:
Public Sub WaitUntilFive()
  Dim comptime As Date
  Do
    DoEvents
    comptime = Time()
    ' if you want to show the time passing on the form,
    ' uncomment the next line
    ' MyTextBox = Format$(comptime, "Long Time")
  Loop Until comptime = #5:00:00 AM#
End Sub

Your code where you want to wait would then just call WaitUntilFive.

The above routine will use up a lot of your computer's CPU time, and could make other background programs run slow, even though the loop includes a DoEvents call. If you want something that interferes less with other programs that might be running in the background, look into using a Timer component. Rick Sprague
 
Okay Rick thats not it but it does work,(of course). What i am trying to do is use a running clock in my form with other subs running. in one other sub i will save a file at a set time each day.
The way i see this running now it will not go to any other sub while this "Do" is running. I was under the impression that the Time() would run in the background while the rest of the code could run.
 
No, Time() just returns the current system time immediately. What you want is a Timer component on your form. This is an invisible (at run time) component that you use by setting its Interval property to the number of milliseconds you want to pass before the component fires a Timer event. Meanwhile you can do other stuff.

When the Timer event fires, you can do anything you want in it, like saving your file. The event procedure can then set the Interval again to schedule another Timer event later, if desired. When the procedure exits, whatever code was running will resume. I think that's exactly what you want.

When using Timer controls, you have to keep in mind that the Timer event occurs "asynchronously", which means the current state of your application at that moment is unpredictable. There might be situations in which your application should not save the file (e.g., you might be doing some sort of global search and replace). If such conditions exist, your non-Timer code should set a "don't save now" flag until it's safe to save. The Timer routine should test that flag, and if it's set, the Timer should wait for a short interval to pass (say, 1 second) before trying again. It can do this simply by setting Interval to 1000 and exiting.

Also remember that whatever form contains the Timer control must stay open the entire time it's waiting. If you close it, the Timer will be canceled. Rick Sprague
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top