Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

timer function

WilkoWilko Member Posts: 8
Hi guys, im looking to incorporate a timer function into my database.

Basically at the moment students can takes test in the 'quiz' form - id like a function that starts a timer once its opened, and then display the time taken in a message box when the test is finished.

Any ideas on how i should go about this would be much appreciated,

Ross

Comments

  • dokken2dokken2 Member Posts: 532
    : Hi guys, im looking to incorporate a timer function into my database.
    :
    : Basically at the moment students can takes test in the 'quiz' form - id like a function that starts a timer once its opened, and then display the time taken in a message box when the test is finished.
    :
    : Any ideas on how i should go about this would be much appreciated,
    :
    : Ross
    :

    I used three labels, lblSTART, lblSTOP, lblTIME and cmd-buttons to start/stop the timer. You could move the the cmdSTART_Click() to the form_load event if you want the test to begin as soon as the form is opened but as a student I would prefer having a START button.

    You also would want some better error handling in case the student clicks the start button by mistake [maybe set the ENABLE property so it can't be clicked twice]


    Option Compare Database

    Dim dtSTART As Date 'STARTING TIME
    Dim dtSTOP As Date 'STOP TIME

    Private Sub cmdSTART_Click()
    Me.TimerInterval = 1000 'START TIMER

    dtSTART = Time 'STARTING TIME
    Me.lblSTART.Caption = dtSTART 'SHOW STARTING TIME
    End Sub

    Private Sub cmdSTOP_Click()
    Me.TimerInterval = 0 'STOP TIMER

    dtSTOP = Time 'STOP TIME
    Me.lblSTOP.Caption = dtSTOP 'SHOW STOP TIME

    Dim dtELAPSED As String
    dtELAPSED = ElapsedTime(dtSTART, dtSTOP) 'CALCULATE ELAPSED TIME
    Me.lblTIME.Caption = dtELAPSED
    MsgBox "Elapsed time = " & dtELAPSED, vbExclamation, "Test Time"
    End Sub

    'CALCULATE ELAPSED TIME
    Private Function ElapsedTime(ByVal d1 As Date, ByVal d2 As Date) As String
    Dim HR As Integer, Min As Integer
    Dim tmp As String

    HR = DateDiff("h", d1, d2) 'HOURS
    Min = DateDiff("n", d1, d2) - (HR * 60) 'MINS, MINUS HOURS

    tmp = ""
    tmp = Format(HR, "00") & ":"
    tmp = tmp & Format(Min, "00") & ":00"

    ElapsedTime = tmp 'RETURN ELAPSED
    End Function

    Private Sub Form_Timer()
    Me.lblTIME.Caption = Time 'UPDATE CURRENT TIME
    End Sub

Sign In or Register to comment.