I have a text box on a form. This form is built off just 1 table.
The form field in question is feeding the "minutes" field of the table.
The table data contains minutes such as 15, 60, 240, etc.
The form however I need it to display, or allow the users to enter data in h:nn format or decimal such as either 1:30 or 1.5
I have this unbound text box with the following Before and After event procedures:
Code:
Private Sub txtMinutesWorked_AfterUpdate()
txtMinutesWorked = FormatMinutesAsHMM([MinutesWorked])
End Sub
Private Sub txtMinutesWorked_BeforeUpdate(Cancel As Integer)
Dim vResult As Variant
vResult = ParseMinutes(txtMinutesWorked)
If IsEmpty(vResult) Then
Cancel = True
Else
MinutesWorked = vResult
End If
End Sub
The form itself has the following Current event procedure applied:
Code:
Private Sub Form_Current()
txtMinutesWorked = FormatMinutesAsHMM([MinutesWorked])
End Sub
Those event procedures are using this code from my custom public module:
Code:
Option Compare Database
'Format Minutes
Public Function FormatMinutesAsHMM(MinutesValue As Variant) As String
If IsNumeric(MinutesValue) Then
FormatMinutesAsHMM = Format(MinutesValue \ 60, "0") & ":" & Format(MinutesValue Mod 60, "00")
Else
FormatMinutesAsHMM = ""
End If
End Function
'Parse Minutes
Public Function ParseMinutes(vTime As Variant) As Variant
Dim h As Long, m As Long, aTime As Variant
On Error GoTo ProcErr
aTime = Split(vTime & "", ":")
Select Case UBound(aTime)
Case -1 ' no data entered - return null with no error
ParseMinutes = Null
Case 0 ' no colon found
If Not IsNumeric(aTime(0)) Then Err.Raise 5 ' hours not numeric
If aTime(0) < 0 Then Err.Raise 5 ' hours negative
ParseMinutes = CLng(aTime(0) * 60)
Case 1 'exactly one colon
If Not IsNumeric(aTime(0)) Then Err.Raise 5 ' hours not numeric
If aTime(0) <> "" & CLng(aTime(0)) Then Err.Raise 5 ' hours not whole number
If aTime(0) < 0 Then Err.Raise 5 ' hours negative
If Not IsNumeric(aTime(1)) Then Err.Raise 5 ' minutes not numeric
If aTime(1) <> "" & CLng(aTime(1)) Then Err.Raise 5 ' minutes not whole number
If aTime(1) < 0 Then Err.Raise 5 ' minutes negative
If aTime(1) >= 60 Then Err.Raise 5 ' minutes 60 or more
ParseMinutes = aTime(0) * 60 + aTime(1)
Case Else ' more than one colon
Err.Raise 5 ' invalid
End Select
ProcEnd:
On Error Resume Next
Exit Function
ProcErr:
ParseMinutes = Empty
If Err = 5 Then
MsgBox "Invalid time", vbExclamation
Else
MsgBox Err.Description, vbExclamation
End If
Resume ProcEnd
End Function
How can I make this become data entry? My problem is I have the form set as Data Entry=Yes, but this code isn't allowing me to enter data, only allows me to view/parse the data if its already existing. But as you know, with Data Entry=Yes my form loads on a new record so there isn't any data to view yet.
I need this form text box to be setup for data entry. The user will put in either 1:30 or 1.5, and in turn when they hit the save button it will turn that into 90 for the table.
Thank you in advance,
Tom