Ayo said:
We only have a Current event that is suppose to execute only when there
are
values in 2 of the txt boxes, see below
Private Sub Form_Current()
If Me.Date_Invoice_Received_In_A_P.Value <> "" And
Me.Date_Invoice_Received_In_Market.Value <> "" Then
Me.Days_to_Market.Value = DateDiff("d",
Me.Date_Invoice_Received_In_A_P.Value,
Me.Date_Invoice_Received_In_Market.Value)
Else
Me.Days_to_Market.Value = 0
End If
End Sub
Yes, that would do it, all right. Every time you arrive at a new record,
you set the value of Days_to_Market, either to a value calculated from the
other text boxes, or to zero. That will happen whether you are on a blank
new record, or on an existing record.
At the very least, you should fix the date tests to check for Null rather
than "", and you should drop the Else clause that sets Days_to_Market to
zero. How about this:
If Not IsNull(Me.Date_Invoice_Received_In_A_P) _
And Not IsNull(Me.Date_Invoice_Received_In_Market) _
Then
Me.Days_to_Market = _
DateDiff("d", _
Me.Date_Invoice_Received_In_A_P,
Me.Date_Invoice_Received_In_Market)
End If
I would leave Days_to_Market as Null when either of those base dates is
unknown; however, you could have it *default* to zero by setting it's
Default Value property to 0.
But aside from all that, this looks a whole lot like storing a calculated
value. Is it possible for the value of Days_to_Market ever to have a value
that is *different* from the number of days between those two dates? If
not, there is not reason ever to store it, and several good reasons not to
(efficiency, reliability). So in all probability, you should just define
this as a calculated field, either in the text box's controlsource or as a
calculated field in the form's recordsource query. If you do it as a
calculated control, the controlsource would be:
=DateDiff("d", [Date_Invoice_Received_In_A_P],
[Date_Invoice_Received_In_Market])
As a calculated field in the query, you might enter something like this in
the query design grid:
Days_to_Market: DateDiff("d", Date_Invoice_Received_In_A_P,
Date_Invoice_Received_In_Market)
Note that both of the above expressions will have been broken onto two lines
by the newsreader, but they are intended each to be on a single line.