Formato dd/mm/yyyy en un textbox

Tengo una ultima duda (lo prometo) je je no se si me podrías colaborar con la programación. Dentro un textbox quisiera que se almacene fechas en formato dd/mm/yyyy, y
Que si no lo ingresa de esa manera o introduce cualquier fecha que no es correcta aparezca error.

1 respuesta

Respuesta
1
Si tengo ese código ya hecho te lo envío para que lo pruebes, debes acomodarlo colocando el nombre del textbox utilizado.
Private Sub Text18_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If Not IsNumeric("0" & Chr(KeyAscii)) Then
Beep
KeyAscii = 0
End If
End If
End Sub
Private Sub Text18_Change()
Select Case Len(Text18.Text)
Case 1
If Me.Text18.Text > 3 Then
Me.Text18.Text = "0" & Me.Text18.Text & "/"
SendKeys "{End}"
End If
Case 2
If Right(Me.Text18.Text, 1) <> "/" Then
Me.Text18.Text = Me.Text18.Text & "/"
Else
Me.Text18.Text = "0" & Me.Text18.Text
End If
SendKeys "{End}"
Case 3
If Right(Me.Text18.Text, 1) > 1 And Right(Me.Text18.Text, 1) <> "/" Then
Me.Text18.Text = Format(Me.Text18.Text & "/" & Year(Date), "DD/MM/YYYY")
SendKeys "{End}"
If IsDate(Me.Text18.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
Text18.SetFocus
SendKeys "{home}+{end}"
End If
End If
Case 4
If Right(Me.Text18.Text, 1) = "/" Then
Me.Text18.Text = Format(Me.Text18.Text & Year(Date), "DD/MM/YYYY")
SendKeys "{End}"
If IsDate(Me.Text18.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
Text18.SetFocus
SendKeys "{home}+{end}"
End If
ElseIf Right(Me.Text18.Text, 1) > 1 And Right(Me.Text18.Text, 1) <> "/" Then
Me.Text18.Text = Format(Me.Text18.Text & "/" & Year(Date), "DD/MM/YYYY")
SendKeys "{End}"
If IsDate(Me.Text18.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
Text18.SetFocus
SendKeys "{home}+{end}"
End If
End If
Case 5
If Right(Me.Text18.Text, 1) <> "/" Then
Me.Text18.Text = Format(Me.Text18.Text & "/" & Year(Date), "DD/MM/YYYY")
Else
Me.Text18.Text = Format(Me.Text18.Text & Year(Date), "DD/MM/YYYY")
End If
SendKeys "{End}"
If IsDate(Me.Text18.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
Text18.SetFocus
SendKeys "{home}+{end}"
End If
End Select
End Sub
hola leone, cambie todos los text18 por textbox3 y me aparece: Compile error   Procedure declaration does not match description of event or procedure having the same name.
¿Te mando como cambie lo que me dijiste pa que me le des un chequeada bueno?. Te agradecería tu ayuda.
Private Sub Textbox3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If Not IsNumeric("0" & Chr(KeyAscii)) Then
Beep
KeyAscii = 0
End If
End If
End Sub
Private Sub Textbox3_Change()
Select Case Len(TextBox3.Text)
Case 1
If Me.TextBox3.Text > 3 Then
Me.TextBox3.Text = "0" & Me.TextBox3.Text & "/"
SendKeys "{End}"
End If
Case 2
If Right(Me.TextBox3.Text, 1) <> "/" Then
Me.TextBox3.Text = Me.TextBox3.Text & "/"
Else
Me.TextBox3.Text = "0" & Me.TextBox3.Text
End If
SendKeys "{End}"
Case 3
If Right(Me.TextBox3.Text, 1) > 1 And Right(Me.TextBox3.Text, 1) <> "/" Then
Me.TextBox3.Text = Format(Me.TextBox3.Text & "/" & Year(Date), "DD/MM/YYYY")
SendKeys "{End}"
If IsDate(Me.TextBox3.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox3.SetFocus
SendKeys "{home}+{end}"
End If
End If
Case 4
If Right(Me.TextBox3.Text, 1) = "/" Then
Me.TextBox3.Text = Format(Me.TextBox3.Text & Year(Date), "DD/MM/YYYY")
SendKeys "{End}"
If IsDate(Me.TextBox3.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox3.SetFocus
SendKeys "{home}+{end}"
End If
ElseIf Right(Me.TextBox3.Text, 1) > 1 And Right(Me.TextBox3.Text, 1) <> "/" Then
Me.TextBox3.Text = Format(Me.TextBox3.Text & "/" & Year(Date), "DD/MM/YYYY")
SendKeys "{End}"
If IsDate(Me.TextBox3.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox3.SetFocus
SendKeys "{home}+{end}"
End If
End If
Case 5
If Right(Me.TextBox3.Text, 1) <> "/" Then
Me.TextBox3.Text = Format(Me.TextBox3.Text & "/" & Year(Date), "DD/MM/YYYY")
Else
Me.TextBox3.Text = Format(Me.TextBox3.Text & Year(Date), "DD/MM/YYYY")
End If
SendKeys "{End}"
If IsDate(Me.TextBox3.Text) = False Then
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox3.SetFocus
SendKeys "{home}+{end}"
End If
End Select
End Sub
Me parece que estas trabajando es desde el editor de visual Basic de Excel y no con la aplicación Visual basic directamente.
Te envío la coreccion para el editor de Visual basic de Excel.
Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If Not IsNumeric("0" & Chr(KeyAscii)) Then
Beep
KeyAscii = 0
End If
End If
End Sub
El evento change si esta bien solo debes cambiar el evento keypress.
MUCHAS GRACIAS LEONE, pero una duda más, el año se introduce solo 2008, sucederá lo mismo en el caso de que ya estemos en el 2009, ¿se introducirá solo este también?
Otra consulta, ¿al momento de hacer la prueba coloque 54 en la parte de días y me lo coloco de la siguiente manera 05/04/2008, como podría hacerlo para que aparezca error en ese caso?. Que no permita ingresar en el día un numero mayor a 31, ¿en elmes un numero mayor a 12?
Con respecto a la primera pregunta, si, el coloca el año actual, cuando pasemos al 2009 comenzara a colocar ese año.
La segunda pregunta cuando introduces 54 te da 05/04/2008, esto es porque al teclear el numero 5, el ya sabe que no puede colocar otro dígito más para el día, solo permite cuando es 1,2 ó 3 y te coloca 05/ el siguiente numero que coloques sera el mes. Cuando colocas el 4 igualmente asume que este es el mes, solo permite colocar otro dígito si el mes comienza con 1.
Puedes ajustar la programación a una lógica más sencilla para validar la fecha, pero el usuario tendrá que teclear más. Si el usuario se acostumbra a esta lógica colocara fechas de manera muy rápida.
Si no deseas: eliminar el año predeterminado, permitir el día 54 y luego te envíe un mensaje, y otras cosas lo puedes ajustar.
Te recomiendo estudies el código, una vez comprendas lo que hace podrás ajustarlo a tus necesidades.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas