Calculando datas no InfoPath 2003
14/03/2011 1 Comentário
Um dos grandes problemas do InfoPath é a incapacidade de calcular datas, para isto devemos progredir utilizando codificação, e neste caso vou utilizar VBScript para isto.
O artigo original está no link: http://www.bizsupportonline.net/infopath2003/datediff-vbscript-infopath.htm
Mas vou resumir o artigo acima.
1. Modifique o InfoPath para executar códigos em VBScript ao invés de JScript: Ferramentas – Opções do Formulário – Avançado
2. Crie três campos no seu formulário, sendo:
a) startDate e endDate como DateTime
b) dateDiff e dateInterval como Text
3. Acrescente o código conforme informações abaixo:
Dentro do formulário:
Function ISODateStringToVBDate(ISODateString)
Dim dtRetVal
dtRetVal = Null
If Trim(ISODateString) <> “” Then
dtRetVal = DateSerial(Mid(ISODateString, 1, 4), _
Mid(ISODateString, 6, 2), Mid(ISODateString, 9, 2))
End If
ISODateStringToVBDate = dtRetVal
End Function
Sub CalculateDateDifference()
Dim strStartDate
Dim strEndDate
Dim strDateInterval
Dim dtStart
Dim dtEnd
Dim intDateDiff
‘Retrieve the ISO date from the InfoPath date fields
strStartDate = XDocument.DOM.selectSingleNode(“/my:meusCampos/my:startDate”).text
strEndDate = XDocument.DOM.selectSingleNode(“/my:meusCampos/my:endDate”).text
‘Convert ISO date into VBScript date
dtStart = ISODateStringToVBDate(strStartDate)
dtEnd = ISODateStringToVBDate(strEndDate)
If Not IsNull(dtStart) And Not IsNull(dtEnd) Then
‘Retrieve the date interval to be used
strDateInterval = XDocument.DOM.selectSingleNode(“/my:meusCampos/my:dateInterval”).text
Select Case LCase(strDateInterval)
Case “yyyy”
‘Difference between dates in years
intDateDiff = DateDiff(“yyyy”, dtStart, dtEnd)
Case “m”
‘Difference between dates in months
intDateDiff = DateDiff(“m”, dtStart, dtEnd)
Case “d”
‘Difference between dates in days
intDateDiff = DateDiff(“d”, dtStart, dtEnd)
Case “h”
‘Difference between dates in hours
intDateDiff = DateDiff(“h”, dtStart, dtEnd)
Case “n”
‘Difference between dates in minutes
intDateDiff = DateDiff(“n”, dtStart, dtEnd)
Case “s”
‘Difference between dates in seconds
intDateDiff = DateDiff(“s”, dtStart, dtEnd)
Case Else
‘If interval is not recognized so use difference in days
intDateDiff = DateDiff(“d”, dtStart, dtEnd)
End Select
‘Set the value of the dateDiff field
XDocument.DOM.selectSingleNode(“/my:meusCampos/my:dateDiff”).text = intDateDiff
End If
End Sub
4. Duplo clique no campo startDate vá em Validação de Dados e depois selecione OnAfterChange no evento e clique em Editar. Acrescente o código a seguir, tanto no startDate quanto do endDate.
Sub msoxd_my_startDate_OnAfterChange(eventObj)
If eventObj.IsUndoRedo Then
Exit Sub
End If
CalculateDateDifference
End Sub