473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update Query Question

8 New Member
Hi again, I’ve hit another problem, I hope someone can help.

I have a table [tblTreatment Received] that contains numerous records for the same single record in the other table [tblAppointments]. The [tblAppointments] table is therefore related to the [tblTreatment Received] in a one-to-many relationship.

I have created an update query that I want to update a field in the main table [tblAppointments] with the sum of all the Unit Costs from the related table. At the moment, it's only updating it with one of them. E.g. there are three unit costs saved for one appointment , £200, £50 and £25; when I run the query, the field updates to £25 instead of £275. At present, the query is set to update the field [AppCost] in the [tblAppointments] table and in the ‘update to:’ I’ve simply entered ‘[tblTreatmentRec eived].[Unit Cost].

Does anyone know how to make an update query update the field with the sum of all of the fields rather than just one. I tried using Sum() but no go.

Thanks a lot.
Feb 22 '08 #1
2 1095
ADezii
8,834 Recognized Expert Expert
Hi again, I’ve hit another problem, I hope someone can help.

I have a table [tblTreatment Received] that contains numerous records for the same single record in the other table [tblAppointments]. The [tblAppointments] table is therefore related to the [tblTreatment Received] in a one-to-many relationship.

I have created an update query that I want to update a field in the main table [tblAppointments] with the sum of all the Unit Costs from the related table. At the moment, it's only updating it with one of them. E.g. there are three unit costs saved for one appointment , £200, £50 and £25; when I run the query, the field updates to £25 instead of £275. At present, the query is set to update the field [AppCost] in the [tblAppointments] table and in the ‘update to:’ I’ve simply entered ‘[tblTreatmentRec eived].[Unit Cost].

Does anyone know how to make an update query update the field with the sum of all of the fields rather than just one. I tried using Sum() but no go.

Thanks a lot.
Here is a solution which definately works, but I'm pretty sure the SQL Gang will come up with a better one, so be patient. First, a couple of Assumptions:
  1. tblAppointments contains a Field named [Total Treatment Cost] {CURRENCY} which will contain the Total Cost of each related Treatment in tblTreatmentRec eived.
  2. tblTreatmentRec eived contains a Field named [Cost] {CURRENCY} which contains indiviodual ttreatment costs related to a specific Appointment.
  3. tblAppointments and tblTreatmentRec eived are related by the Field [AppID], namely: tblAppointments .[AppID]{1} ==> tblTreatmentRec eived.[AppID](MANY}.
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String, MyDB As Database, MyRS As DAO.Recordset
  2.  
  3. strSQL = "Select * From tblAppointments;"
  4.  
  5. Set MyDB = CurrentDb()
  6. Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  7.  
  8. Do While Not MyRS.EOF
  9.   MyRS.Edit
  10.     MyRS![Total Treatment Cost] = DSum("[Cost]", "tblTreatmentReceived", "[AppID] = " & MyRS![AppID])
  11.   MyRS.Update
  12.   MyRS.MoveNext
  13. Loop
  14.  
  15. MyRS.Close
  16. Set MyRS = Nothing
Feb 22 '08 #2
uksigma
8 New Member
Thanks for that.
It seems to work okay!
Feb 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
10238
by: Surendra | last post by:
I have this query that I need to use in an Update statement to populate a field in the table by the value of Sq ---------------------------------------------------------------------------- Inline View Query: Select Sq from ( Select substr(to_date(End_Date,"DD-MON-YYYY"),4), End_Date, Rank() Over (Partition by substr(to_date(End_Date,"DD-MON-YYYY"),4) Order by End_Date) As Sq
17
14080
by: Felix | last post by:
Dear Sql Server experts: First off, I am no sql server expert :) A few months ago I put a database into a production environment. Recently, It was brought to my attention that a particular query that executed quite quickly in our dev environment was painfully slow in production. I analyzed the the plan on the production server (it looked good), and then tried quite a few tips that I'd gleaned from reading newsgroups. Nothing worked....
17
5030
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by cust_no, ded_type_cd, chk_no)
2
8542
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An example of what I'm trying to do is below: update (tbl_ind_mananas LEFT JOIN (select count(*) as count, (dubicacion || zona || manzana) as cod_manzana from tbl_censo_poblacion_1993 group by dubicacion, zona, manzana) tbl1 on relacion = cod_manzana) as...
4
2259
by: dp | last post by:
After looking and looking, it appears that Access ADPs graphic query designer won't display: update customer set = . from customer, where customer. = .; It comes up with the "Query Definitions Differ" dialog box. Anybody know anything about this? I can live this with I guess, however it was sure
10
3286
by: Randy Harris | last post by:
I imported records into a table, later found out that many of them had trailing spaces in one of the fields. If I'd caught it sooner, I could have trimmed the spaces before the import. This wouldn't work (nothing changed): UPDATE tblManuals SET tblManuals.PARTNUM = Trim(); Would someone please tell me how to do an update query that will trim the spaces?
5
4705
by: Andrew | last post by:
I've got a list box that selects a record on a subform, and in that subform are a few text fiels and a button that runs an update query. How do I have the update query button run and only update the record that is selected in the list box? The data updates right, but I can't get the update query to do anything but update all of the records. Thanks, Andrew
4
11342
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET AddressDescription of Entity 456 = AddressDescription of Entity_ID 123 Address1 of Entity 456 = Address1 of Entity_ID 123 City of Entity 456 = City of Entity_ID 123
12
1631
by: si_owen | last post by:
Hi all, I have a SQL query that worked fine in my project until it came to testing. I found that the NvarChar fields I have wont accept the use of an ' My code and query is here does anyone know how to change the query to accept an ' whilst keeping the data true.
16
3524
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
0
9687
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.