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
| Option Explicit
Sub control_saisie(txt As Object, KeyCode)
Dim T As String, MsA$, MsJ$, msM$, X&, I&
MsA = "l'année n'est pas valide pour cette date"
MsJ = " le premier segment (jour/mois) n'est pas valide"
msM = " le 2d segment(jours/mois) n'est pas valide"
With txt
If txt = "" Then txt = "__/__/____"
T = .Value
If T Like "__/__/____" Then .SelStart = 0
X = .SelStart
Select Case KeyCode
Case 8
If Mid(T, X + 1, .SelLength) Like "*/*" Or .SelLength > 0 Then KeyCode = 0: Exit Sub
If X > 0 And .SelLength = 0 Then
Mid(T, X) = IIf(X = 3 Or X = 6, "/", "_"): .Value = T: KeyCode = 0: .SelStart = X - 1
End If
Case 46
KeyCode = 0
If .SelLength > 0 Then For I = X To X + .SelLength - 1: Mid(T, I + 1, 1) = IIf(Mid(T, I + 1, 1) <> "/", "_", "/"): Next: .Text = T: .SelStart = X: KeyCode = 0: Exit Sub
If X < 10 And Mid(T, X + 1, 1) <> "/" Then Mid(T, X + 1, 1) = "_": .Text = T: .SelStart = X + 1 Else .SelStart = X + 1
If .SelLength = 0 Then Mid(T, X + 1, 1) = IIf(Mid(T, X + 1, 1) <> "/", "_", "/"): .SelStart = X
Case Is >= 95 And KeyCode <= 105
If Mid(T, X + 1, 1) = "/" Then KeyCode = 0: Exit Sub
If .SelLength = 0 Then
If Not T Like "*_*" And IsDate(T) Then KeyCode = 0: Exit Sub
Mid(T, X + 1, 1) = Chr(KeyCode - 48): KeyCode = 0: .Value = T:
.SelStart = IIf(Mid(T, X + 2, 1) = "/", X + 2, X + 1)
ElseIf .SelLength > 1 Then
If Mid(T, X + 1, .SelLength) Like "*/*" Then KeyCode = 0: .SelLength = 0: Exit Sub
Mid(T, X + 1, .SelLength) = Chr(KeyCode - 48) & Left("____", .SelLength - 1): .Value = T: KeyCode = 0: .SelStart = X + 1: .SelLength = 1
If Mid(T, X + 1, 1) <> "_" Then .SelStart = X + 1
End If
'controle de date valide
If Val(T) > 31 Then MsgBox MsJ: .Value = "__/__/____": .SelStart = 0
If Not (Mid(T, 1, 5)) Like "*_*" And Not IsDate(Mid(T, 1, 5) & "/2000") Then MsgBox msM: Mid(T, X, 2) = "__": .Value = T: .SelStart = X - 1
If Not T Like "*_*" Then
If Not IsDate(T) Then Mid(T, 7, 4) = "____": .Value = T: .SelStart = 6: MsgBox MsA: Exit Sub
If Year(T) <> Val(Mid(T, 7, 4)) Then Mid(T, 7, 4) = "____": .Value = T: .SelStart = 6
End If
Case 13
X = txt.TabIndex
If txt.Value Like "*_*" Then With txt.Parent.Controls(X + 1): .SetFocus: .SelStart = 0: End With
End Select
End With
End Sub |