VERIFICAR SE A HORA E VALIDA
Olá gente... Existe alguma função q me retorna se a hora é válida ou não ??? Abraço a todos.
Oks,
Rafael Portal
Oks,
Rafael Portal
Como assim hora valida????
Ué !!!
Tipo, eu quero somente horas q variam entre 00:01 e 23:59 entende ... é isso .. Existe alguma função do VB que verifique isso ? Abraços.
Oks,
Rafael Portal
Tipo, eu quero somente horas q variam entre 00:01 e 23:59 entende ... é isso .. Existe alguma função do VB que verifique isso ? Abraços.
Oks,
Rafael Portal
veja
Private Sub Command1_Click()
MsgBox IsDate("23:59")
MsgBox IsDate("24:59")
End Sub
Veja :
'Adicione um TextBox com o nome de txtHora e use este código.
Private bDozeHoras As Boolean
Private bComSegundos As Boolean
Private Sub txthora_Change()
With txthora
If bDozeHoras Then
If Len(.Text) = 1 And .Text > "1" Then .Text = "": .SelStart = Len(.Text)
If Len(.Text) = 2 And Mid(.Text, 2, 1) > "2" Then .Text = Left(.Text, 1): .SelStart = Len(.Text)
Else
If Len(.Text) = 1 And .Text > "2" Then .Text = "": .SelStart = Len(.Text)
End If
If Len(.Text) = 2 And Mid(.Text, 1, 1) = "2" And Mid(.Text, 2, 1) > "4" Then .Text = Left(.Text, 1): .SelStart = Len(.Text)
If Len(.Text) = 4 And Mid(.Text, 4, 1) > "5" Then .Text = Left(.Text, 3): .SelStart = Len(.Text)
If bComSegundos Then
If Len(.Text) = 7 And Mid(.Text, 7, 1) > "5" Then .Text = Left(.Text, 6): .SelStart = Len(.Text)
End If
Me.Caption = Len(.Text)
End With
End Sub
Private Sub txthora_GotFocus()
If bComSegundos Then
txthora.MaxLength = 8
Else
txthora.MaxLength = 5
End If
Screen.ActiveControl.SelStart = 0: Screen.ActiveControl.SelLength = Len(Screen.ActiveControl.Text)
End Sub
Private Sub txthora_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Or KeyAscii = 8 Then
If Len(txthora.Text) = 4 Then txthora.Text = Left(txthora.Text, 3): txthora.SelStart = Len(txthora.Text)
If Len(txthora.Text) = 7 Then txthora.Text = Left(txthora.Text, 6): txthora.SelStart = Len(txthora.Text)
Else
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
Else
If Len(txthora.Text) = 2 Then txthora.Text = txthora.Text & ":": txthora.SelStart = Len(txthora.Text)
If bComSegundos Then
If Len(txthora.Text) = 5 Then txthora.Text = txthora.Text & ":": txthora.SelStart = Len(txthora.Text)
End If
End If
End If
End Sub
Private Sub txthora_LostFocus()
With txthora
If bComSegundos Then
If Mid(.Text, 3, 1) <> ":" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 6, 1) <> ":" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If bDozeHoras Then
If Left(.Text, 1) > "1" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Left(.Text, 2) > "2" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 4, 1) > "5" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 7, 1) > "5" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
Else
If Left(.Text, 1) > "2" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 4, 1) > "5" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 7, 1) > "5" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
End If
Else
If Mid(.Text, 3, 1) <> ":" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If bDozeHoras Then
If Left(.Text, 1) > "1" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Left(.Text, 2) > "2" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 4, 1) > "5" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
Else
If Left(.Text, 1) > "2" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
If Mid(.Text, 4, 1) > "5" Then: MsgBox "Hora inválida!": .SetFocus: Exit Sub
End If
End If
End With
End Sub
'Obs:
'1 ) A hora esta no formanto "HH:MM" para se usar no formato "HH:MM:SS" ative a variável : bComSegundos
'2) A hora esta no formato de 24 horas, para se ativar o formato de 12 horas, ative a variável : bDozeHoras
Tópico encerrado , respostas não são mais permitidas