Hi
If i want to repeat an action every X seconds..how can i do that?
and another question: how can i use the real time to be printed in an output file?
Thanks
Printable View
Hi
If i want to repeat an action every X seconds..how can i do that?
and another question: how can i use the real time to be printed in an output file?
Thanks
First things first: how to use the timer
Timers work in milliseconds...once you have the timer on your form (let's assume you call it timer1) you call it using "timer1.interval = 6000"...this sets it to "fire" after 6 seconds. Now double-click the timer control on your form and put code in for what you want to happen every 6 seconds. If at any time you need to stop the timer, put "timer1.enabled = false" in your code
Second one is a simple pre-defined variable called "time"...if you put "label1 = time" in your code and had label1 on your form, it would have the current time put into it. It's as simple as that to do the same to a textbox or an output to file or printer
Try this (state on how many seconds you would like to write date and time to file):
VB Code:
Option Explicit Dim interval As Integer Dim WithEvents myTimer As Timer Private Sub Form_Load() Set myTimer = Me.Controls.Add("VB.Timer", "myTimer") Do Until IsNumeric(interval) = True And interval > 0 interval = InputBox("State your inteval (in s):", "Interval", "10") interval = interval * 1000 'multiply to get ms Loop myTimer.interval = interval End Sub Private Sub myTimer_timer() Dim ff As Integer ff = FreeFile Open "c:\test.txt" For Output As #ff Print #ff, Date & " - " & Time 'writing to file Close #ff End Sub
BIG THANKS smUX
your post was usefull and was rated :)
by the way, do you know how can i create a file and write some data into it..
creating a file is simple...what usually isn't simple is when you get into different features, or what you can do...here's a quick and dirty way to write an array to a file
VB Code:
fre = FreeFile 'freefile returns the next available free number Open "c:\data.txt" For Output As #fre 'output is usually the method you would use, or input for 'getting it back in...there's others, but ignore those for now For b = 1 To 100 Print #fre, Array(b) 'print #fre is printing to the file. We use multiple numbers 'because you can theoretically print(write) to 100+ files at 'a time...some people read in a file with one and write the 'same with another and make changes to the data as it goes 'through this process :-) Next b Close fre 'Got to close the file after writing or it'll never be readable
The green lines are comments and are just there to give you extra information. You don't *have* to use an array or a loop, you can put 1 "print #fre," per variable you wish to write, but most people tend to store the data in an array and write it in one big bunch for ease of use
Thank you very much gavio and smUX
but how can i write in the file witout losing the information written in it?
Do you mean an already saved file? If so, change "output" to "append" and it'll append the data to the end of the file
Open it, read it, add something and write it. For example:
edit: or like smuX suggested ;)VB Code:
Dim iFF As Integer Dim sFile As String Dim sContents As String iFF=FreeFile sFile="c:\test.txt" 'read it Open sFile For Input As #iFF sContents=Input(LOF(iFF),iFF) Close #iFF 'write what was before and add something new Open sFile For Output As #iFF Print #iFF, sContents Print #iFF, "something new" Close #iFF
here's another way of writing the array at once:VB Code:
Open "C:\data.txt" For Output As #fre Print #fre, Join(MyArray, vbCrLf) Close #fre
Also, "sContents=Input(LOF(iFF),iFF)" uses a clever trick to take the whole file into a string quickly...although I think it might be missing something (or gavio is using a format I don't know about for input :-))
Another way to do what Gavio did with loading a file quickly is in my sig below :-)
Edit: and I never knew about the join command that Bush suggested there...could be useful later :-)
Thanks to all of you guys..you were really helpful and my problems were solved :)
i uesd Append that smUX suggested :)
We do what we can :-)
If you got your answer then you can pull down the Thread Tools menu and Mark Thread Resolved ;)