davedave24
Programmer
This is strange. I have a textbox that formats to hh:mm. When the user clicks the button, it determines if the time is in the correct format:
- if it's hh:mm, it checks if the hours are > 23, and mins > 59
- if it's h:mm, it checks if the mins are > 59
- if it's >5 numbers (i.e the user has entered 123:45)
-formatting code is below
The problem is that the simple validation If-ElseIf statement won't go past the Time textbox. It validates all of the above rules, but then won't check if textPostCode is empty (and anything else after this).
Extra: this just formats the time textbox to hh:mm, or to ##:## if the user doesn't enter a colon
This just stops the user entering anything other than numbers and colon
- if it's hh:mm, it checks if the hours are > 23, and mins > 59
- if it's h:mm, it checks if the mins are > 59
- if it's >5 numbers (i.e the user has entered 123:45)
-formatting code is below
The problem is that the simple validation If-ElseIf statement won't go past the Time textbox. It validates all of the above rules, but then won't check if textPostCode is empty (and anything else after this).
Code:
'this is the Submit/Ok button
If textTime.Text = "" Then
MsgBox "Please enter a delivery Time", vbExclamation + vbOKOnly, "Oops!"
textTime.SetFocus
ElseIf Len(textTime) = 5 Then 'hh:mm
If Mid(textTime, 1, 2) > 23 Then
MsgBox "Please enter a proper time" & vbCrLf & vbCrLf & "Format: hh:mm", vbExclamation + vbOKOnly, "Not like that!"
textTime.SetFocus
ElseIf Mid(textTime, 4, 2) > 59 Then
MsgBox "Please enter a proper time" & vbCrLf & vbCrLf & "Format: hh:mm", vbExclamation + vbOKOnly, "Not like that!"
textTime.SetFocus
End If
ElseIf Len(textTime) = 4 Then 'h:mm
If Mid(textTime, 3, 2) > 59 Then
MsgBox "Please enter a proper time" & vbCrLf & vbCrLf & "Format: hh:mm", vbExclamation + vbOKOnly, "Not like that!"
textTime.SetFocus
End If
ElseIf Len(textTime) > 5 Then 'too many numbers
MsgBox "Please enter a proper time" & vbCrLf & vbCrLf & "Format: hh:mm", vbExclamation + vbOKOnly, "Not like that!"
textTime.SetFocus
ElseIf textPostCode.Text = "" Then
MsgBox "Please enter a Post Code", vbExclamation + vbOKOnly, "Oops!"
textPostCode.SetFocus
End If
Extra: this just formats the time textbox to hh:mm, or to ##:## if the user doesn't enter a colon
Code:
Private Sub textTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
textTime.BackColor = &HFFFFFF
If InStr(textTime.Text, ":") Then
textTime.Text = Format(textTime.Text, "hh:mm")
Else
textTime.Text = Format(textTime.Text, "##:##")
End If
End Sub
This just stops the user entering anything other than numbers and colon
Code:
Private Sub textTime_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(":")
Case Asc("-")
If InStr(1, Me.textTime.Text, "-") > 0 Or Me.textTime.SelStart > 0 Then
KeyAscii = 0
End If
Case Asc(".")
If InStr(1, Me.textTime.Text, ".") > 0 Or Me.textTime.SelStart > 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub