Private Sub TextBox1_Change()
If Not FormatDate(TextBox1) And Len(TextBox1) < 10 Then
TextBox1.SetFocus
TextBox1.SelStart = 100
Exit Sub
ElseIf Not FormatDate(TextBox1) And Len(TextBox1) = 10 Then
TextBox1.SetFocus
TextBox1.SelStart = 100
MsgBox "Vous n'avez pas saisi une date valide"
Exit Sub
End If
If FormatDate(TextBox2) Then
If Not Verif2dates(TextBox1, TextBox2) Then
MsgBox "le début du contrat est postérieur à la fin de contrat" _
& vbLf & vbLf & "vérifiez les dates saisies"
End If
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not FormatDate(TextBox1) Then
Cancel = True
TextBox1.SelStart = 100
Beep
End If
End Sub
Private Sub Textbox2_Change()
If Not FormatDate(TextBox2) And Len(TextBox2) < 10 Then
TextBox2.SetFocus
TextBox2.SelStart = 100
Exit Sub
ElseIf Not FormatDate(TextBox2) And Len(TextBox2) = 10 Then
TextBox2.SetFocus
TextBox2.SelStart = 100
MsgBox "Vous n'avez pas saisi une date valide"
Exit Sub
End If
If FormatDate(TextBox1) Then
If Not Verif2dates(TextBox1, TextBox2) Then
MsgBox "le début du contrat est égal ou postérieur à la fin de contrat" _
& vbLf & vbLf & "vérifiez les dates saisies"
End If
End If
End Sub
Private Sub Textbox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not FormatDate(TextBox2) Then
Cancel = True
TextBox2.SelStart = 100
Beep
End If
End Sub
Function FormatDate(x As Control)
If Len(x) <> 10 Then GoTo Pas_Date
On Error GoTo Pas_Date
FormatDate = IsDate(CDate(x.Value))
Exit Function
Pas_Date:
FormatDate = False
End Function
Function Verif2dates(x As Control, y As Control) As Boolean
If Not FormatDate(x) Then
Verif2dates = False
ElseIf Not FormatDate(y) Then
Verif2dates = False
Else
Verif2dates = CDate(x) < CDate(y)
End If
End Function