1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 32, 47, 58, 48 To 57 'espace, slash, deux-points et chiffres
Case Else: KeyAscii = 0
End Select
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim I As Integer
Dim Texte As String
With TextBox1
'contrôle la bonne saisie
For I = 1 To Len(.Text)
Select Case I
Case 1, 2, 4, 5, 7, 8, 9, 10, 12, 13, 15, 16
If Not IsNumeric(Mid(.Text, I, 1)) Then .Text = Left(.Text, I - 1)
Case 3, 6
If Mid(.Text, I, 1) <> "/" Then .Text = Left(.Text, I - 1) & "/"
Case 11
If Mid(.Text, I, 1) <> " " Then .Text = Left(.Text, I - 1) & " "
Case 14
If Mid(.Text, I, 1) <> ":" Then .Text = Left(.Text, I - 1) & ":"
End Select
Next I
'ajoute les caractères pour une saisie plus rapide
Select Case Len(.Text)
Case 2, 5: .Text = .Text & "/"
Case 10: .Text = .Text & " "
Case 13: .Text = .Text & ":"
End Select
'les secondes ne pouront être saisies
If Len(.Text) > 16 Then .Text = Left(.Text, Len(.Text) - 1)
'contrôle la validité des veleurs entrées
If Len(.Text) = 16 Then
If Valide(.Text, Texte) = False Then
If InStr(Texte, "Date") <> 0 Then MsgBox Texte: .Text = "" Else MsgBox Texte: .Text = Left(.Text, 11)
End If
End If
End With
End Sub
Function Valide(Valeur As String, Retour As String) As Boolean
Dim LaDate As Date
Valide = True
On Error Resume Next
LaDate = DateValue(Split(Valeur, " ")(0))
If Err.Number <> 0 Then
Valide = False
Retour = "Date non valide !"
Exit Function
End If
Err.Clear
LaDate = TimeValue(Split(Valeur, " ")(1))
If Err.Number <> 0 Then
Valide = False
Retour = "Heure non valide !"
End If
End Function |
Partager