jcutler
04-08-2014, 04:26 PM
Private Sub txtTestTimeInput_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With txtTestTimeInput
txtTestTimeInput.Text = Format(txtTestTimeInput.Value, "##:##")
End With
Dim strMsge As String
Dim arrSplit
dteDate = 0
If Trim(Len(txtTestTimeInput)) = 0 Then Exit Sub 'Required to allow deletion of time
arrSplit = Split(txtTestTimeInput.Value, ":")
If UBound(arrSplit) <> 1 Then 'Does not have colon separator
strMsge = "Incorrect time format. Use colon as separator."
GoTo msgLabel
End If
If arrSplit(0) < 0 Or arrSplit(0) > 23 Then
strMsge = "Hours must be between 0 and 23."
GoTo msgLabel
End If
If arrSplit(1) < 0 Or arrSplit(1) > 59 Then
strMsge = "Minutes must be between 0 and 59."
GoTo msgLabel
End If
dteTime = TimeSerial(arrSplit(0), arrSplit(1), 0)
txtTestTimeInput.Value = Format(dteTime, "hh:mm") 'Re-write time in correct format.
Exit Sub 'If gets to here then time OK so exit here
msgLabel:
Cancel = True 'Cancels the update and cursor remains in the TextBox control
MsgBox strMsge & vbCrLf & "Re-enter time correctly in hhmm format."
txtTestTimeInput.Text = ""
End Sub
I found this above code on another forum and the only problem that I can see so far is if the user enters the time between midnight(00:00) and 1am(01:00). It will execute this part of the code. In other words when the user enters "00" for midnight the program will display the below message. What could I add to the current code to fix this problem. Thanks.
If arrSplit(0) < 0 Or arrSplit(0) > 23 Then
strMsge = "Hours must be between 0 and 23."
GoTo msgLabel
End If
With txtTestTimeInput
txtTestTimeInput.Text = Format(txtTestTimeInput.Value, "##:##")
End With
Dim strMsge As String
Dim arrSplit
dteDate = 0
If Trim(Len(txtTestTimeInput)) = 0 Then Exit Sub 'Required to allow deletion of time
arrSplit = Split(txtTestTimeInput.Value, ":")
If UBound(arrSplit) <> 1 Then 'Does not have colon separator
strMsge = "Incorrect time format. Use colon as separator."
GoTo msgLabel
End If
If arrSplit(0) < 0 Or arrSplit(0) > 23 Then
strMsge = "Hours must be between 0 and 23."
GoTo msgLabel
End If
If arrSplit(1) < 0 Or arrSplit(1) > 59 Then
strMsge = "Minutes must be between 0 and 59."
GoTo msgLabel
End If
dteTime = TimeSerial(arrSplit(0), arrSplit(1), 0)
txtTestTimeInput.Value = Format(dteTime, "hh:mm") 'Re-write time in correct format.
Exit Sub 'If gets to here then time OK so exit here
msgLabel:
Cancel = True 'Cancels the update and cursor remains in the TextBox control
MsgBox strMsge & vbCrLf & "Re-enter time correctly in hhmm format."
txtTestTimeInput.Text = ""
End Sub
I found this above code on another forum and the only problem that I can see so far is if the user enters the time between midnight(00:00) and 1am(01:00). It will execute this part of the code. In other words when the user enters "00" for midnight the program will display the below message. What could I add to the current code to fix this problem. Thanks.
If arrSplit(0) < 0 Or arrSplit(0) > 23 Then
strMsge = "Hours must be between 0 and 23."
GoTo msgLabel
End If