Function ChecaMesAno(Data As String) As Boolean
'FUNÇÃO PARA CHECAR SE DATA É VÁLIDA
'FORMATO DE ENTRADA DA DATA: MM/AAAA
'SAÍDA: ChecaData = TRUE - DATA VÁLIDA
' ChecaData = FALSE - DATA INVÁLIDA
ChecaMesAno = True
If Len(Data) <> 7 Then
ChecaMesAno = False
Exit Function
End If
Dim X As Integer
Dim erro As Boolean
erro = False
For X = 1 To Len(Data)
Select Case X
Case 1, 2, 4, 5, 6, 7:
If Not IsNumeric(Mid(Data, X, 1)) Then
erro = True
Exit For
End If
Case 3:
If Mid(Data, X, 1) <> "/" Then
erro = True
Exit For
End If
End Select
Next
If erro Then
ChecaMesAno = False
Exit Function
End If
Dim Mes As Integer
Mes = Mid(Data, 1, 2)
If Mes < 1 Or Mes > 12 Then
ChecaMesAno = False
Exit Function
End If
If Val(Right(Data, 4)) < 1900 Then
ChecaMesAno = False
Exit Function
End If
End Function
Public Sub CampoData_MesAno_Change(ByRef oControle As Object)
Dim X As Byte
Dim AuxStart As Byte
Dim AuxData As String
AuxStart = oControle.SelStart
AuxData = ""
For X = 1 To Len(Trim(oControle.Text))
If Mid(oControle.Text, X, 1) <> "/" Then
AuxData = AuxData & Mid(oControle.Text, X, 1)
End If
Next X
Select Case Len(Trim(AuxData))
Case Is < 3: oControle.Text = AuxData
Case 3: FlagData = False
oControle.Text = Mid(AuxData, 1, 2) & "/" & Mid(AuxData, 3, 1)
Case Is > 3: FlagData = False
oControle.Text = Mid(AuxData, 1, 2) & "/" & Mid(AuxData, 3, 4)
End Select
Select Case AuxStart
Case 3: oControle.SelStart = 4
Case Else: oControle.SelStart = AuxStart
End Select
End Sub