I have a form where a user can enter a start date (dtDateStart) and end date (dtDateEnd) or a length of time in hours (intLength). The users that work in this application will typically only know 1 or 2 pieces of this at a time, so I use code to calculate the rest. We are having an issue right now where a user may have 2 parts filled in, and the 3rd calculates, which is fine.
The problem comes in when a user wants to wipe out 1 value, because they get updated information. When a user goes to wipe out, say a start date or end date, the minute they leave the field, it re-calculates and fills the old value back in. This makes it impossible to change values.
This is my code. If anyone can help me figure out a way to run this calc. but also let the users clear out a value and add one in later, it would really help.
I was thinking of a button to clear all values, or one to run the calc, but I am not sure what would be most user friendly.
Thanks for any help!
misscrf
It is never too late to become what you could have been ~ George Eliot
The problem comes in when a user wants to wipe out 1 value, because they get updated information. When a user goes to wipe out, say a start date or end date, the minute they leave the field, it re-calculates and fills the old value back in. This makes it impossible to change values.
This is my code. If anyone can help me figure out a way to run this calc. but also let the users clear out a value and add one in later, it would really help.
I was thinking of a button to clear all values, or one to run the calc, but I am not sure what would be most user friendly.
Code:
Option Compare Database
Public flgChange As Byte
Private Sub dtDateEnd_AfterUpdate()
Call CalcMissing
End Sub
Private Sub dtDateStart_AfterUpdate()
Call CalcMissing
End Sub
Private Sub intLength_AfterUpdate()
Call CalcMissing
End Sub
Private Sub dtDateEnd_Change()
flgChange = 2
End Sub
Private Sub dtDateStart_Change()
flgChange = 1
End Sub
Private Sub intLength_Change()
flgChange = 4
End Sub
Public Function CalcMissing()
Dim BinVal As Byte
'Note Binary Bit Positions: dtDateStart=1, dtDateEnd=2, intLength=4
BinVal = Abs(IsDate(Me.dtDateStart) = True) + _
Abs(IsDate(Me.dtDateEnd) = True) * 2 + _
Abs(IsNumeric(Me.intLength) = True) * 4
'Set BinVal by deleting sought value from all three!
If BinVal = 7 Then 'All data present but edit was made
If flgChange = 1 Or flgChange = 2 Then
' Me.intLength = Null
BinVal = 3
ElseIf flgChange = 4 Then
' Me.dtDateEnd = Null
BinVal = 5
End If
End If
flgChange = 0
'Calculations Here
If BinVal = 3 Then 'calculate hours
Me.intLength = DateDiff("h", Me.dtDateStart, Me.dtDateEnd)
ElseIf BinVal = 5 Then 'calculate dtDateEnd
Me.dtDateEnd = DateAdd("h", Me.intLength, Me.dtDateStart)
ElseIf BinVal = 6 Then 'calculate dtDateStart
Me.dtDateStart = DateAdd("h", (Me.intLength * -1), Me.dtDateEnd)
End If
End Function
Private Sub dtDateEnd_Click()
flgChange = 2
DoCmd.OpenForm "frmMiniDateTime", , , , , acDialog
Call CalcMissing
End Sub
Private Sub dtDateStart_Click()
flgChange = 1
DoCmd.OpenForm "frmMiniDateTime", , , , , acDialog
Call CalcMissing
End Sub
Thanks for any help!
misscrf
It is never too late to become what you could have been ~ George Eliot