Technical stuff you probably wouldn't understand.

Rather than cluttering up other blogs with my musings, I made my own.

Thursday, May 04, 2006

Infopath Timer

I found some good tips on this issue, and built a little bit-o code to make a timer for a detail record. Needs to be broken down into reusable classes, but the concept is usable as-is.

I'll clear it and shoot the code. Psudo-code to follow.

Note: Infopath SP1, using VB.Net Form Code.

1) Add the following fields to the level of your doc that can be timed. (For example if you have a heder with multiple details, you may want to start a timer at each detail item, so each would need these fields):
  • TimerRunning, Boolean, False
  • TimerStartTime, DateTime
  • ElapsedDuration, Decimal
2) Add a button control, and and click "Edit Form Code" to create an event.

  • Test e.Source.selectSingleNode("ActTimerRunning").text = "true". Then true, the timer is running for this item, and the stop and accumulate code executes
    • Turn the timer flag to off
    • Calculate Elapsed time by comparing the current system time to our timer flag
e.Source.selectSingleNode("ActTimerRunning").text = "false"
ldc_elapsed = Math.Round(System.DateTime.Now().Subtract(Iso8601ToDate(e.Source.selectSingleNode("ActStopDateTime").text)).TotalHours, 2) ' Calc elapsed and Round to two decimal places.


ldc_currentActDuration = CType(e.Source.selectSingleNode("ActDuration").text, Decimal)
e.Source.selectSingleNode("ActDuration").text = (ldc_currentActDuration + ldc_elapsed).ToString
e.Source.selectSingleNode("ActStopDateTime").text = DateToIso8601(DateTime.Now)

  • Timer is not running, exec Start Timer logic
    • e.Source.selectSingleNode("ActTimerRunning").text = "true"
    • e.Source.selectSingleNode("ActStopDateTime").text = DateToIso8601(DateTime.Now)



) Date Util functions for Infopath VB.Net
These two functions allow you to convert dates to/from infopath friendly date formats. (Note, this date format is an ISO standard imposed by the XML Consortium, not by MS or Infopath).

Private Function Iso8601ToDate(ByVal iso8601Date As String) As DateTime

Dim provider As IFormatProvider

If (iso8601Date Is DBNull.Value) Then

Throw New ArgumentNullException("iso8601Date")
End If


Return DateTime.ParseExact(iso8601Date, "yyyy-MM-ddTHH:mm:ss", provider)


End Function


Private Function DateToIso8601(ByVal adtdateTime As DateTime) As String


Return adtdateTime.ToString("yyyy-MM-ddTHH:mm:ss")


End Function

0 Comments:

Post a Comment

<< Home