JasonTFleishman
New member
- Local time
- Today, 16:38
- Joined
- Nov 2, 2021
- Messages
- 15
Excellent! The back story is ... I've decided to change my Datebook TimeFrom control to string format instead of a DateTime format so that partial time entries are accepted, and the computer deduces what was intended. The new control accepts the following partial time entries:1. Use the right tool for the job. There is no need to use a Case Statement with a Boolean test:
Select Case IsNumeric(CCValue)
Use an IF there, not a Select Case
2. Use the right tool for the job #2. If each Case of the Select is going to be a test itself, then Select probably isn't the right tool to begin with. Break out of the Select and just make each Case test an If test:
if A>B Then...
if A=C Then...
3. Speciulation and an assumption---Are all those numerical tests going to work on CCValue? I'm not certain. That first Select uses IsNumeric(CCValue) which makes me think CCValue is a string. Then later on everything is a numerical test against CCValue. And numbers and strings don't compare the same when you use > and <:
7 < 10 but '7' > '10'
4. Incomplete logic. Looks like you are doing military time evaluations. normally there's a 0 hour - I don't see a 0 accounted for in all your logic. Perhaps there can't be a 0 hour. Even so, you should have a default case--even if that default is to throw an error. You need to write code that accounts for all possibilities even ones that "shouldn't" be there. Between is fine to use, but make sure there are no unaccounted for end points beyond the Between statements.
9
08
14
7:3
1122
7p
2230
17:29
and the computer rejects
2230a
73:22
as:00
The output will always be a sortable time format like '10:20 pm'.
I agree using Case statements where the answer is a boolean is a bad idea. I am trying to anticipate odd time entries that don't make sense, like '2230a', and so working through the scenarios, I'm discovering that in many instances, if statements were better. I've starting converting some of the code from Case to If. It's evolving and simplifying as I churn through the non-sensical possibilities of allowing someone to enter '13a', for example, which might be converted to 1:00 pm with a msgbox warning that the deduction may be wrong.
My solution is to iterate through every Case where the entry is Len() = 1 to Len() = 6 so the initial Case is necessary. An example of the tough code-churning for Len()=5 appears at the end.
This is my first big effort to re-write Access to make it appear to have fuzzy logic, more flexible and deductive. I appreciate the theme, 'Use the right tool for the job'.
Dim CC As Control
Dim CCName As String
Dim CCLength As Integer
Set CC = Screen.ActiveControl
CCName = CC.Name
CCLength = Len(CC)
' Partial time entries assume 7am is a cut off for am/pm assumptions, meaning if a partial entry is a '7',
' then the code deduces you mean 7 am, but if '6' then pm is deduced.
' ! ToolTip advising that that if entering a partial time of '7' or higher, it is assumed to be am, and lower is assumed pm.
Case Is = 5 ' For example, '1:30a' = 1:30 am, '12:30p' = 12:30 pm, or '2230p' = 10:30 pm, so the colon may be missing, or it can be in one of two positions
If IsNumeric(CC) Then ' If the entry is all numbers, deducing the time is difficult
GoTo EnterValidTime
End If
If InStr(CC, ":") = False Then ' For example, '2230p' = 10:30 pm
If Right(CC, 1) = "a" And Left(CC, 2) > 12 Then
MsgBox "You may have meant 'pm'?"
Exit Sub
End If
If Left(CC, 2) > 12 And Right(CC, 1) = "p" Then ' Military time and the usage of pm is incompatible and may lead to errors
MsgBox "Military time and the usage of pm is incompatible and may lead to errors."
Exit Sub
End If
CC = Left(CC, 2) & ":" & Mid(CC, 3, 2) & IIf(Right(CC, 1) = "a", " am", " pm")
Exit Sub
End If
Select Case InStr(CC, ":") ' For example, '1:30a' = 1:30 am, '12:30p' = 12:30 pm
Case Is = 2
CC = Left(CC, 1) & ":" & Mid(CC, 3, 2) & IIf(Right(CC, 1) = "a", " am", " pm")
Case Is = 3 ' For example, '11:30' = 11:30 am, '12:4a' = 12:40 am
Select Case Left(CC, 2)
Case Is > 11
CC = (Left(CC, 2) - 12) & ":" & Mid(CC, 4, 2) & " pm"
Exit Sub
Case Is = 12
CC = Left(CC, 2) & ":" & Mid(CC, 4, 2) & " pm"
Exit Sub
Case Else
CC = Left(CC, 2) & ":" & Mid(CC, 4, 2) & Switch(Right(CC, 1) = "a", " am", Right(CC, 1) = "p", " pm", 1 = 1, " am")
Exit Sub
End Select
Case Else
CC = Left(CC, 2) & ":" & Mid(CC, 3, 2) & " " & IIf(Right(CC, 1) = "a", " am", " pm")
Exit Sub
End Select
End Select