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
| Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
verifdate TextBox1
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
verifdate TextBox2
End Sub
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
verifdate TextBox3
End Sub
Private Sub TextBox1_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer)
control_saisie TextBox1, keycode
End Sub
Private Sub TextBox2_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer)
control_saisie TextBox2, keycode
End Sub
Private Sub TextBox3_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer)
control_saisie TextBox3, keycode
End Sub
Private Sub control_saisie(txt As Object, keycode)
'--merci de laisser ces 2 lignes de commentaires si vous utiliser ce mécanisme--
'-- l'auteur de cette fonction est Patrick Verne----:):):):):)Alias patricktoulon--------------------------
Dim T$, X&
With txt
T = .Text
X = .SelStart
If .SelLength = 10 And keycode = 46 Then txt = "__/__/____": keycode = 0: .SelLength = 0: Exit Sub
If Mid(T, X + 1, .SelLength) Like "*/*" Then keycode = 0: Exit Sub
Select Case keycode
Case 13:: Exit Sub
Case 8
.SelStart = X: keycode = 0:
If X < 6 Then Mid(T, 4, 2) = "__": .Text = T: .SelStart = 2
If X < 3 Then Mid(T, 1, 2) = "__": .Text = T: .SelStart = 0
If X >= 6 Then Mid(T, 7, 4) = "____": .Text = T: .SelStart = 5
Case 46
.SelStart = X: keycode = 0:
If X < 3 Then Mid(T, 1, 2) = "__": .Text = T: .SelStart = 3
If X >= 3 And X < 6 Then Mid(T, 4, 2) = "__": .Text = T: .SelStart = 6
If X > 4 Then Mid(T, 7, 4) = "____": .Text = T: .SelStart = 0
Case 96 To 105
T = .Text
X = .SelStart
If .SelLength > 1 Then
Mid(T, X + 1, .SelLength) = Chr(keycode - 48) & Left("___", .SelLength - 1): keycode = 0: .Text = T: .SelStart = X + 1: .SelLength = 0
If Not IsDate(Mid(T, 1, 6) & "2000") Then Mid(T, 4, 2) = "__": .SelStart = 3
Exit Sub
End If
If .SelLength = 1 Then
Mid(T, X + 1, .SelLength) = Chr(keycode - 48): keycode = 0: .Text = T
If Not IsDate(Mid(T, 1, 6) & "2000") Then Mid(T, 4, 2) = "__": .Text = T: .SelStart = 3
Exit Sub
End If
If InStr(T, "_") = 0 And .SelLength = 0 Then keycode = 0
T = .Text
If InStr(1, T, "_") <> 0 Then Mid(T, InStr(1, T, "_")) = Chr(keycode - 48): keycode = 0
If Val(Mid(T, 1, 1)) > 3 Then Mid(T, 1, 1) = "_"
If Val(Mid(T, 1, 2)) > 31 Then Mid(T, 1, 2) = "__"
If Val(Mid(T, 4, 1)) > 1 Then Mid(T, 4, 1) = "_"
If Val(Mid(T, 4, 2)) > 12 Then Mid(T, 4, 1) = "__": keycode = 0: Exit Sub
If InStr(1, T, "_") = 4 And Not IsDate(Mid(T, 1, 3) & "01/2000") Then Mid(T, 1, 2) = "__": .SelStart = 0
If InStr(1, T, "_") = 7 And Not IsDate(Mid(T, 1, 6) & "2000") Then Mid(T, 4, 2) = "__": .SelStart = 3
If Not T Like "*_*" And Not IsDate(Mid(T, 1, 6) & "2000") Then Mid(T, 4, 2) = "__": .Text = T
If Not T Like "*_*" And Not IsDate(T) Then Mid(T, 7, 4) = "____"
.Text = T: .SelStart = InStr(1, T, "_") - IIf(InStr(1, T, "_") = 0, 0, 1):
Exit Sub
Case Else: keycode = 0
End Select
End With
End Sub
Private Sub verifdate(txt As Object)
Dim ctrl, z
Dim dToDay As Date, Darrive$, DdiFF&
If Not txt.Value Like "*_*" Then
dToDay = Date: Darrive = txt.Value
DdiFF = DateDiff("d", dToDay, CDate(Darrive))
MsgBox "la diference est de " & DdiFF & " jours"
Else
If txt.Value = "__/__/____" Then Exit Sub
MsgBox "la date n'apas été compléteée entierement "
'***************************************************
'cette ligne ne fait pas son boulot conflit avec le focus du textbox suivant le celui qui est en cause
txt.SetFocus: txt.SelStart = InStr(1, txt.Text, "_") - 1
'******************************************************
End If
End Sub |
Partager