Update one table with another table field value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgitaunjoroge
    New Member
    • Nov 2015
    • 4

    Update one table with another table field value

    Hi All,

    Am trying to update one table with another tables value where both tables have a common relationship.
    my code is a below.

    Code:
    DoCmd.RunSQL "update customer set customer.paid" = totalprojsumpaid.AMOUNT where "customer.ACCNO = totalprojsumpaid = ACCNO"
    Note the two fields calculated fields.

    Thanks,
    Njoroge Joseph
  • strive4peace
    Recognized Expert New Member
    • Jun 2014
    • 39

    #2
    perhaps this?

    Code:
    dim sSQL as string
    ssql = "UPDATE Customer C INNER JOIN totalprojsumpaid TP " _
    & " ON C.ACCNO = TP.ACCNO " _
    & " SET C.Paid = TP.Amount"
    
    currentdb.execute sSQL

    This will do all records since there is no criteria for a specific account

    Comment

    • jgitaunjoroge
      New Member
      • Nov 2015
      • 4

      #3
      Strive4peace,

      Please note when i run the code it returns Two few parameters. Expected 1

      Customer.paid is the main table being update by totalprojsumpai d.amount
      the criteria is Customer.accno= txtaccno must be same with totalprojsumpai d.accno as it is in relationship. txtaccno is a text field recording accno for both tables.

      Comment

      • strive4peace
        Recognized Expert New Member
        • Jun 2014
        • 39

        #4
        your error message suggests that something is not filled out. What is the SQL for totalprojsumpai d ? (am assuming it must be a query).

        Perhaps there is a better way to update the paid field in your customers table.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          jgitaunjoroge,

          What we should understand first is why you are doing this in the first place. A normalized database most likely would not need such an update between tables.

          Comment

          • strive4peace
            Recognized Expert New Member
            • Jun 2014
            • 39

            #6
            sorry to disagree, but not necessarily. If methods are in place that can be run anytime, storing some calculated fields in favor of better performance is ok, in my opinion. I know not everyone agrees with this but I have seen many databases that took a long time to calculate -- not because they weren't designed properly, but because there was so much data.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              S4P:
              Certainly there are times when such things are needed - agreed; however, I am not sure why you would disagree with my last post in it's entirety, in as much as it is an inquiry regarding the basis for OP's goal - in which I noted that most likely such things are not needed within a properly normalized database - not that there is never a need... to whit: Is it for an audit trail, a one time discounted price, or as you've noted - a prefetch/calc for efficiency on a large dataset (I've done this myself in temp tables upon occasion :) )

              IMHO, once we have a better understanding of what OP is trying to accomplish we might be able to point him towards a solution to the problem or maybe a better method of handling the data. Indeed, we may need a bit more detail too before the picture clears up for us. :)

              Comment

              • strive4peace
                Recognized Expert New Member
                • Jun 2014
                • 39

                #8
                sorry, it just seemed a little rough to me .... I agree that a better understanding would help.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  strive4peace, if occasionally something seems/appears to be harsh, please keep in mind that many of my shorter posts are along the line of a "text-message" - devoid of body language - short - concise. I work in a lab and all too often these post are made in the 5 minute break between tests and my normal data-entry and DBA tasks; thus, I need to pack a lot of information in a very short time frame....

                  bright side... I've learned to touch type at a rate I would never have thought possible :)

                  Comment

                  • strive4peace
                    Recognized Expert New Member
                    • Jun 2014
                    • 39

                    #10
                    Thanks. I have read some of your posts, zmbd, and most are very good (must be that scientist way of thinking). Oftentimes, alleviating frustration with comforting words gives others a way to see the answer for themselves. Access is hard to learn. I applaud those who try.

                    Comment

                    Working...