Io inserisco del codice per 3 eventi:
codice:
'Superfluo
Public Sub ObjData_GotFocus(obj As Object)
On Error Resume Next
obj.SelStart = 0
obj.SelLength = Len(obj.Text)
End Sub
Public Sub ObjData_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8 ' accetta solo 0-9 e {BS}
'Accetta solo i numeri
Case Else
KeyAscii = 0
Beep
End Select
End Sub
Public Sub ObjData_Validate(obj As Object, Cancel As Boolean)
On Error GoTo ErrValidate
If Not obj.DataChanged Then Exit Sub
If Len(obj.Text) = 0 Then Exit Sub
If IsDate(obj.Text) Then Exit Sub
'Controllo quante cifre sono state inserite, se 6 o 8
Select Case Len(obj.Text)
Case 6 'inserito la data breve
obj.Text = Format(Format(obj.Text, "00/00/00"), "Short Date")
Case 8 'inserito la data lunga
obj.Text = Format(Format(obj.Text, "00/00/0000"), "Short Date")
Case Else
Beep
MsgBox "Data errata! Inserire la data nel formato GGMMAAAA."
obj.Text = ""
Cancel = True
Exit Sub
End Select
'ora controllo se è una data valida
If Not IsDate(obj.Text) Then
MsgBox "Inserire una data valida nel formato GGMMAA oppure GGMMAAAA, senza alcun separatore." & vbCr & vbCr & "Esempi di date valide:" & vbCr & "Inserire: 050201" & vbTab & " per ottenere: 05/02/2001" & vbCr & "Inserire: 05022001" & vbTab & " per ottenere: 05/02/2001", vbExclamation, "Data non valida"
obj.SelStart = 0
obj.SelLength = Len(obj.Text)
Cancel = True
Exit Sub
End If
Exit Sub
ErrValidate:
Cancel = False
End Sub