Private Sub TextBox3_Change()
On Error Resume Next
x = Len(TextBox3)
y = LTrim(TextBox3.Text)
d = TextBox3
If d = "" Then
Exit Sub
End If
If Left(d, 2) > 31 Then
TextBox3.SelStart = 0
TextBox3.SelLength = Len(TextBox3)
Exit Sub
End If
If x = 2 Then TextBox3 = y & "/"
If x = 4 Then Exit Sub
If Mid(d, 4, 2) = "" Then Exit Sub
If Mid(d, 4, 2) > 12 Then 'ora controlliamo che il mese non superi il numero 12, si avvisa, si esce
TextBox3.SelStart = 3
TextBox3.SelLength = Len(TextBox3)
Exit Sub
End If
'--------per rendere completo il sistema di verifica, inseriamo le istruzioni che controllino anche di non superare il 'giorno di fine mese rispetto ai mesi di febbraio e dei mesi a fine 30
e = Left(d, 2) 'con la variabile "e" prendiamo il giorno della data introdotta
f = Mid(d, 4, 2) 'con la variabile "f" prendiamo il mese della data introdotta
Select Case f 'usiamo il Select Case che verificherà il Caso Febbraio (02 in numero)
Case "02"
If e > 29 Then GoTo mess 'se il giorno ("e") è maggiore di 29 avvisiamo, selezioniamo e usciamo dalla routine
Case "04", "06", "09", "11" 'ora si controlla i Case dei mesi di 30 giorni
If e > 30 Then GoTo mess 'se il giorno ("e") è maggiore di 30 avvisiamo, selezioniamo e usciamo dalla routine
End Select
If x = 5 Then
TextBox3 = y & "/"
Exit Sub
End If
If x = 6 Then Exit Sub
If x = 7 Then Exit Sub
If x = 8 Then Exit Sub
If x = 9 Then Exit Sub
g = Mid(d, 7, 4)
If g <= 1950 Or g >= 2101 Then ' ora controlla se l'anno è compreso tra il 1900 ed il 2100 (variabile)
MsgBox "L'anno deve essere tra il 1900 ed il 2100", Title:=" "
TextBox3.SelStart = 6
TextBox3.SelLength = Len(TextBox3)
Exit Sub
End If
If IsDate(TextBox3) = False Then 'ora controlla se la data è una data valida
MsgBox "La data non è valida - Controllare mese, giorno e anno", Title:=" "
TextBox3.SelStart = 0
TextBox3.SelLength = Len(TextBox3)
Exit Sub
End If
If x = 10 Then
TextBox3.SetFocus 'il cursore si sposta su una seconda textbox al fine di far partire "Private Sub textbox1_Exit"
End If
Exit Sub
mess:
MsgBox "Giorno inesistente nel mese " & f & "", Title:=""
TextBox3.SelStart = 0
TextBox3.SelLength = Len(TextBox3)
Set x = Nothing
Set y = Nothing
Set d = Nothing
Set e = Nothing
Set f = Nothing
Set g = Nothing
Resume
End Sub |