Results 1 to 11 of 11
  1. #1
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37

    Update rows of data from other rows

    Hi,



    I have a table of data which will have values missing in some fields. What I'd like is the code for an update query that will look for records with these null values and then take the value from the previous relevant record (with the previous relevant record being based on an "Order" and "Name" fields), and then populate the missing values. Please see the Before and After tables below as an example.

    Can anyone assist?

    Thanks so much in advance for your help.

    BEFORE
    Name Age Order
    Tom 20 1
    Dick 22 2
    Harry 31 3
    Dick 4
    Tom 5


    AFTER
    Name Age Order
    Tom 20 1
    Dick 22 2
    Harry 31 3
    Dick 22 4
    Tom 20 5

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    it really looks like your data is not stored correctly, you should have name/age in one table so dick and tom only appears once. Also age should always be calculated, you should instead be storing the date of birth. Otherwise, next year you will need to change all the ages.

    And what is the significance of order?

  3. #3
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37

    data from other rows

    Hi Ajax,

    The data tables I showed are just an example to illustrate my need (and a bad example at that). It was just my attempt to simplify the question.

    My real need is still there, namely to populate null fields for records with the same key ("name" for example), using another field to determine the most recent, previous record with that missing field (the "Order" field for example, with the highest value).

    I'm sure its doable but not sure how.

    Can you assist?

    Thanks


    Quote Originally Posted by Ajax View Post
    it really looks like your data is not stored correctly, you should have name/age in one table so dick and tom only appears once. Also age should always be calculated, you should instead be storing the date of birth. Otherwise, next year you will need to change all the ages.

    And what is the significance of order?

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    Making it too simple and too far removed from the real thing can be very problematic for those trying to help. For one thing, Name is not a label (name) you can use for any field or control. The only way I can see accomplishing the goal is to either create a custom function if this is strictly working against tables (I struggle too much with sub queries to be of much help there) or you could calculate this on a form. The problem I'd have with this outside of the procedure method is how to get around the circular reference since name has to refer to itself when looking up the DMax of Age based on that name. A calculated textbox on a form along with a textbox that references the table name field can solve the circular reference issue.
    Code:
    =IIf(IsNull([text2]),DMax("[age]","[tblMaxWhereNull]","[svname] = '" & [text0] & "'"),[text2])
    where text2 holds the name, text0 the age and the calculated age control contains the expression. If the age appears Null, the calculation provides the age you need. However, this would only be a temporary thing that allows you to do the updates and fix things. It shouldn't be an on-going concern, otherwise I also tend to think you have a data structure issue. Be warned that DMax calculations on your table might be slower than you'd like, and will only slow down as the records increase, which is why I don't consider this a viable solution for on-going data fixing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37

    appreciate your efforts...

    Thanks Micron.

    I was kind of thinking I could use a bit of SQL that utilised "SELECT TOP 1" on the "Order" column (as long as it wasn't NULL), and take the value from there, to populate the missing field data.

    Is there anything workable on that front? And if so, what would the code be?

    Again... thanks for the help.

    Quote Originally Posted by Micron View Post
    Making it too simple and too far removed from the real thing can be very problematic for those trying to help. For one thing, Name is not a label (name) you can use for any field or control. The only way I can see accomplishing the goal is to either create a custom function if this is strictly working against tables (I struggle too much with sub queries to be of much help there) or you could calculate this on a form. The problem I'd have with this outside of the procedure method is how to get around the circular reference since name has to refer to itself when looking up the DMax of Age based on that name. A calculated textbox on a form along with a textbox that references the table name field can solve the circular reference issue.
    Code:
    =IIf(IsNull([text2]),DMax("[age]","[tblMaxWhereNull]","[svname] = '" & [text0] & "'"),[text2])
    where text2 holds the name, text0 the age and the calculated age control contains the expression. If the age appears Null, the calculation provides the age you need. However, this would only be a temporary thing that allows you to do the updates and fix things. It shouldn't be an on-going concern, otherwise I also tend to think you have a data structure issue. Be warned that DMax calculations on your table might be slower than you'd like, and will only slow down as the records increase, which is why I don't consider this a viable solution for on-going data fixing.

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    you would have to use domain functions. for a normal select query you can use subqueries, but update queries cannot use subqueries

    since your data is not clear, best I can suggest is something like

    Update mytable
    Set age=dlookup("age","mytable","name='" & name & "' AND isnull(age)=false AND order=" & dmax("order","mytable","name='" & name & "' AND isnull(age)=false"))

  7. #7
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    Good point, Ajax. But an update can use a select subquery as a table, no?
    Too complicated for me. I think I'd try a vba procedure.

    @anasttin - experiment with a table copy. When I placed my field and table names in the suggested expression, it wiped all the age values.
    Later, if I can, I'll try playing around with the suggestion to see what parts of it return what, but Ajax will probably beat me to it as I have other things nagging at me at present.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    But an update can use a select subquery as a table, no?
    it can but in this case the OP needs the 'previous' value so needs to reference the 'original' table on a record by record basis. If they just needed the max value for example, I think you could do it with an aliased group by query

    The OP has not been clear about the real requirement and there are other ways to skin the cat. My way is not that efficient but you can only work with what you have.

    There is one small refinement to my suggestion. Since it only needs to update records where age is null, that can be included as a criteria

  9. #9
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    Well, as I mentioned, I tend to struggle with sub queries, but I'm not going to delve into a vba procedure with the pseudo fields and tables that were provided. It would just have to be re-written to suit reality.
    Last edited by Micron; 06-27-2017 at 07:50 PM. Reason: spelin

  10. #10
    Join Date
    Jun 2017
    Posts
    1
    Hi sir .. can i ask a help... i would like to have a sample database which shows the opening and closing balance
    example: DATE ITEM OpeningBalance Arrival Sold Ending
    06/27/2017 Ballpen 5 10 5 10
    and on the next day the ending balance must be the Opening balance like
    06/28/2017 Ballpen 10 0 5 5

  11. #11
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @ katrinabelle,
    First, Welcome to the forum.

    So what you have done is called "HI-jacking a thread". You posted your question in someone else's thread.
    By doing this, you will get very few responses because no one will see your post.

    You should start your own thread, describing your problem or question..

    Good luck......

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

Similar Threads

  1. Show % of current rows in total rows.
    By redbull in forum Programming
    Replies: 8
    Last Post: 03-29-2016, 09:26 AM
  2. Replies: 22
    Last Post: 12-20-2015, 02:46 AM
  3. Replies: 4
    Last Post: 02-25-2014, 01:09 AM
  4. Replies: 0
    Last Post: 02-09-2012, 05:43 PM
  5. Replies: 2
    Last Post: 08-28-2011, 06:06 AM

Tags for this Thread

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