Adding timer to text box


G

Guest

I have a subform where I am calculating time difference between current time
and the last update. I have a text box txtTimeDiff with control source
=ElapsedTimeString([Last Update],Now())

Is it possible to add timer so this text box and time difference can be
updated every second?


Here is ElapsedTimeString function
Public Function ElapsedTimeString(Date1 As Date, _
Date2 As Date) _
As String

Dim interval As Double, str As String, days As Variant
Dim hours As String, Minutes As String, seconds As String

If IsNull(Date1) = True Or _
IsNull(Date2) = True Then Exit Function

interval = Date2 - Date1
days = Fix(CSng(interval))
hours = Format(interval, "h")
Minutes = Format(interval, "n")
seconds = Format(interval, "s")



' Hours part of the string
str = str & IIf(hours = "0", "", _
IIf(hours = "1", hours & " Hour", hours & " Hours"))
str = str & IIf(hours = "0", "", _
IIf(Minutes & seconds <> "00", ", ", " "))

' Minutes part of the string
str = str & IIf(Minutes = "0", "", _
IIf(Minutes = "1", Minutes & " Minute", _
Minutes & " Minutes"))

str = str & IIf(Minutes = "0", "", _
IIf(seconds <> "0", ", ", " "))

'Seconds part of the string
str = str & IIf(seconds = "0", "", _
IIf(seconds = "1", seconds & " Second", _
seconds & " Seconds"))


If seconds > 30 Then Minutes = Minutes + 1
If Minutes > 59 Then hours = hours + 1: Minutes = 0

ElapsedTimeString = IIf(str = "", "0", str)


End Function
 
Ad

Advertisements

J

John W. Vinson

Is it possible to add timer so this text box and time difference can be
updated every second?

Not to the textbox, but you can use the Form's Timer event. Set the form's
Timer property to 1000 (milliseconds, to fire the event once a second) and its
On Timer event to code which sets the value of the textbox to your function.

John W. Vinson [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top