473,546 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.OpenRecordse t("tblClientTre atment")
With rsAdd
.AddNew
!ClientID = Me.ClientID
!TreatmentPlanC omplete = Me.TxPlanComple ted
!TreatmentPlanR eviewed = Me.TxPlanReview ed
!SignedByClient = Me.TxClientSign ed
!NextReview = DateAdd("d", Me.txtNumberOfD ays,
qryListClientTr eatmentLast.Las tReview)
.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 3707
Greetings,

The reason you are getting your error is because

qryListClientTr eatmentLast.Las tReview

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 qryListClientTr eatmentLast.Las tReview 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("LastRe view", "tblClientTreat ment", "some criteria would go
here")

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

d1 = DMax("LastRevie w", "tblClientTreat ment")

where d1 contains the DateValue of LastReview. Now you can say
...
!NextReview = DateAdd("d", Me.txtNumberOfD ays,
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.OpenRecordse t("tblClientTre atment")
With rsAdd
.AddNew
!ClientID = Me.ClientID
!TreatmentPlanC omplete = Me.TxPlanComple ted
!TreatmentPlanR eviewed = Me.TxPlanReview ed
!SignedByClient = Me.TxClientSign ed
!NextReview = DateAdd("d", Me.txtNumberOfD ays,
qryListClientTr eatmentLast.Las tReview)
.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 "qryListClientT reatmentLast.La stReview"?
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 qryListClientTr eatmentLast query has only one record, then you
can use (directly in a form or report control control source):

= DateAdd("d",txt NumberOfDays,
DLookUp("[LastReview]","qryListClien tTreatmentLast" ))

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",txt NumberOfDays,
DLookUp("[LastReview]","qryListClien tTreatmentLast" ,"[ClientID] = " &
[ClientID]))
--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.
Nov 13 '05 #3
an******@co.sai nt-croix.wi.us (Annette Massie) wrote in message news:<c6******* *************** ****@posting.go ogle.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.OpenRecordse t("tblClientTre atment")
With rsAdd
.AddNew
!ClientID = Me.ClientID
!TreatmentPlanC omplete = Me.TxPlanComple ted
!TreatmentPlanR eviewed = Me.TxPlanReview ed
!SignedByClient = Me.TxClientSign ed
!NextReview = DateAdd("d", Me.txtNumberOfD ays,
qryListClientTr eatmentLast.Las tReview)
.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.txtNumberOfD ays, qryListClientTr eatmentLast.Las tReview)

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

GetLastTreatmen t=rs.Fields("Tr eatmentDate").V alue
End Function

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

!NextReview = DateAdd("d", Me.txtNumberOfD ays,
GetLastTreatmen t("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
12323
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 End Range 0 100 200 300
1
2273
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 store. Arjen <html> <head>
6
3250
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 by a form. I created an update query called qryUpdate, which has both tblCustomers and qrySelect (linked by CustomerID). And I try to update one...
2
7345
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
1236
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 their descriptions are in separate tables. The DataKeyField of the DataGrid is set to the description ID, but I also need to be able to access the...
4
5148
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 table called Place. My Data Sources window shows AreaDataSet at the top, and the Places table underneath. Expanding the Places table shows me each...
7
7208
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 = "xxxx"; $pwd = "xxxxx"; $db = "xxxxx";
8
8035
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: WHERE LIKE '" & ASPvar & "' % ORDER BY ... However, my call is similar to: conn.qMyLookup strVar, rs
1
1222
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 form for both. When I add a record from the first form it adds the data and generates an auto number key then moves onto the second form. My only...
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.