3 Attachment(s)
how to display count up timer in database field
Hi, I've made a count up timer, but I want to display the running time in the listview or other database field, can you help me?
Sorry my english is not good, I live in bali.
Here the screenshot when program run.
Attachment 96081
Here the timer code.
Attachment 96083
I want to be like this, durasi means duration that the time can running in database field when the program run.
Attachment 96085
The point is such as computer rental program at internet cafe. I have been looking everywhere but no solution.
Please help. Thank you masters.
Re: how to display count up timer in database field
This is just a slight improvement of your code, there are much better solutions around:
Code:
Private Sub Form_Load()
Timer1 = False
CmdStop.Enabled = False
ListView1.ListItems.Add 1, , "00:00:00"
End Sub
Private Sub Timer1_Timer()
Xsecond = Format$(CLng(Xsecond) + 1&, "00")
If Xsecond = "60" Then
Xsecond = "00"
xminute = Format$(CLng(xminute) + 1&, "00")
End If
If xminute = "60" Then
xminute = "00"
xmenit = Format$(CLng(xmenit) + 1&, "00")
End If
ListView1.ListItems(1).Text = xmenit & ":" & xminute & ":" & Xsecond
End Sub
BTW, rather than posting a screenshot of your code, paste it instead and surround it with Code tags ( [CODE]your code here[/CODE] ).
Re: how to display count up timer in database field
Here is another approach using DateAdd/Format functions:
Code:
Option Explicit
Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = False
lblHours.Caption = "00"
lblMinutes.Caption = "00"
lblSeconds.Caption = "00"
End Sub
Private Sub Timer1_Timer()
Dim dtTime As Date
dtTime = DateAdd("s", 1, CDate(Date & Space(1) & _
lblHours.Caption & ":" & _
lblMinutes.Caption & ":" & _
lblSeconds.Caption))
lblHours.Caption = Format(dtTime, "HH")
lblMinutes.Caption = Format(dtTime, "NN")
lblSeconds.Caption = Format(dtTime, "SS")
End Sub
Private Sub btnStart_Click()
Timer1.Enabled = True
End Sub
Private Sub btnStop_Click()
Timer1.Enabled = False
End Sub
Note: this is a very quick sample so you may need to modify it to fit your app. I would also add Restore button that will reset all labels to "00".
Re: how to display count up timer in database field
You should keep in mind that using a timer in such a way will not be 100% accurate. If you want a true counter then rather than adding 1 second each time the timer tick fires you should compare the system time with the time the counter was started.
if the timer is set to 1000 then it will be pretty close just using a counter but if it runs for very long it will start to fall behind and if there are other things going on in your app that consume CPU then it could fall way behind after a while. Using the system clock the worst that can happen is that the display does not update every second but it will not actually fall behind. If for some reason the timer is unable to fire for 3 seconds then when it does the seconds will go up by 3 rather than 1 keeping the time accurate.
Re: how to display count up timer in database field
Unless OP needs to build time sensitive scientific app ordinary timer should not be an issue.
Re: how to display count up timer in database field
Wow thanks, the code that BonnieWest and RhinoBull gave to me, it works perfectly.
I'll keep in mind not to post code in screenshot again.
But I am still confused how to program the timer according to the system time as DataMises said.
Even so the problem is solved, thanks again, you guys awesome. :)
Re: how to display count up timer in database field
To use system time instead of simply adding 1 each time the timer fires you would check the clock.
When you first start you would grab the value from the clock and store it in a variable. You could do this with the Now() function
When the timer fires you can use DateDiff() to check the number of seconds difference between the stored date/time and the current value of Now().
This would be important if you program was running some process that could take a few seconds to complete because the timer will not be adding while that prcess is running so when the timer fires using method 1 it will add 1 even if 5 seconds have passed where the clock method would add 5 in this scenario.
Re: how to display count up timer in database field
To display a counter either going up or down, you can use a TextBox or a LabelControl to do the displaying of the data. But remember if you want it to count up use the plus key and then to go down use the minus key, for that to happen...