Results 1 to 8 of 8
  1. #1
    Deutz is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    51

    UPDATE query with many fields

    Hi and thanks in advance,



    I am using Access 2003.

    I have an UPDATE query (which crashes) where I am trying to update a table with about 50 columns from the result of a query. The field names in the query match the fieldnames in the table but the table also has a lot of other fields as well.

    Is it possible to write some SQL where you do not need to explictly type in all 50 field names in the SET part of the UPDATE (since the field names match) ...

    I came up with this below but of course it is not quite right:

    UPDATE Revenue INNER JOIN qryRevenue ON Revenue.pk_PlanID = qryRevenue.pk_PlanID
    SET (Select * FROM Revenue) = (SELECT * FROM qryRevenue) "




    Thanks
    Deutz

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    A few things:

    A table with 50 columns is a little rare. Perhaps the table is not normalized.

    In an Update query you do NOT have to SET every field. You only set those fields where the values are to be changed.

    What exactly is qryRevenue? How does it relate to the Revenue table?

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I'm just guessing here and it seems to me like you're trying to update the REVENUE field in the REVENUE table with a value from a query, but you have no established link between the query and the table. They must share a key field in order to do this. Secondly, if I'm right, why would you store a calculated value? calculated values can be reproduced in queries or reports and doing this sort of update is a prime reason why you want to normalize your database, updating summary information every time new information hits your database is a royal pain in the butt.

  4. #4
    Deutz is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    51
    The revenue table is just a temp table that I use along the way to getting data into the correct format for a number of reports. The reason for the temp table is that the processes is quite complex and involves many discreet pieces of data that have to be calculated and the queries are too complex to be executed in one go, so I break it up midstream with the temp table.
    I have no problems with this method, my only question is whether there is a way to not have to specify each and every fieldname in the SET part of the UPDATE query.

    The revenue table and the query have a unique field in common: pk_PlanID.

    thanks
    Deutz

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    See orange's response, the only thing you have to SET in an update query or the fields you're going to change. Note this is different than an INSERT INTO query. If you've got your intermediary query showing you what you want you shouldn't have to do anything fancy, just make the query an append query and add it to your intermediary table. (after purging it of data first I assume)

    I will agree with orange though if your calculations are that complex it's likely a sign that your database is not normalized. I've got databases that have tracked 10 years worth of financial data with grant tracking and general ledger accounting information and never run into a problem with a query that was too complex. Might be a good idea to look at normalization rules and see if you can benefit.

  6. #6
    Deutz is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    51
    Thanks for your replies; your points are well taken.

    It may seem that the db is not normalised as there are many fields in the one table, which I don't like either, but this is dictated by the nature of our dept's reporting requirements. So I have to carry out similar calculations on each and every field and end up with a number of queries that have to be combined to get the data into a format suitable for our reports ... hence the temp table.

    The first query to get the data into the temp table is an INSERT but the others that follow must by necessity be UPDATE queries, hence my question about an easy way to refer to the salient fields.

    I know you can do this in an INSERT query but it looks like this is not the case with an UPDATE query so I will just have to do it the long way.

    Once again, thanks for your suggestions.

    Deutz

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    If your intermediary table has data in it, it should also have a foreign key that you can use to identify updates that you want to perform. For instance let's say you're calculating bills for a client, in your intermediary table you'd have a clientID and the associated information, then when you update their information as you go along you should also have the clientID in the query you're using to update, if you do it should be a simple matter to update the intermediary table by linking it to your query through the client ID and taking information from the update query and placing it in the intermediary table.

  8. #8
    Deutz is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    51
    Thanks rpeare

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Update Query on empty fields
    By bryan in forum Access
    Replies: 3
    Last Post: 01-18-2011, 11:36 AM
  2. Update Query that uses fields from other tables?
    By DarrenReeder in forum Queries
    Replies: 1
    Last Post: 12-06-2010, 10:47 AM
  3. Cant update fields when using a query
    By JohnBoy in forum Queries
    Replies: 4
    Last Post: 06-12-2010, 01:13 AM
  4. Replies: 5
    Last Post: 03-20-2010, 08:30 AM
  5. Replies: 1
    Last Post: 02-03-2010, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums