Results 1 to 8 of 8

Thread: how to display count up timer in database field

  1. #1

    Thread Starter
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    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.
    Name:  1.JPG
Views: 1458
Size:  14.2 KB

    Here the timer code.
    Name:  2.JPG
Views: 1171
Size:  49.5 KB

    I want to be like this, durasi means duration that the time can running in database field when the program run.
    Name:  svrmain1.jpg
Views: 1044
Size:  10.9 KB

    The point is such as computer rental program at internet cafe. I have been looking everywhere but no solution.
    Please help. Thank you masters.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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] ).
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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".

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  5. #5

  6. #6

    Thread Starter
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    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.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  8. #8
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    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...
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width