On 6 Oct 2004 06:28:35 -0700, Annette Massie wrote:
I am trying to insert a record into a table via code and one of the
values to add I would like as a dateadd calculation on a value from a
query.
My code looks like this:
Set db = CurrentDb() ' if the table is in the same database
Set rsAdd = db.OpenRecordset("tblClientTreatment")
With rsAdd
.AddNew
!ClientID = Me.ClientID
!TreatmentPlanComplete = Me.TxPlanCompleted
!TreatmentPlanReviewed = Me.TxPlanReviewed
!SignedByClient = Me.TxClientSigned
!NextReview = DateAdd("d", Me.txtNumberOfDays,
qryListClientTreatmentLast.LastReview)
.Update
.Close
End With
It is erroring out at the dateadd statement. Is there a different way
to do this?
A couple of things to consider.
1) You are trying to store a calculated field (NextReview) in your
table, which is a No No in Access.
Fields which are of a derived nature should not be stored.
When your NextReview date is needed, use a DateAdd() expression, based
upon the [LastReview] to return the Next Review date.
2) If you are doing this code in an Access Module, the Me Keyword
cannot be used, as the module is not part of any form/report object.
If you arte doing this code in a form's code window, then your use of
Me! is OK.
3) What is "qryListClientTreatmentLast.LastReview"?
If that is a field returned by a query, you need to use DLookUp() to
return the value. DLookUp(), without any additional criteria, will
return the first record's value it comes to.
If the qryListClientTreatmentLast query has only one record, then you
can use (directly in a form or report control control source):
= DateAdd("d",txtNumberOfDays,
DLookUp("[LastReview]","qryListClientTreatmentLast"))
Note: Me is a VBA keyword, not an Access keyword, so you cannot use
Me! in a control's control source.
If the query returns more than one record you will have to add
criteria to the DLookUp to get the correct value. Based upon the
information you posted, the criteria MAY be something like this:
= DateAdd("d",txtNumberOfDays,
DLookUp("[LastReview]","qryListClientTreatmentLast","[ClientID] = " &
[ClientID]))
--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.