473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dateadd on query value

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?
Nov 13 '05 #1
3 3691
Greetings,

The reason you are getting your error is because

qryListClientTreatmentLast.LastReview

does not represent a date value. It is just a field in a query. You
have to extract the date value from that field.

I am assuming that qryListClientTreatmentLast.LastReview is a query
specifically for retrieving the LastReview date. Like it only has one
column and one row. Well, you can use the DLookUp function to do the
same thing as your query:

Dim d1 As Date
d1 = DLookup("LastReview", "tblClientTreatment", "some criteria would go
here")

Or, if there is no criteria and it is just the Max date, you can do
this:

d1 = DMax("LastReview", "tblClientTreatment")

where d1 contains the DateValue of LastReview. Now you can say
...
!NextReview = DateAdd("d", Me.txtNumberOfDays,
d1)
...

DLookup and DMax are in the Access Help Files. There are also examples
of the usage.

HTH
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #2
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.
Nov 13 '05 #3
an******@co.saint-croix.wi.us (Annette Massie) wrote in message news:<c6**************************@posting.google. com>...
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?
Umm... where do you get the value from the query? Are you somehow
assured of only a single row/value being returned when you run it?
Normally, you'd either use a domain function (DSum, DCount,
DLookup...) to return a value, and then use that, or use a function to
return a single value.

so your code *might* (VERY provisional!) look something like this:

!NextReview = DateAdd("d", Me.txtNumberOfDays, qryListClientTreatmentLast.LastReview)

Function GetLastTreatment(strCustID As String) As Date
....open a parameterized query to return a single value..

GetLastTreatment=rs.Fields("TreatmentDate").Value
End Function

and then you could use that function in your code...

!NextReview = DateAdd("d", Me.txtNumberOfDays,
GetLastTreatment("cust0001"))

Hope I haven't completely lost you...
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Max | last post by:
Hi All, I am new to Sql and I want to know if a given value lets say its 225 is within ranges defined in the table. I have a table TblControl. Data type of both field is int. StartRange ...
1
by: Arjen | last post by:
IE 6 does not return the value of a dynamic option list. Mozilla performs fine. How can I get the value of a choice in IE? Simple example below. Option list is filled with first element of array...
6
by: Nicolae Fieraru | last post by:
Hi All, I was trying to update a field in a table, based on the results from a query. The table to be updated is tblCustomers and the query is qrySelect. This query has two parameters, provided...
2
by: OSMeier | last post by:
Hi Everyone, I am hoping you can help me. I am using MS Access 2000 for this. Here is the scenario: I have two tables: tblMain -------- ID (Primary Key)
1
by: Anna | last post by:
I have a simple DataGrid that displays a list of characteristics and allows you to edit a description for each characteristic. The query that feeds this involves a join, as the characteristics and...
4
by: spamsickle | last post by:
I've created a query using the Query Designer under VB Express, and I can execute it in the Query Designer to verify that it returns the data I want. My database is called Area. It contains one...
7
by: underground | last post by:
I wonder if possible on page load to query the value of a specific colmn and insert the result into another table. My query looks like so <? include("include/session.php"); ?> <? $usr =...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
1
by: Raito | last post by:
This is a fairly simple question, I haven't had much experience with Access and I've gone rusty on what I do know. I've relational linked 2 tables together via a primary key "orgid" and created a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.