Results 1 to 3 of 3
  1. #1
    mduplantis is offline Advanced Beginner
    Windows Vista Access 2003
    Join Date
    Jun 2010
    Location
    Omaha, Ne
    Posts
    65

    Question Formatting Timer with DateDiff

    Hi,



    I'm working on a timer project, but I can't figure out what is happening with my timer when I try to format it. My project works without formatting, but as soon as I format it, the timer stops counting, although the formatting is correct. My overall goal is to format the timer to Ss.

    Thanks for any help!


    Option Compare Database
    Option Explicit
    Dim StartTime As Date
    Dim ElapsedTime As Date




    Private Sub cmdReset_Click()
    txtTimeOutput.Value = ":00"
    Me.TimerInterval = 0
    End Sub


    Private Sub cmdStart_Click()


    Me.TimerInterval = 1000 ' 1 second
    StartTime = Time()


    End Sub


    Private Sub cmdStop_Click()


    Me.TimerInterval = 0
    'Me.txtTimeOutput.Value = ":00"


    End Sub


    Private Sub Form_Load()


    Me.TimerInterval = 0


    End Sub


    Private Sub Form_Timer()


    'txtTimeOutput.Value = DateDiff("s", StartTime, Time() )
    txtTimeOutput.Value = Format(DateDiff("s", StartTime, Time()), "Ss")


    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    The Forms' TimerInterval property needs to be set during the Form's On Load event.

    However, I do not see you using the Form's Timer Event.

    I see you using the DateDiff function. The Date Diff function returns a Long Integer. It does not return a Date type. Also, if memory served me correctly, the Time() function has been deprecated in VBA so recommend using Now().

    In the click event to start
    Dim dtStart As Date
    dtStart = Now

    and in the click event to stop
    Dim lngAnswer As Long
    lngAnswer = DateDiff("s", dtStart, Now)
    Me.txtTimeOutput.Value = lngAnswer


    Now that you have counted the seconds from start to stop, using the System's clock, you can not format that result to HH:MM:SS using VBA. I am not aware of a time conversion method in VBA. There is in other programming languages.
    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    Using the system clock is not always accurate to the second, and I don't know of a way to get fractions of a second using the system clock and VBA. To get fractions of a second and also make sure that you do not lose a second in your overall count, use the form's timer event.

  3. #3
    mduplantis is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2010
    Location
    Omaha, Ne
    Posts
    65
    Okay, I will start over again with the form's timer event. Thank you for the help! Brent

Please reply to this thread with any new information or opinions.

Similar Threads

  1. In Use Timer
    By dccjr in forum Programming
    Replies: 7
    Last Post: 09-18-2013, 12:40 PM
  2. Timer on IIf Statements?
    By athyeh in forum Programming
    Replies: 4
    Last Post: 07-29-2013, 09:54 PM
  3. Debugger and Timer
    By DepricatedZero in forum Programming
    Replies: 2
    Last Post: 05-29-2013, 01:15 PM
  4. countdown Timer
    By yigitki in forum Programming
    Replies: 16
    Last Post: 11-11-2011, 11:24 AM
  5. Timer object
    By joki in forum Programming
    Replies: 7
    Last Post: 03-17-2011, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums